Пример #1
0
        private static void ParseMessageBytesAndRespondToClient(byte[] bytes, System.Net.Sockets.TcpClient tcpClient)
        {
            if (bytes.Length >= ClientCommandHeader.GetLength())
            {
                ClientCommandHeader commandHeader = ClientCommandHeader.FromBytes(bytes);
                IEnumerable <byte>  commandBytes  = bytes.Skip(ClientCommandHeader.GetLength());

                var commandType = commandHeader.Type;

                BaseCommand baseCommand = commandsFromBytes[commandType].Invoke(commandBytes.ToArray());

                switch (commandType)
                {
                    //case CommandType.SendChatMessage:
                    //    if (OnStringCommand != null)
                    //        OnStringCommand((StringCommand)baseCommand, tcpClient);
                    //    break;
                    //case CommandType.Single:
                    //    if (OnSingleCommand != null)
                    //        OnSingleCommand((SingleCommand)baseCommand, tcpClient);
                    //    break;
                    //case CommandType.File:
                    //    if (OnSingleCommand != null)
                    //        OnFileCommand((FileCommand)baseCommand, tcpClient);
                    //    break;
                    //case CommandType.SaveUser:
                    //    if (OnSingleCommand != null)
                    //        OnSaveUserCommand((SaveUserCommand)baseCommand, tcpClient);
                    //    break;
                }
            }
        }
Пример #2
0
        public void HandleServerResponse(object server)
        {
            /** Алгоритм:
             *  Определить тип сообщения
             *  в зависимости от этого,
             *      распарсить байты сообщения (подойдет Dictionary с методами FromStream.)
             *      предпринять действия
             *      отправить ответ.
             *
             *
             *
             *
             */


            var tcpClient = (System.Net.Sockets.TcpClient)server;

            tcpClient.ReceiveTimeout = 3;
            NetworkStream clientStream = tcpClient.GetStream();

            var ms           = new MemoryStream();
            var binaryWriter = new BinaryWriter(ms);

            var messageLengthBytes = new byte[4];
            var messageBytes       = new byte[tcpClient.ReceiveBufferSize];
            int readCount;
            int totalReadMessageBytes = 0;

            clientStream.Read(messageLengthBytes, 0, sizeof(int));
            int messageLength = CommandUtils.BytesToInt(messageLengthBytes);

            while ((readCount = clientStream.Read(messageBytes, 0, tcpClient.ReceiveBufferSize)) != 0)
            {
                binaryWriter.Write(messageBytes, 0, readCount);
                totalReadMessageBytes += readCount;
                if (totalReadMessageBytes >= messageLength)
                {
                    break;
                }
            }

            if (ms.Length > 0)
            {
                ParseResponseBytes(ms.ToArray());
            }

            void ParseResponseBytes(byte[] bytes)
            {
                if (bytes.Length >= ClientCommandHeader.GetLength())
                {
                }
            }
        }