Пример #1
0
        public void SendCommandWithAnswer(RPCCommand cmd, CommandReplyEventHandler OnCommandReply)
        {
            lock (_commandsToSend)
            {
                if (OnCommandReply != null)
                {
                    //_eventsByCmdId.Add(cmd.Uid, OnCommandReply);
                }

                _commandsToSend.Add(cmd);
            }
        }
Пример #2
0
 public CommandReplyEventArgs(RPCCommand cmd)
     : base()
 {
     _cmd = cmd;
 }
Пример #3
0
 public void SendCommand(RPCCommand cmd)
 {
     SendCommandWithAnswer(cmd, null);
 }
Пример #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);
            }
        }