Archived
1
0
Fork 0
This repository has been archived on 2024-05-21. You can view files and clone it, but cannot push or open issues or pull requests.
maki/Maki/Gateway/GatewayHeartbeatManager.cs

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;
}
}
}