Exemple #1
0
 protected internal virtual void OnNewCommand(CommandOperationEventArgs e)
 {
     if (NewCommand != null)
     {
         NewCommand(this, e);
     }
 }
Exemple #2
0
 /// <summary>
 /// Handles server received a new command
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void server_NewCommand(object sender, CommandOperationEventArgs e)
 {
     SetText("Command Received: " + e.Command.Name);
     if (e.Command.Name == "TEST")
     {
         // custom operation by handling New Command event
         e.Operation = new Cmd_Test();
         e.Handled   = true;
     }
 }
        /// <summary>
        /// Occurs when a new command received
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnNewCommand(CommandOperationEventArgs e)
        {
            Server.OnNewCommand(e);

            if (!e.Handled)
            {
                e.Operation = Server.GetOperation(e.Command);
            }

            if (e.Operation != null)
            {
                e.Operation.Command = e.Command;
                Operations.Add(e.Operation);
                e.Operation.StartOperation();
            }
        }
 /// <summary>
 /// Operation canceled
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OperationCanceled(object sender, CommandOperationEventArgs e)
 {
     OnCommandCanceled(e.Operation);
 }
 /// <summary>
 /// Operation finished
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void OperationFinished(object sender, CommandOperationEventArgs e)
 {
     OnCommandFinished(e.Operation);
 }
        /// <summary>
        /// Process client operations
        /// </summary>
        private void ClientProcess()
        {
            DateTime lastCommandDate = DateTime.Now;

            _SocketReader.Socket    = Socket;
            _SocketReader.SleepTime = 5;             // 5 ms
            _SocketReader.Timeout   = Server.CommandTimeout;

            _BadCommandCount = 0;

            try {
                // send welcome message to the client
                var response = new NetResponse(true, "Server is ready at " + DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                response.CommandId = -1;
                response.Parameters.Add(new ParameterInt32("hash", Guid.GetHashCode()));

                Send(response);

                Server.OnClientConnected(this);

                double time = 0;
                response = null;
                bool       badcommand = false;
                NetCommand command    = null;

                while (_IsOnProcess)
                {
                    response   = null;
                    badcommand = false;
                    command    = null;

                    if (Socket.Available > 0)
                    {
                        _SocketReader.Clear();
                        _SocketReader.Read();

                        lastCommandDate = DateTime.Now;

                        if (_SocketReader.LastStatus == SocketReader.ReadStatus.Success)
                        {
                            try {
                                if (_SocketReader.Type == SocketReader.ReadType.Command)
                                {
                                    command    = NetCommand.Parse(_SocketReader.Data);
                                    command.Id = _SocketReader.CommandId;

                                    CommandOperationEventArgs e = new CommandOperationEventArgs(command);
                                    OnNewCommand(e);

                                    if (!e.Handled)
                                    {
                                        badcommand = true;
                                    }
                                }
                                else
                                {
                                    command = _SendedCommands[_SocketReader.CommandId]
                                              as NetCommand;
                                    if (command != null)
                                    {
                                        response           = NetResponse.Parse(_SocketReader.Data);
                                        response.CommandId = _SocketReader.CommandId;

                                        command.Response = response;
                                        command.Status   = CommandStatus.Executed;
                                        _SendedCommands.Remove(command.Id);
                                    }
                                }
                            }
                            catch (Exception exception) {
                                Server.OnSystemError(exception);
                                badcommand = true;
                            }

                            if (badcommand)
                            {
                                _BadCommandCount++;
                                if (_SocketReader.Type == SocketReader.ReadType.Command)
                                {
                                    if (command != null)
                                    {
                                        response         = new NetResponse(false, "Bad command");
                                        command.Response = response;
                                        Send(response);
                                        command.Status = CommandStatus.Executed;
                                    }
                                }
                                else
                                {
                                    command = _SendedCommands[response.CommandId]
                                              as NetCommand;
                                    if (command != null)
                                    {
                                        command.Response = new NetResponse(false, "Bad command");
                                        command.Status   = CommandStatus.Executed;
                                        _SendedCommands.Remove(command.Id);
                                    }
                                }

                                if (_BadCommandCount > Server.BadCommandCount)
                                {
                                    response = new NetResponse(false, "Because of too much bad commands your connection will be terminated");
                                    Send(response);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            switch (_SocketReader.LastStatus)
                            {
                            case SocketReader.ReadStatus.Exception:
                                response = new NetResponse(false, "Socket reading exception");
                                Send(response);
                                break;

                            case SocketReader.ReadStatus.Timeout:
                                response = new NetResponse(false, "Timeout");
                                Send(response);
                                break;

                            default:
                                response = new NetResponse(false, "Unknow socket reading exception");
                                Send(response);
                                break;
                            }
                        }
                    }
                    else
                    {
                        NetCommand active = null;
                        for (int k = 0; k < Commands.Count; k++)
                        {
                            NetCommand cd = Commands[k, true];
                            if (cd.Status == CommandStatus.Waiting)
                            {
                                active = cd;
                                break;
                            }
                        }

                        if (active != null)
                        {
                            active.Status = CommandStatus.SendingToTarget;
                            Send(active);
                            _SendedCommands.Add(active.Id, active);
                            active.Status = CommandStatus.WaitingForResponse;
                        }
                        else
                        {
                            time = DateTime.Now.Subtract(lastCommandDate).TotalMilliseconds;
                            if (time > Server.ConnectionTimeout)
                            {
                                response = new NetResponse(false, "Connection is timeout");
                                Send(response);
                                Disconnect();
                                break;
                            }

                            Thread.Sleep(10);
                        }
                    }
                }
            }
            catch (SocketException ex)
            {
                if (ex.NativeErrorCode == 0x2745)
                {
                    // connection close by remote host is not an exception (client might be closed)
                }
                else if (ex.NativeErrorCode != 0x2714)                 // WSACancelBlockCall (We might close)
                {
                    Server.OnSystemError(new Exception(string.Format("Socket exception in Client {0}", Guid.ToString("N")), ex));
                }
            }
            catch (Exception ex) {
                Server.OnSystemError(new Exception(string.Format("Exception in Client {0}", Guid.ToString("N")), ex));
            }
            finally{
                Disconnect();
            }
        }