private void HandleDispatchEvent(GatewayPayload evnt, TaskCompletionSource <bool> readySignal)
        {
            switch (evnt.DispatchType)
            {
            case GatewayDispatchType.Ready:
                var readyEvent = evnt.Data as ReadyEvent;
                SetSession(readyEvent.SessionId);
                readySignal.TrySetResult(true);
                Ready?.Invoke(readyEvent);
                break;

            case GatewayDispatchType.Resumed:
                readySignal.TrySetResult(true);
                Resumed?.Invoke();
                break;

            case GatewayDispatchType.GuildCreate: GuildCreate?.Invoke(evnt.Data as GatewayGuild); break;

            case GatewayDispatchType.GuildUpdate: GuildUpdate?.Invoke(evnt.Data as Guild); break;

            case GatewayDispatchType.GuildDelete: GuildDelete?.Invoke(evnt.Data as UnavailableGuild); break;

            case GatewayDispatchType.ChannelCreate: ChannelCreate?.Invoke(evnt.Data as Channel); break;

            case GatewayDispatchType.ChannelUpdate: ChannelUpdate?.Invoke(evnt.Data as Channel); break;

            case GatewayDispatchType.ChannelDelete: ChannelDelete?.Invoke(evnt.Data as Channel); break;

            case GatewayDispatchType.ChannelPinsUpdate: ChannelPinsUpdate?.Invoke(evnt.Data as ChannelPinsUpdateEvent); break;

            case GatewayDispatchType.GuildMemberAdd: GuildMemberAdd?.Invoke(evnt.Data as GuildMemberAddEvent); break;

            case GatewayDispatchType.GuildMemberUpdate: GuildMemberUpdate?.Invoke(evnt.Data as GuildMemberUpdateEvent); break;

            case GatewayDispatchType.GuildMemberRemove: GuildMemberRemove?.Invoke(evnt.Data as GuildMemberRemoveEvent); break;

            case GatewayDispatchType.GuildMembersChunk: GuildMembersChunk?.Invoke(evnt.Data as GuildMembersChunkEvent); break;

            case GatewayDispatchType.GuildRoleCreate: GuildRoleCreate?.Invoke(evnt.Data as GuildRoleCreateEvent); break;

            case GatewayDispatchType.GuildRoleUpdate: GuildRoleUpdate?.Invoke(evnt.Data as GuildRoleUpdateEvent); break;

            case GatewayDispatchType.GuildRoleDelete: GuildRoleDelete?.Invoke(evnt.Data as GuildRoleDeleteEvent); break;

            case GatewayDispatchType.GuildBanAdd: GuildBanAdd?.Invoke(evnt.Data as GuildBanAddEvent); break;

            case GatewayDispatchType.GuildBanRemove: GuildBanRemove?.Invoke(evnt.Data as GuildBanRemoveEvent); break;

            case GatewayDispatchType.GuildEmojisUpdate: GuildEmojisUpdate?.Invoke(evnt.Data as GuildEmojiUpdateEvent); break;

            case GatewayDispatchType.GuildIntegrationsUpdate: GuildIntegrationsUpdate?.Invoke(evnt.Data as GuildIntegrationsUpdateEvent); break;

            case GatewayDispatchType.MessageCreate: MessageCreate?.Invoke(evnt.Data as Message); break;

            case GatewayDispatchType.MessageUpdate: MessageUpdate?.Invoke(evnt.Data as Message); break;

            case GatewayDispatchType.MessageDelete: MessageDelete?.Invoke(evnt.Data as MessageDeleteEvent); break;

            case GatewayDispatchType.MessageDeleteBulk: MessageDeleteBulk?.Invoke(evnt.Data as MessageDeleteBulkEvent); break;

            case GatewayDispatchType.MessageReactionAdd: MessageReactionAdd?.Invoke(evnt.Data as MessageReactionAddEvent); break;

            case GatewayDispatchType.MessageReactionRemove: MessageReactionRemove?.Invoke(evnt.Data as MessageReactionRemoveEvent); break;

            case GatewayDispatchType.MessageReactionRemoveAll: MessageReactionRemoveAll?.Invoke(evnt.Data as MessageReactionRemoveAllEvent); break;

            case GatewayDispatchType.PresenceUpdate: PresenceUpdate?.Invoke(evnt.Data as Presence); break;

            case GatewayDispatchType.UserUpdate: UserUpdate?.Invoke(evnt.Data as User); break;

            case GatewayDispatchType.TypingStart: TypingStart?.Invoke(evnt.Data as TypingStartEvent); break;

            case GatewayDispatchType.VoiceStateUpdate: VoiceStateUpdate?.Invoke(evnt.Data as VoiceState); break;

            case GatewayDispatchType.VoiceServerUpdate: VoiceServerUpdate?.Invoke(evnt.Data as VoiceServerUpdateEvent); break;

            case GatewayDispatchType.WebhooksUpdate: WebhooksUpdate?.Invoke(evnt.Data as WebhooksUpdateEvent); break;
            }
        }
