class RCON::Client

Overview

This class represents a RCON connection.

RCON::Client.open(address, port, password) do |client|
  client.command "say Hello from RCON =)"
end

Use as a Server

While this is primarily a client implementation it can also be used as for a server because both ends work in the same manner.

def handle_connection(socket)do
  rcon = RCON::Client.new(socket)
  packet = rcon.read_
  puts "Received packet #{packet}"
  # Probably need some authentication logic here
  socket.close
end

TCPServer.open(27015) do |server|
  while socket = server.accept?
    spawn handle_connection(socket)
  end
end

Defined in:

client.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(socket : IO, sync_close : Bool = true) #

Creates a new client instance wrapping socket.

The socket should usually be a TCPSocket, but it can really be any IO.

Calling .open is recommended for most use cases. When using this constructor, authentication needs to be handled explicitly (see #authenticate).


[View source]

Class Method Detail

def self.open(address : String, port : Int32, password : String | Nil, & : self -> ) #

Opens a new connection to the server at address:port and authenticates with password. Yields the client instance to the block and ensures the connection is closed after returning.


[View source]
def self.open(uri : URI, & : self -> ) #

Opens a new connection to the server at uri and authenticates.

URI format: rcon://:password@host:port

Yields the client instance to the block and ensures the connection is closed after returning.


[View source]

Instance Method Detail

def authenticate(password) : Bool #

Sends an auth command to authenticate with password.

Return true if authenticated successfully and false otherwise. Raises AuthenticationError if there was an error in the authentication process.


[View source]
def close #

Sends #close command and closes the connection.


[View source]
def closed? : Bool #

Returns true when this client has been closed.


[View source]
def command(command : String) : String | Nil #

Sends command and returns the server's response.


[View source]
def read : Packet | Nil #

Reads a response from the server.

Returns nil if the connection is closed.


[View source]
def send(command : String | Bytes, cmd_type : Command | Int32 = Command::EXEC_COMMAND) : Int32 #

Sends command and returns the request id.


[View source]
def send(packet : Packet) #

Sends packet and returns the request id.


[View source]