示例#1
0
        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);
        }
示例#2
0
 public RPCCommand(int commandType)
 {
     _cmdHeader = new CommandHeader(commandType);
 }
示例#3
0
 public RPCCommand(CommandHeader cmdHeader)
 {
     _cmdHeader = cmdHeader;
 }
示例#4
0
        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);
            }
        }