62 lines
1.4 KiB
C#
62 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="shard">Parent GatewayShard instance</param>
|
|
public GatewayHeartbeatManager(GatewayShard shard)
|
|
{
|
|
Shard = shard;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
}
|