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