public void Register <TRequest, TResponse>(NetworkCommandType source, NetworkCommandHandler <TRequest, TResponse> handler)
            where TResponse : NetworkCommand
            where TRequest : NetworkCommand
        {
            switch (source)
            {
            case NetworkCommandType.FromClient:
                _fromClient.Add(NetworkCommandBinding.From(source, handler));
                break;

            case NetworkCommandType.FromMaster:
                _fromMaster.Add(NetworkCommandBinding.From(source, handler));
                break;

            case NetworkCommandType.FromAny:
                _fromClient.Add(NetworkCommandBinding.From(NetworkCommandType.FromClient, handler));
                _fromMaster.Add(NetworkCommandBinding.From(NetworkCommandType.FromMaster, handler));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(source), source, null);
            }
        }
 private void CommandsChecker()
 {
     try
     {
         while (true)
         {
             try
             {
                 if (!ClientSocket.Connected)
                 {
                     Logger.NetworkCommandsLog($"User with IP: {ClientIP}:{ClientPort}, Login: {Account.Login}, Disconnected from RCON. Connection time: {(DateTime.Now - this.ConnectedAt)}. ClientID: {this.ClientID}");
                     return;
                 }
                 byte[] array = new byte[ClientSocket.ReceiveBufferSize];
                 ClientSocket.Receive(array);
                 if (LastCommandTime != null)
                 {
                     if (this.Account.PermissionLvl < 6)
                     {
                         if ((DateTime.Now - LastCommandTime).TotalSeconds < MainManager.CommandsCooldown) //too Fast
                         {
                             SendMessage($"You use commands too fast, use command every {MainManager.CommandsCooldown} second/s");
                             continue;
                         }
                     }
                 }
                 string        serial      = Encoding.UTF8.GetString(array);
                 string[]      serial_pkg  = serial.Split('|');
                 string        commandName = serial_pkg[1];
                 List <string> parameters  = serial_pkg.ToList <string>();
                 parameters.Remove(commandName);
                 string paramsAsString = String.Empty;
                 foreach (string parameter in parameters.ToList())
                 {
                     if (string.IsNullOrEmpty(parameter))
                     {
                         parameters.Remove(parameter);
                     }
                     else
                     {
                         paramsAsString += parameter;
                         paramsAsString += ",";
                     }
                 }
                 LastCommandTime = DateTime.Now;
                 if (parameters.Count < 1)
                 {
                     if (NetworkCommandHandler.ExecuteCommand(commandName, Account, this))
                     {
                         Logger.NetworkCommandsLog($"{Account.Login} used command \'{commandName}\', parameters: {paramsAsString}, ClientID: {ClientID}");
                     }
                 }
                 else
                 {
                     if (NetworkCommandHandler.ExecuteCommand(commandName, Account, this, parameters))
                     {
                         Logger.NetworkCommandsLog($"{Account.Login} used command \'{commandName}\', parameters: {paramsAsString}, ClientID: {ClientID}");
                     }
                 }
             }
             catch { LastCommandTime = DateTime.Now; Thread.Sleep(1000); continue; }
         }
     }
     catch
     {
         try
         {
             ClientSocket.Close();
             Logger.NetworkCommandsLog($"User with IP: {ClientIP}:{ClientPort}, Login: {Account.Login}, Disconnected from RCON. Connection time: {(DateTime.Now - this.ConnectedAt)}. ClientID: {this.ClientID}");
         }
         catch { MainManager.CommandsClients.Remove(this); Logger.NetworkCommandsLog($"User with IP: {ClientIP}:{ClientPort}, Login: {Account.Login}, Disconnected from RCON. Connection time: {(DateTime.Now - this.ConnectedAt)}. ClientID: {this.ClientID}"); return; }
         MainManager.CommandsClients.Remove(this);
         return;
     }
 }
Ejemplo n.º 3
0
        public static NetworkCommandBinding From <TRequest, TResponse>(NetworkCommandType source, NetworkCommandHandler <TRequest, TResponse> handler)
            where TRequest : NetworkCommand where TResponse : NetworkCommand
        {
            switch (source)
            {
            // Request that are from client are handled as requests on the master.
            case NetworkCommandType.FromClient:
                return(new NetworkCommandBinding()
                {
                    CommandInternalType = NetworkCommand.CommandTypeFor(typeof(TRequest)),
                    _deserializeRequest = JsonUtility.FromJson <TRequest>,
                    _handleRequest = async(net, clientId, command) => await handler.ProcessRequestOnMaster(net as INetworkMaster, command as TRequest, clientId),
                });

            // Request that are from master are handled as requests on the client.
            case NetworkCommandType.FromMaster:
                return(new NetworkCommandBinding()
                {
                    CommandInternalType = NetworkCommand.CommandTypeFor(typeof(TRequest)),
                    _deserializeRequest = JsonUtility.FromJson <TRequest>,
                    _handleRequest = async(net, clientId, command) => await handler.ProcessRequestOnClient(net as INetworkClient, command as TRequest)
                });

            default:
                throw new ArgumentOutOfRangeException(nameof(source), source, null);
            }
        }