コード例 #1
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
        private static TcpChatMessageStruct GetMessageRequestMessage(TcpChatMessageStruct message, int position, byte[] buffer)
        {
            UInt32 requestId = (UInt32)((((buffer[position] << 8) | buffer[position + 1] << 8) | buffer[position + 2] << 8) | buffer[position + 3]);

            message.requestIds = new UInt32[] { requestId };
            return(message);
        }
コード例 #2
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
 /// <summary>
 /// Gets the username and password
 /// </summary>
 /// <param name="message"></param>
 /// <param name="position">Position of username length</param>
 private static TcpChatMessageStruct GetUsernamePassword(TcpChatMessageStruct message, int position, byte[] buffer)
 {
     message.usernameLength = (ushort)((buffer[position] << 8) | buffer[position + 1]);
     message.passwordLength = (ushort)((buffer[position + 2 + message.usernameLength] << 8) | buffer[position + 3 + message.usernameLength]);
     message.username       = (Encoding.UTF8.GetString(buffer.Skip(position + 2).Take(message.usernameLength).ToArray()));
     message.password       = (Encoding.UTF8.GetString(buffer.Skip(position + 4 + message.usernameLength).Take(message.passwordLength).ToArray()));
     return(message);
 }
コード例 #3
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
        private static TcpChatMessageStruct GetMessageRequestMessageList(TcpChatMessageStruct message, int position, byte[] buffer)
        {
            UInt32 numOfRequests = (UInt32)((((buffer[position] << 8) | buffer[position + 1] << 8) | buffer[position + 2] << 8) | buffer[position + 3]);

            position += 4;
            List <UInt32> requests = new List <uint>();

            for (int i = 0; i < numOfRequests; i++)
            {
                requests.Add((UInt32)((((buffer[position] << 8) | buffer[position + 1] << 8) | buffer[position + 2] << 8) | buffer[position + 3]));
                position += 4;
            }
            message.requestIds = requests.ToArray();
            return(message);
        }
コード例 #4
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
 public TcpChatMessage(TcpChatMessageStruct message)
 {
     MessageType            = message.MessageType;
     Code                   = message.code;
     Content                = message.content;
     ContentLength          = message.contentLength;
     Token                  = message.token;
     Username               = message.username;
     UsernameLength         = message.usernameLength;
     Password               = message.password;
     PasswordLength         = message.passwordLength;
     ResponseMessagesLength = message.responseMessagesLength;
     ResponseMessages       = message.responseMessages;
     RequestIds             = message.requestIds;
 }
コード例 #5
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
        private static TcpChatMessageStruct GetPackagedMessages(TcpChatMessageStruct message, int position, byte[] buffer)
        {
            ushort numOfMessages = (ushort)((buffer[position] << 8) | buffer[position + 1]);

            message.responseMessagesLength = numOfMessages;
            position += 2;
            List <Message> messages = new List <Message>();

            for (int i = 0; i < numOfMessages; i++)
            {
                ushort messageLength = (ushort)((buffer[position] << 8) | buffer[position + 1]);
                position += 2;
                messages.Add(GetPackedMessage(position, buffer));
                position += messageLength;
            }
            message.responseMessages = messages.ToArray();
            return(message);
        }
コード例 #6
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
        public static TcpChatMessage Parse(byte[] buffer)
        {
            //The message goes a little like this
            //First octet = message Type
            //[2 to 17] 16 octets = token       OR       [2 to 3] 2 username Length
            //token                                   // [4 to 4 + username Length] username
            //2 octets content length  OR message id  // [4 + username Length + 1 to 4 + username Length + 2] 2 octets password Length
            //content                    //           // password
            //
            //

            /*
             * A SendMessage request is like this:
             * [0](1 octet)SendMessage Declaration
             * [1 - 16](16 octets)Token
             * [17 - 18](2 octets)Content Length
             * [19 - END](Content length)Content
             *
             * A Login request is like this:
             * [0](1 octet)Login Declaration
             * [1 - 2](2 octets)Username Length
             * [3 - ?](Username Length)Username
             * [? - ?](2 octets)Password Length
             * [? - END](Password length)Password
             *
             * A MessagePackage request is like this
             * [0](1 octet)MessagePackage request declaration
             * (4 octets)Requested message id
             *
             * A MessagePackage response is like this
             * [0](1 octet)MessagePackage response declaration
             * [1 - 2](2 octets)Number of response messages (used for deserialization, useless in practice)
             * --Begin Message--
             * (2 octets)Message byte length (used for deserialization)
             * (4 octets)Message Id
             * (2 octets)Message author's username length
             * (username length)Message author's username
             * (2 octets)Message content's length
             * (content length)Message content
             * (8 octets)Message timestamp in ticks
             * -- End  Message--
             *
             * A MessagePackageList request is like this
             * [0](1 octet)declaration
             * [1 - 4](4 octets)Number of requests
             * *For each request*
             * (4 octets)Message Id
             */



            TcpChatMessageStruct message = new TcpChatMessageStruct();

            message.MessageType = (MessageTypes)buffer[0];
            switch (message.MessageType)
            {
            //May be unknown when an empty packet attempts to be parsed, aka the default value for the enumerable
            case MessageTypes.Unknown:
                message.code = TcpChatMessageStruct.Code.ERROR;
                break;

            case MessageTypes.SendMesssage:
                message = GetToken(message, 1, buffer);     // 1 is the first char of token
                message = GetContent(message, 17, buffer);
                break;

            case MessageTypes.Login:
            case MessageTypes.RegisterSelf:
                message = GetUsernamePassword(message, 1, buffer);
                break;

            case MessageTypes.ResponseMessagePacket:
                message = GetPackagedMessages(message, 1, buffer);
                break;

            case MessageTypes.RequestMessage:
                message = GetMessageRequestMessage(message, 1, buffer);
                break;

            case MessageTypes.RequestMessageList:
                message = GetMessageRequestMessageList(message, 1, buffer);
                break;

            case MessageTypes.ResponseToken:
                message = GetToken(message, 1, buffer);
                break;
            }
            return(new TcpChatMessage(message));
        }
コード例 #7
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
 /// <summary>
 ///
 /// </summary>
 /// <param name="message"></param>
 /// <param name="position">Position of first char of token</param>
 private static TcpChatMessageStruct GetToken(TcpChatMessageStruct message, int position, byte[] buffer)
 {
     message.token = (Encoding.ASCII.GetString(buffer.Skip(position).Take(16).ToArray()));
     return(message);
 }
コード例 #8
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
 /// <summary>
 ///
 /// </summary>
 /// <param name="message"></param>
 /// <param name="position">Position of content length</param>
 private static TcpChatMessageStruct GetContent(TcpChatMessageStruct message, int position, byte[] buffer)
 {
     message.contentLength = (ushort)((buffer[position] << 8) | buffer[position + 1]);
     message.content       = (Encoding.UTF8.GetString(buffer.Skip(position + 2).Take(message.contentLength).ToArray()));
     return(message);
 }
コード例 #9
0
ファイル: TcpChatMessage.cs プロジェクト: MrDoritos/TCPChat
 /// <summary>
 ///
 /// </summary>
 /// <param name="message"></param>
 /// <param name="position">Start of content</param>
 /// <param name="length">Amount of bytes in the content</param>
 private static TcpChatMessageStruct GetContent(TcpChatMessageStruct message, int position, int length, byte[] buffer)
 {
     message.content = (Encoding.UTF8.GetString(buffer.Skip(position).Take(length).ToArray()));
     return(message);
 }