public RPCCommand CreateCommandFromCommandHeader(CommandHeader cmdHeader) { if (_commandsByType.ContainsKey(cmdHeader.CommandType)) { Type cmdInstanceType = _commandsByType[cmdHeader.CommandType]; return(Activator.CreateInstance(cmdInstanceType, new object[] { cmdHeader }) as RPCCommand); } return(null); }
public RPCCommand(int commandType) { _cmdHeader = new CommandHeader(commandType); }
public RPCCommand(CommandHeader cmdHeader) { _cmdHeader = cmdHeader; }
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); } }