コード例 #1
0
        //Checks if the message/file is encrypted and sets the header.
        private void CheckIfEncryptedAndSetHeader()
        {
            byte[] headerBytes = new byte[State.HeaderSize];
            Array.Copy(State.Buffer, 8, headerBytes, 0, State.HeaderSize);


            //Copy the first 10 bytes of the header to a new byte array.
            //byte[] prefixBytes = new byte[4];
            //Array.Copy(headerBytes, 0, prefixBytes, 0, 4);

            if (headerBytes.Length > 10)
            {
                //Copy the first 10 bytes of the header to a new byte array.
                byte[] encryptedBytes = new byte[10];
                Array.Copy(headerBytes, 0, encryptedBytes, 0, 10);

                //The first 10 bytes of the header are unecrypted in a encrypted message and read "ENCRYPTED_".
                if (Encoding.UTF8.GetString(encryptedBytes) == "ENCRYPTED_")
                {
                    //Get the header without the "ENCRYPTED_" String, then set the state to Encrypted.
                    byte[] newHeader = new byte[headerBytes.Length - 10];
                    Array.Copy(headerBytes, 10, newHeader, 0, newHeader.Length);
                    State.Encrypted = true;
                    State.Header    = Encrypter.DecryptStringFromBytes(newHeader);
                }
            }


            //If the message is not encrypted, convert the bytes to string
            if (State.Header == string.Empty)
            {
                State.Header = Encoding.UTF8.GetString(headerBytes);
            }
        }
コード例 #2
0
        /// <summary>
        /// Invokes MessageReceived when a message has been fully received.
        /// </summary>
        /// <param name="receive"></param>
        public override void Receive(int receive)
        {
            //Decode the received message, decrypt when necessary.
            var text = string.Empty;

            byte[] receivedMessageBytes = State.ReceivedBytes;

            //Check if the bytes are encrypted or not.
            if (State.Encrypted)
            {
                text = Encrypter.DecryptStringFromBytes(receivedMessageBytes);
            }
            else
            {
                text = Encoding.UTF8.GetString(receivedMessageBytes);
            }

            if (Client == null)
            {
                if (State.Header == "MESSAGE")
                {
                    Server.InvokeMessageReceived(State.Id, text);
                }
                else if (State.Header.EndsWith("</h>") && State.Header.StartsWith("<h>"))
                {
                    Server.InvokeCustomHeaderReceived(State.Id, text, ReplaceHeader(State.Header));
                }
                else
                {
                    throw new Exception("Incorrect header received.");
                }


                return;
            }

            if (Server == null)
            {
                if (State.Header == "MESSAGE")
                {
                    Client.InvokeMessage(text);
                }
                else if (State.Header.EndsWith("</h>") && State.Header.StartsWith("<h>"))
                {
                    Client.InvokeCustomHeaderReceived(text, ReplaceHeader(State.Header));
                }
                else
                {
                    throw new Exception("Incorrect header received.");
                }


                return;
            }
        }
コード例 #3
0
        /// <summary>
        /// Invokes MessageReceived when a message has been fully received.
        /// </summary>
        /// <param name="receive"></param>
        public override void Receive(int receive)
        {
            //Decode the received message, decrypt when necessary.
            var text = string.Empty;

            byte[] receivedMessageBytes = State.ReceivedBytes;

            //Check if the bytes are encrypted or not.
            if (State.Encrypted)
            {
                text = Encrypter.DecryptStringFromBytes(receivedMessageBytes);
            }
            else
            {
                text = Encoding.UTF8.GetString(receivedMessageBytes);
            }

            if (Client == null)
            {
                if (State.Header == "MESSAGE")
                {
                    Server.InvokeMessageReceived(State.Id, text);
                }
                else if (State.Header == "COMMAND")
                {
                    Server.InvokeCommandReceived(State.Id, text);
                }
                else if (State.Header == "OBJECT")
                {
                    Server.InvokeObjectReceived(State.Id, text);
                }
                else
                {
                    throw new Exception("Incorrect header received.");
                }


                return;
            }

            if (Server == null)
            {
                if (State.Header == "MESSAGE")
                {
                    Client.InvokeMessage(text);
                }
                else if (State.Header == "COMMAND")
                {
                    Client.InvokeCommand(text);
                }
                else if (State.Header == "OBJECT")
                {
                    Client.InvokeObject(text);
                }
                else
                {
                    throw new Exception("Incorrect header received.");
                }


                return;
            }
        }