using System;
using System.Threading;
namespace Maki.Gateway
{
///
/// Handles Heartbeat (pings) in the background for a Shard
///
class GatewayHeartbeatManager
{
///
/// Subject shard
///
private readonly GatewayShard shard;
///
/// Interval timer for repeating the request
///
private Timer timer = null;
///
/// Constructor
///
/// Parent GatewayShard instance
public GatewayHeartbeatManager(GatewayShard s)
{
shard = s;
}
///
/// Starts sending heartbeats
///
public void Start()
{
if (timer != null)
Stop();
timer = new Timer(Handler, null, TimeSpan.Zero, shard.HeartbeatInterval);
}
///
/// Heartbeat sender
///
///
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;
}
}
}