public void SendCommandWithAnswer(RPCCommand cmd, CommandReplyEventHandler OnCommandReply) { lock (_commandsToSend) { if (OnCommandReply != null) { //_eventsByCmdId.Add(cmd.Uid, OnCommandReply); } _commandsToSend.Add(cmd); } }
public CommandReplyEventArgs(RPCCommand cmd) : base() { _cmd = cmd; }
public void SendCommand(RPCCommand cmd) { SendCommandWithAnswer(cmd, null); }
public void RunClient(object sender) { bool shouldRun = true; List <RPCCommand> cmds = new List <RPCCommand>(); while (shouldRun && _clientSocket.Connected && SocketUtils.IsSocketConnectedToHost(_clientSocket)) { const int MaxPerFrame = 16; int cmdsReceived = 0; bool hasNoMoreData = false; // Get any incoming command if any while (shouldRun && !hasNoMoreData && cmdsReceived < MaxPerFrame) { try { CommandHeader cmdHeader = new CommandHeader(_clientSocket); RPCCommand cmd = CommandManager.Instance.CreateCommandFromCommandHeader(cmdHeader); cmds.Add(cmd); cmd.DeserializeCommand(); RPCCommand cmdReply = cmd.DoCommand(); if (cmdReply != null) { lock (_commandsToSend) { _commandsToSend.Add(cmdReply); } } ++cmdsReceived; } catch (SocketException e) { if (e.SocketErrorCode == SocketError.WouldBlock) { hasNoMoreData = true; } else { shouldRun = false; } } } // Send all buffered commands if any if (_commandsToSend.Count > 0) { lock (_commandsToSend) { foreach (RPCCommand cmd in _commandsToSend) { cmd.SerializeCommand(); cmd.CommandHeader.WriteCommand(_clientSocket); } _commandsToSend.Clear(); } } Thread.Sleep(20); } _clientSocket.Close(1); if (Disconnected != null) { Disconnected(this, EventArgs.Empty); } }