Beispiel #1
0
        /// <summary>
        /// Executes the command received from the client.
        /// </summary>
        private void ExecuteCommand()
        {
            try
            {
                // Receive the serialized user command.
                int receivedLength = clientSocket.Receive(receiveBuffer);

                // Try to view the bytes as a command string.
                clientCommand = Encoding.UTF8.GetString(receiveBuffer).Substring(0, receivedLength);
                if (clientCommand == "[<Close/>]")
                {
                    clientSocket.Close();
                    timer.Stop();
                    return;
                }

                // Put the bytes into a memory stream.
                MemoryStream ms = new MemoryStream(receiveBuffer, 0, receivedLength);

                // Execute the user command.
                // Note that for each UserCommand object, it needs a length of 512 B.
                for (int i = 1; ms.Position < ms.Length; i++)
                {
                    // Deserialize the next object in the memory stream.
                    UserCommandBase userCommand = (UserCommandBase)(formatter.Deserialize(ms));

                    // Execute the user command sent by the client.
                    userCommand.Execute();

                    // Reset the position of the memory stream.
                    ms.Position = i * 512;
                }
            }
            catch (SocketException)
            {
                SocketExceptionHandler();
            }
        }
        private void ExecuteCommand()
        {
            int receivedLength = clientSocket.Receive(receiveBuffer);

            clientCommand = Encoding.UTF8.GetString(receiveBuffer).Substring(0, receivedLength);
            if (clientCommand == "[<Close/>]")
            {
                return;
            }

            MemoryStream    ms        = new MemoryStream(receiveBuffer, 0, receivedLength);
            BinaryFormatter formatter = new BinaryFormatter();

            //ms.Position = 0;
            for (int i = 1; ms.Position < ms.Length; i++)
            {
                UserCommandBase userCommand = (UserCommandBase)(formatter.Deserialize(ms));
                userCommand.Execute();

                ms.Position = i * 512;

                this.Dispatcher.Invoke(() => txtCommandExecutor.AppendText(userCommand.ToString() + "\n"));
            }
        }