Exemple #1
0
        /// <summary>
        /// Send the proper authentication packet and parse the response
        /// </summary>
        /// <param name="password">Current server password</param>
        /// <returns>True if the connection has been authenticated; False elsewhere</returns>
        /// <remarks>This method must be called prior to sending any other command</remarks>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="password"/> parameter is null or empty</exception>
        public async Task <bool> AuthenticateAsync(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("password parameter must be a non null non empty string");
            }

            var authPacket = new RconPacket(PacketType.Auth, password);
            var response   = await _socket.SendDataAndReadResponseAsync(authPacket.GetBytes());

            var responsePacket = RconPacket.FromBytes(response);

            return(responsePacket.Id != -1);
        }
Exemple #2
0
        /// <summary>
        /// Send a command encapsulated into an Rcon message packet and get the response
        /// </summary>
        /// <param name="command">Command to be executed</param>
        /// <returns>The response to this command</returns>
        /// <exception cref="ArgumentException">Is thrown if <paramref name="command"/> parameter is null or empty</exception>
        /// <exception cref="InvalidOperationException">Is thrown if the connection is not properly opened and authenticated</exception>
        public async Task <string> ExecuteCommandAsync(string command)
        {
            if (string.IsNullOrEmpty(command))
            {
                throw new ArgumentException("command parameter must be a non null non empty string");
            }

            if (!_socket.IsConnected)
            {
                throw new InvalidOperationException("You must authenticate the connection before sending any command to the server");
            }

            var commandPacket = new RconPacket(PacketType.ExecCommand, command);
            var response      = await _socket.SendDataAndReadResponseAsync(commandPacket.GetBytes());

            var responsePacket = RconPacket.FromBytes(response);

            return(responsePacket.Body);
        }