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/GatewayShardClient.cs

300 lines
12 KiB
C#
Raw Normal View History

2017-05-14 12:02:51 +00:00
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 Dictionary<int, GatewayShard> shards = new Dictionary<int, GatewayShard>();
/// <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="c">Parent DiscordClient instance</param>
public GatewayShardClient(Discord c)
{
client = c;
}
/// <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);
shards.Add(id, shard);
return shard;
}
/// <summary>
/// Destroys a Gateway Shard using its id
/// </summary>
/// <param name="id">Id of the shard to destroy</param>
public void Destroy(int id) => Destroy(shards[id]);
/// <summary>
/// Destroys a Gateway Shard
/// </summary>
/// <param name="shard">Shard to destroy</param>
public void Destroy(GatewayShard shard)
{
shards.Remove(shard.Id);
// 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() => shards.Keys.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;
private void Dispose(bool disposing)
{
if (!IsDisposed)
{
IsDisposed = true;
Disconnect();
}
}
~GatewayShardClient()
{
Dispose(false);
}
/// <summary>
/// Releases all unmanaged resources used by this object
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
#endregion
}
}