300 lines
12 KiB
C#
300 lines
12 KiB
C#
using Maki.Structures.Channels;
|
|
using Maki.Structures.Gateway;
|
|
using Maki.Structures.Guilds;
|
|
using Maki.Structures.Messages;
|
|
using Maki.Structures.Presences;
|
|
using Maki.Structures.Users;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Maki.Gateway
|
|
{
|
|
/// <summary>
|
|
/// Gateway connection shard manager
|
|
/// </summary>
|
|
class GatewayShardClient : IDisposable
|
|
{
|
|
/// <summary>
|
|
/// Managed (active) shards
|
|
/// </summary>
|
|
private readonly List<GatewayShard> Shards = new List<GatewayShard>();
|
|
|
|
private readonly object Lock = new object();
|
|
|
|
/// <summary>
|
|
/// Parent DiscordClient instance
|
|
/// </summary>
|
|
private Discord Client;
|
|
|
|
/// <summary>
|
|
/// Number of active managed shards
|
|
/// </summary>
|
|
public int ShardCount
|
|
=> Shards.Count;
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="client">Parent DiscordClient instance</param>
|
|
public GatewayShardClient(Discord client)
|
|
=> Client = client;
|
|
|
|
/// <summary>
|
|
/// Creates a new Gateway Shard
|
|
/// </summary>
|
|
/// <param name="id">Id of the shard</param>
|
|
/// <returns>New Gateway Shard</returns>
|
|
public GatewayShard Create(int id)
|
|
{
|
|
GatewayShard shard = new GatewayShard(id, Client);
|
|
ApplyEvents(shard);
|
|
|
|
lock (Lock)
|
|
Shards.Add(shard);
|
|
|
|
return shard;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys a Gateway Shard
|
|
/// </summary>
|
|
/// <param name="shard">Shard to destroy</param>
|
|
public void Destroy(GatewayShard shard)
|
|
{
|
|
lock (Lock)
|
|
Shards.Remove(shard);
|
|
|
|
// Disconnect and Dispose are called separately so OnSocketClose is called properly
|
|
shard.Disconnect();
|
|
RemoveEvents(shard);
|
|
shard.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys all shards
|
|
/// </summary>
|
|
public void Disconnect()
|
|
{
|
|
lock (Lock)
|
|
Shards.ToList().ForEach(x => Destroy(x));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Links all gateway events handlers to the shard
|
|
/// </summary>
|
|
/// <param name="shard">Shard to link events to</param>
|
|
private void ApplyEvents(GatewayShard shard)
|
|
{
|
|
shard.OnCallCreate += OnCallCreate;
|
|
shard.OnCallDelete += OnCallDelete;
|
|
shard.OnCallUpdate += OnCallUpdate;
|
|
shard.OnVoiceServerUpdate += OnVoiceServerUpdate;
|
|
shard.OnVoiceStateUpdate += OnVoiceStateUpdate;
|
|
|
|
shard.OnChannelCreate += OnChannelCreate;
|
|
shard.OnChannelDelete += OnChannelDelete;
|
|
shard.OnChannelPinsAck += OnChannelPinsAck;
|
|
shard.OnChannelPinsUpdate += OnChannelPinsUpdate;
|
|
shard.OnChannelRecipientAdd += OnChannelRecipientAdd;
|
|
shard.OnChannelRecipientRemove += OnChannelRecipientRemove;
|
|
shard.OnChannelUpdate += OnChannelUpdate;
|
|
|
|
shard.OnGuildBanAdd += OnGuildBanAdd;
|
|
shard.OnGuildBanRemove += OnGuildBanRemove;
|
|
shard.OnGuildCreate += OnGuildCreate;
|
|
shard.OnGuildDelete += OnGuildDelete;
|
|
shard.OnGuildEmojisUpdate += OnGuildEmojisUpdate;
|
|
shard.OnGuildIntegrationsUpdate += OnGuildIntegrationsUpdate;
|
|
shard.OnGuildMemberAdd += OnGuildMemberAdd;
|
|
shard.OnGuildMemberUpdate += OnGuildMemberUpdate;
|
|
shard.OnGuildMemberRemove += OnGuildMemberRemove;
|
|
shard.OnGuildMembersChunk += OnGuildMembersChunk;
|
|
shard.OnGuildRoleCreate += OnGuildRoleCreate;
|
|
shard.OnGuildRoleUpdate += OnGuildRoleUpdate;
|
|
shard.OnGuildRoleDelete += OnGuildRoleDelete;
|
|
shard.OnGuildUpdate += OnGuildUpdate;
|
|
|
|
shard.OnMessageAck += OnMessageAck;
|
|
shard.OnMessageCreate += OnMessageCreate;
|
|
shard.OnMessageDelete += OnMessageDelete;
|
|
shard.OnMessageDeleteBulk += OnMessageDeleteBulk;
|
|
shard.OnMessageReactionAdd += OnMessageReactionAdd;
|
|
shard.OnMessageReactionRemove += OnMessageReactionRemove;
|
|
shard.OnMessageReactionsRemoveAll += OnMessageReactionsRemoveAll;
|
|
shard.OnMessageUpdate += OnMessageUpdate;
|
|
|
|
shard.OnTypingStart += OnTypingStart;
|
|
shard.OnPresenceUpdate += OnPresenceUpdate;
|
|
shard.OnPresencesReplace += OnPresencesReplace;
|
|
|
|
shard.OnFriendSuggestionDelete += OnFriendSuggestionDelete;
|
|
shard.OnRelationshipAdd += OnRelationshipAdd;
|
|
shard.OnRelationshipRemove += OnRelationshipRemove;
|
|
|
|
shard.OnReady += OnReady;
|
|
shard.OnResumed += OnResumed;
|
|
|
|
shard.OnUserUpdate += OnUserUpdate;
|
|
shard.OnUserNoteUpdate += OnUserNoteUpdate;
|
|
shard.OnUserSettingsUpdate += OnUserSettingsUpdate;
|
|
|
|
shard.OnSocketOpen += OnSocketOpen;
|
|
shard.OnSocketClose += OnSocketClose;
|
|
shard.OnSocketError += OnSocketError;
|
|
shard.OnSocketMessage += OnSocketMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unlinks all gateway events handlers from the shard
|
|
/// </summary>
|
|
/// <param name="shard">Shard to unlink events from</param>
|
|
public void RemoveEvents(GatewayShard shard)
|
|
{
|
|
shard.OnCallCreate -= OnCallCreate;
|
|
shard.OnCallDelete -= OnCallDelete;
|
|
shard.OnCallUpdate -= OnCallUpdate;
|
|
shard.OnVoiceServerUpdate -= OnVoiceServerUpdate;
|
|
shard.OnVoiceStateUpdate -= OnVoiceStateUpdate;
|
|
|
|
shard.OnChannelCreate -= OnChannelCreate;
|
|
shard.OnChannelDelete -= OnChannelDelete;
|
|
shard.OnChannelPinsAck -= OnChannelPinsAck;
|
|
shard.OnChannelPinsUpdate -= OnChannelPinsUpdate;
|
|
shard.OnChannelRecipientAdd -= OnChannelRecipientAdd;
|
|
shard.OnChannelRecipientRemove -= OnChannelRecipientRemove;
|
|
shard.OnChannelUpdate -= OnChannelUpdate;
|
|
|
|
shard.OnGuildBanAdd -= OnGuildBanAdd;
|
|
shard.OnGuildBanRemove -= OnGuildBanRemove;
|
|
shard.OnGuildCreate -= OnGuildCreate;
|
|
shard.OnGuildDelete -= OnGuildDelete;
|
|
shard.OnGuildEmojisUpdate -= OnGuildEmojisUpdate;
|
|
shard.OnGuildIntegrationsUpdate -= OnGuildIntegrationsUpdate;
|
|
shard.OnGuildMemberAdd -= OnGuildMemberAdd;
|
|
shard.OnGuildMemberUpdate -= OnGuildMemberUpdate;
|
|
shard.OnGuildMemberRemove -= OnGuildMemberRemove;
|
|
shard.OnGuildMembersChunk -= OnGuildMembersChunk;
|
|
shard.OnGuildRoleCreate -= OnGuildRoleCreate;
|
|
shard.OnGuildRoleUpdate -= OnGuildRoleUpdate;
|
|
shard.OnGuildRoleDelete -= OnGuildRoleDelete;
|
|
shard.OnGuildUpdate -= OnGuildUpdate;
|
|
|
|
shard.OnMessageAck -= OnMessageAck;
|
|
shard.OnMessageCreate -= OnMessageCreate;
|
|
shard.OnMessageDelete -= OnMessageDelete;
|
|
shard.OnMessageDeleteBulk -= OnMessageDeleteBulk;
|
|
shard.OnMessageReactionAdd -= OnMessageReactionAdd;
|
|
shard.OnMessageReactionRemove -= OnMessageReactionRemove;
|
|
shard.OnMessageReactionsRemoveAll -= OnMessageReactionsRemoveAll;
|
|
shard.OnMessageUpdate -= OnMessageUpdate;
|
|
|
|
shard.OnTypingStart -= OnTypingStart;
|
|
shard.OnPresenceUpdate -= OnPresenceUpdate;
|
|
shard.OnPresencesReplace -= OnPresencesReplace;
|
|
|
|
shard.OnFriendSuggestionDelete -= OnFriendSuggestionDelete;
|
|
shard.OnRelationshipAdd -= OnRelationshipAdd;
|
|
shard.OnRelationshipRemove -= OnRelationshipRemove;
|
|
|
|
shard.OnReady -= OnReady;
|
|
shard.OnResumed -= OnResumed;
|
|
|
|
shard.OnUserUpdate -= OnUserUpdate;
|
|
shard.OnUserNoteUpdate -= OnUserNoteUpdate;
|
|
shard.OnUserSettingsUpdate -= OnUserSettingsUpdate;
|
|
|
|
shard.OnSocketOpen -= OnSocketOpen;
|
|
shard.OnSocketClose -= OnSocketClose;
|
|
shard.OnSocketError -= OnSocketError;
|
|
shard.OnSocketMessage -= OnSocketMessage;
|
|
}
|
|
|
|
#region Events
|
|
public event Action<GatewayShard> OnCallCreate;
|
|
public event Action<GatewayShard> OnCallDelete;
|
|
public event Action<GatewayShard> OnCallUpdate;
|
|
public event Action<GatewayShard> OnVoiceServerUpdate;
|
|
public event Action<GatewayShard> OnVoiceStateUpdate;
|
|
|
|
public event Action<GatewayShard, Channel> OnChannelCreate;
|
|
public event Action<GatewayShard, Channel> OnChannelDelete;
|
|
public event Action<GatewayShard> OnChannelPinsAck;
|
|
public event Action<GatewayShard> OnChannelPinsUpdate;
|
|
public event Action<GatewayShard> OnChannelRecipientAdd;
|
|
public event Action<GatewayShard> OnChannelRecipientRemove;
|
|
public event Action<GatewayShard, Channel> OnChannelUpdate;
|
|
|
|
public event Action<GatewayShard, User> OnGuildBanAdd;
|
|
public event Action<GatewayShard, User> OnGuildBanRemove;
|
|
public event Action<GatewayShard, Guild> OnGuildCreate;
|
|
public event Action<GatewayShard, Guild> OnGuildDelete;
|
|
public event Action<GatewayShard, Guild> OnGuildEmojisUpdate;
|
|
public event Action<GatewayShard, GuildIntegration> OnGuildIntegrationsUpdate;
|
|
public event Action<GatewayShard, GuildMember> OnGuildMemberAdd;
|
|
public event Action<GatewayShard, GuildMember> OnGuildMemberUpdate;
|
|
public event Action<GatewayShard, GuildMember> OnGuildMemberRemove;
|
|
public event Action<GatewayShard, GuildMembersChunk> OnGuildMembersChunk;
|
|
public event Action<GatewayShard, GuildRole> OnGuildRoleCreate;
|
|
public event Action<GatewayShard, GuildRole> OnGuildRoleUpdate;
|
|
public event Action<GatewayShard, GuildRole> OnGuildRoleDelete;
|
|
public event Action<GatewayShard, Guild> OnGuildUpdate;
|
|
|
|
public event Action<GatewayShard> OnMessageAck;
|
|
public event Action<GatewayShard, Message> OnMessageCreate;
|
|
public event Action<GatewayShard, Message> OnMessageDelete;
|
|
public event Action<GatewayShard> OnMessageDeleteBulk;
|
|
public event Action<GatewayShard> OnMessageReactionAdd;
|
|
public event Action<GatewayShard> OnMessageReactionRemove;
|
|
public event Action<GatewayShard> OnMessageReactionsRemoveAll;
|
|
public event Action<GatewayShard, Message> OnMessageUpdate;
|
|
|
|
public event Action<GatewayShard, TypingStart> OnTypingStart;
|
|
public event Action<GatewayShard, Presence> OnPresenceUpdate;
|
|
public event Action<GatewayShard> OnPresencesReplace;
|
|
|
|
public event Action<GatewayShard> OnFriendSuggestionDelete;
|
|
public event Action<GatewayShard> OnRelationshipAdd;
|
|
public event Action<GatewayShard> OnRelationshipRemove;
|
|
|
|
public event Action<GatewayShard, GatewayReady> OnReady;
|
|
public event Action<GatewayShard> OnResumed;
|
|
|
|
public event Action<GatewayShard, User> OnUserUpdate;
|
|
public event Action<GatewayShard> OnUserNoteUpdate;
|
|
public event Action<GatewayShard> OnUserSettingsUpdate;
|
|
|
|
public event Action<GatewayShard> OnSocketOpen;
|
|
public event Action<GatewayShard, bool, ushort, string> OnSocketClose;
|
|
public event Action<GatewayShard, Exception> OnSocketError;
|
|
public event Action<GatewayShard, string> OnSocketMessage;
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
|
|
private bool IsDisposed = false;
|
|
|
|
/// <summary>
|
|
/// Releases all unmanaged resources used by this object
|
|
/// </summary>
|
|
private void Dispose(bool disposing)
|
|
{
|
|
if (IsDisposed)
|
|
return;
|
|
|
|
IsDisposed = true;
|
|
Disconnect();
|
|
|
|
if (disposing)
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
~GatewayShardClient()
|
|
=> Dispose(false);
|
|
|
|
public void Dispose()
|
|
=> Dispose(true);
|
|
|
|
#endregion
|
|
}
|
|
}
|