Exemplo n.º 1
0
        void ProcessMesagePart(ref Header header, Connection conn, byte[] payload, int len)
        {
            if (!conn.OngoingMessages.TryGetValue(header.ID, out OngoingMessage mes))
            {
                mes = new OngoingMessage(header.Channel, header.TotalParts, header.Length, header.ID);
                conn.OngoingMessages.Add(mes.ID, mes);
            }

            if (!mes.partsCollected[header.Part])
            {
                MessagePart part     = MessagePart.Desserialize(payload, len);
                bool        finished = mes.MessageRecieved(part, MaxPacketSize);

                if (finished)
                {
                    ProcessData(conn, mes.Data, mes.Data.Length);
                    conn.OngoingMessages.Remove(mes.ID);
                }
            }
            MessagePartConfirmation mpc = new MessagePartConfirmation(header.ID, header.Part);

            Send(conn, ref mpc);
        }
Exemplo n.º 2
0
        void ProcessMessage(ref Header header, Connection conn, byte[] payload, int len)
        {
            switch (header.Channel)
            {
            case MessageTypes.Ping:
                // Ping Test
                InternalSend(conn.Conn, payload.SubArray(0, len));
                conn.FTT = Ping.Desserialize(payload, len).LastKnownFTT;
                break;

            case MessageTypes.MessagePartConfirmation:
                MessagePartConfirmation part = MessagePartConfirmation.Desserialize(payload, len);
                if (ActiveMessageParts.TryGetValue(conn, out Dictionary <ushort, ServerMessagePartsActive> activeMessages))
                {
                    if (activeMessages.TryGetValue(part.MessageID, out ServerMessagePartsActive activeMessage))
                    {
                        activeMessage.RecievedPart(part.PartNumber);
                    }
                }
                break;

            // ======== Encypted Message ===============
            case MessageTypes.RSARegistration:
                RSARegistration rsaMessage = RSARegistration.Desserialize(payload, len);
                RSARegistration rsaReply;
                switch (rsaMessage.step)
                {
                case RSARegistration.Step.InitalRequest:
                    rsaReply = new RSARegistration(RSARegistration.Step.ServerKey, RSAServerKeys.PublicBytes);
                    Send(conn, ref rsaReply);
                    break;

                case RSARegistration.Step.ClientResponse:
                    rsaMessage.Decrypt(RSAServerKeys);

                    byte[] clientRSAKey = rsaMessage.Data;
                    AESKeys[conn] = "Some Generated Key";         // TODO: Generate this AESKey

                    rsaReply = new RSARegistration(RSARegistration.Step.AESKey, System.Text.Encoding.Unicode.GetBytes(AESKeys[conn]));
                    rsaReply.Encrypt(new RSAEncryption.RSAKeys(_public: clientRSAKey));
                    Send(conn, ref rsaReply);
                    break;
                }
                break;
            // ========= End Encrpted Message ==================

            default:
                for (int i = 0; i < Handlers.Count; ++i)
                {
                    Handler h = Handlers[i];
                    if (h.channel == header.Channel)
                    {
                        if (header.IsAsync)
                        {
                            h.Call(conn, payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ServerSyncMessageCall(h, payload, len, conn));
                        }
                    }
                }
                break;
            }
        }
Exemplo n.º 3
0
        void ProcessMessage(ref Header header, byte[] payload, int len)
        {
            switch (header.Channel)
            {
            case MessageTypes.MessagePartConfirmation:
                MessagePartConfirmation part = MessagePartConfirmation.Desserialize(payload, len);

                if (ActiveMessageParts.TryGetValue(part.MessageID, out ClientMessagePartsActive activeMessage))
                {
                    activeMessage.RecievedPart(part.PartNumber);
                }
                break;

            // ======== Encypted Message ===============
            case MessageTypes.RSARegistration:
                RSARegistration rsaMessage = RSARegistration.Desserialize(payload, len);
                RSARegistration rsaReply;
                switch (rsaMessage.step)
                {
                case RSARegistration.Step.ServerKey:
                    ServerPublicRSAKey = new RSAEncryption.RSAKeys(_public: rsaMessage.Data);

                    rsaReply = new RSARegistration(RSARegistration.Step.ClientResponse, RSAKeys.PublicBytes);
                    rsaReply.Encrypt(ServerPublicRSAKey);
                    Send(ref rsaReply);
                    break;

                case RSARegistration.Step.AESKey:
                    rsaMessage.Decrypt(RSAKeys);

                    AESKey = System.Text.Encoding.Unicode.GetString(rsaMessage.Data);
                    break;
                }
                break;
            // ========= End Encrpted Message ==================

            default:
                for (int i = 0; i < Handlers.Count; ++i)
                {
                    Handler h = Handlers[i];
                    if (h.channel == header.Channel)
                    {
                        if (header.IsAsync)
                        {
                            h.Call(payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ClientSyncMessageCall(h, payload, len));
                        }
                    }
                }

                for (int i = DynamicMessagesInProgresses.Count - 1; i >= 0; --i)
                {
                    Handler dh = DynamicMessagesInProgresses[i];

                    if (dh.MessageID == header.ID)
                    {
                        if (header.IsAsync)
                        {
                            dh.Call(payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ClientSyncMessageCall(dh, payload, len));
                        }
                        DynamicMessagesInProgresses.RemoveAt(i);
                    }
                }
                break;
            }
        }