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 shard)
{
Shard = shard;
}
///
/// 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;
}
}
}