Beispiel #2
0
        void parseJson(object sender, string json)
        {
            Logger.LogMsg("Recieved: " + json);
            var packet = new Packet(json);

            if (packet.Id == PacketId.NewMessage)
            {
                var msg = new Message();
                msg.FromJson(packet.Information);

                Form.Invoke(new Action(() =>
                {
                    NewMessage?.Invoke(this, msg);
                }));
            }
            else if (packet.Id == PacketId.GiveIdentity)
            {
                var usr = new User();
                usr.FromJson(packet.Information);
                this.CurrentUser = usr;

                Form.Invoke(new Action(() =>
                {
                    IdentityKnown?.Invoke(this, usr);
                }));
            }
            else if (packet.Id == PacketId.UserUpdate)
            {
                var usr = new User();
                usr.FromJson(packet.Information);
                Form.Invoke(new Action(() =>
                {
                    UserUpdate?.Invoke(this, usr);
                }));
            }
            else if (packet.Id == PacketId.UserLeft)
            {
                var usr = new User();
                usr.FromJson(packet.Information);
                Form.Invoke(new Action(() =>
                {
                    UserDisconnected?.Invoke(this, usr);
                }));
            }
            else if (packet.Id == PacketId.SetMonitorState)
            {
                var state = packet.Information["state"].ToObject <bool>();
                Form.Invoke(new Action(() =>
                {
                    SetMonitorState?.Invoke(this, state);
                }));
            }
            else if (packet.Id == PacketId.Disconnect)
            {
                this.Close();
            }
            else if (packet.Id == PacketId.MessageDeleted)
            {
                var id = packet.Information["id"].ToObject <uint>();
                Form.Invoke(new Action(() =>
                {
                    MessageDeleted?.Invoke(this, id);
                }));
            }
            else if (packet.Id == PacketId.ImageNeedSlice)
            { // Server needs us to send the slice.
                var id        = packet.Information["id"].ToObject <uint>();
                var shiftedId = (uint)0;
                if (packet.Information.TryGetValue("originalId", out var val))
                {
                    shiftedId = id;
                    id        = val.ToObject <uint>();
                }
                var sliceNum = packet.Information["slice"].ToObject <int>();
                if (Common.Images.TryGetValue(id, out var image))
                {
                    if (shiftedId > 0)
                    { // move it into the new location, remove old one.
                        Common.Images[shiftedId] = image;
                    }
                    var slice = image.Slices[sliceNum];
                    var jobj  = new JObject(packet.Information);
                    jobj["done"] = (sliceNum == image.Slices.Count - 1);
                    jobj["data"] = slice;
                    var pongPacket = new Packet(PacketId.ImageSlice, jobj);
                    Send(pongPacket.ToString());
                    Form.Invoke(new Action(() =>
                    {
                        UploadStatus?.Invoke(this, new UploadStatusEvent(image, sliceNum));
                    }));
                }
            }
            else if (packet.Id == PacketId.ImageInitialInformation)
            {
                var image = new Classes.Image();
                image.FromJson(packet.Information);
                if (Common.TryGetImage(image.Id, out var existingImage))
                {
                    if (existingImage.Slices.Count == image.MaximumSlices)
                    { // We already have this image, so we dont need to download it
                        Form.Invoke(new Action(() =>
                        {
                            NewImageUploaded?.Invoke(this, existingImage);
                        }));
                        return;
                    }
                }
                Common.AddImage(image);
                var jobj = new JObject();
                jobj["id"]    = image.Id;
                jobj["slice"] = 0;
                var response = new Packet(PacketId.ImageNeedSlice, jobj);
                Send(response.ToString());
            }
            else if (packet.Id == PacketId.ImageSlice)
            {
                var id       = packet.Information["id"].ToObject <uint>();
                var sliceNum = packet.Information["slice"].ToObject <int>();
                var done     = packet.Information["done"].ToObject <bool>();
                var content  = packet.Information["data"].ToObject <string>();
                if (Common.TryGetImage(id, out var image))
                {
                    image.SetSlice(sliceNum, content);
                    if (done)
                    {
                        Form.Invoke(new Action(() =>
                        {
                            NewImageUploaded?.Invoke(this, image);
                        }));
                    }
                    else
                    {
                        var jobj = new JObject();
                        jobj["id"]    = image.Id;
                        jobj["slice"] = sliceNum + 1;
                        var pongPacket = new Packet(PacketId.ImageNeedSlice, jobj);
                        Send(pongPacket.ToString());
                        Form.Invoke(new Action(() =>
                        {
                            Form.pbProgressBar.Maximum = image.MaximumSlices;
                            Form.pbProgressBar.Value   = sliceNum;
                            Form.pbProgressBar.Tag     = $"Downloading image '{image.Name}'; {sliceNum}/{image.MaximumSlices} {((int)(sliceNum + 1 / image.MaximumSlices + 1))}";
                        }));
                    }
                }
            }
        }