示例#1
0
        public void SendCommand(CommandBase command, ResponseBase response)
        {
            Exception exception = null;

            _networkingTaskQueue.DoWorkInQueue(() =>
            {
                try
                {
                    // Send the command
                    _socket.Send(command.GetByteArray());


                    var responseBytes = new byte[response.GetResponseLength()];

                    // Receive the response
                    Receive(_socket, responseBytes, responseBytes.Length);


                    uint responseId = BitConverter.ToUInt32(responseBytes, 0);

                    if (responseId != response.ResponseId)
                    {
                        // Set an exception when the received response id is not the same as the expected response id
                        // The exception is thrown at the end
                        exception =
                            new InvalidOperationException(
                                $"The response id ({responseId}) is not the same response id ({response.ResponseId}) as expected");
                    }
                    else
                    {
                        // Set the values of the given response object
                        response.FromByteArray(responseBytes);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    exception = ex;
                }
            }, true);


            if (exception != null)
            {
                if (!(exception is InvalidOperationException))
                {
                    Connected = false;
                }

                throw exception;
            }
        }