Esempio n. 1
0
        /// <summary>
        /// Request the text to parse and check for commands that need to be executed.
        /// </summary>
        /// <param name="data">A string of data split by char(1), the first part being the command and the second part being the parameters.</param>
        /// <returns>True if parsed or false if not.</returns>
        public bool Parse(string data)
        {
            if (data.Length == 0 || string.IsNullOrEmpty(data))
            {
                return(false);
            }

            string cmd = data.Split(Convert.ToChar(1))[0];

            IRconCommand command = null;

            if (this._commands.TryGetValue(cmd.ToLower(), out command))
            {
                string   param      = null;
                string[] parameters = null;
                if (data.Split(Convert.ToChar(1))[1] != null)
                {
                    param      = data.Split(Convert.ToChar(1))[1];
                    parameters = param.ToString().Split(':');
                }

                return(command.TryExecute(parameters));
            }
            return(false);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public async Task <IRconResponse> ExecuteCommandAsync(IRconCommand command, CancellationToken cancellationToken)
        {
            if (!Connection.IsOpen)
            {
                Connection.Open();
            }

            var op = Connection.StreamOperator;

            var id = op.Write(command);

            return(await op.GetResponseAsync(id, cancellationToken));
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public IRconResponse ExecuteCommand(IRconCommand command, int timeout = 10000)
        {
            if (!Connection.IsOpen)
            {
                Connection.Open();
            }

            var op = Connection.StreamOperator;

            var id = op.Write(command);

            return(op.GetResponse(id, timeout));
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public int Write(IRconCommand command)
        {
            int id = 0;

            currentId++;

            id = currentId;

            _responses.TryAdd(id, new RconResponse(id));

            var packet     = RconPacket.From(id, command);
            var terminator = RconPacket.CommandTerminator(id);

            _writer.Write(packet.GetBytes());
            _writer.Flush();

            return(id);
        }
Esempio n. 5
0
 /// <summary>
 /// Registers a Rcon command.
 /// </summary>
 /// <param name="commandText">Text to type for this command.</param>
 /// <param name="command">The command to execute.</param>
 public void Register(string commandText, IRconCommand command)
 {
     this._commands.Add(commandText, command);
 }
Esempio n. 6
0
 /// <inheritdoc/>
 public async Task <IRconResponse> ExecuteCommandAsync(IRconCommand command)
 => await ExecuteCommandAsync(command, default);
Esempio n. 7
0
        public static RconPacket From(int commandId, IRconCommand rconCommand)
        {
            rconCommand.ThrowIfNull();

            return(new RconPacket(commandId, rconCommand.CommandType, rconCommand.Text));
        }