63 lines
1.4 KiB
C#
63 lines
1.4 KiB
C#
|
using System;
|
|||
|
using System.Threading;
|
|||
|
|
|||
|
namespace Maki.Gateway
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Handles Heartbeat (pings) in the background for a Shard
|
|||
|
/// </summary>
|
|||
|
class GatewayHeartbeatManager
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Subject shard
|
|||
|
/// </summary>
|
|||
|
private readonly GatewayShard shard;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Interval timer for repeating the request
|
|||
|
/// </summary>
|
|||
|
private Timer timer = null;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Constructor
|
|||
|
/// </summary>
|
|||
|
/// <param name="s">Parent GatewayShard instance</param>
|
|||
|
public GatewayHeartbeatManager(GatewayShard s)
|
|||
|
{
|
|||
|
shard = s;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Starts sending heartbeats
|
|||
|
/// </summary>
|
|||
|
public void Start()
|
|||
|
{
|
|||
|
if (timer != null)
|
|||
|
Stop();
|
|||
|
|
|||
|
timer = new Timer(Handler, null, TimeSpan.Zero, shard.HeartbeatInterval);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Heartbeat sender
|
|||
|
/// </summary>
|
|||
|
/// <param name="state"></param>
|
|||
|
private void Handler(object state)
|
|||
|
{
|
|||
|
shard.Send(GatewayOPCode.Heartbeat, shard.LastSequence);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Stops sending heartbeats
|
|||
|
/// </summary>
|
|||
|
public void Stop()
|
|||
|
{
|
|||
|
if (timer == null)
|
|||
|
return;
|
|||
|
|
|||
|
timer.Dispose();
|
|||
|
timer = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|