Class: RCon::Client
- Inherits:
-
Object
- Object
- RCon::Client
- Defined in:
- lib/rcon/client.rb,
lib/rcon/client/packet.rb,
lib/rcon/client/version.rb,
lib/rcon/client/connection.rb,
lib/rcon/client/packet_type.rb
Overview
TCP client for the Source RCON protocol.
Defined Under Namespace
Modules: PacketType Classes: AuthenticationError, Connection, ConnectionError, Error, Packet
Constant Summary collapse
- DEFAULT_PORT =
27015- VERSION =
"0.5.0"
Class Method Summary collapse
-
.open(host, port = DEFAULT_PORT, password:, sentinel_command: "") {|client| ... } ⇒ RCon::Client
Opens a connection, authenticates, and optionally yields the client.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the connection.
-
#connect ⇒ self
Establishes a TCP connection and authenticates.
- #connected? ⇒ Boolean
-
#execute(command) ⇒ String
Sends a command and returns the server response.
-
#initialize(host, port = DEFAULT_PORT, password:, sentinel_command: "") ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(host, port = DEFAULT_PORT, password:, sentinel_command: "") ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rcon/client.rb', line 41 def initialize(host, port=DEFAULT_PORT, password:, sentinel_command: "") @host = host @port = port @password = password @sentinel_command = sentinel_command @id_counter = Concurrent::AtomicFixnum.new(0) @connection = nil @reader_thread = nil @pending = Concurrent::Map.new @closing = false end |
Class Method Details
.open(host, port = DEFAULT_PORT, password:, sentinel_command: "") {|client| ... } ⇒ RCon::Client
Opens a connection, authenticates, and optionally yields the client.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rcon/client.rb', line 29 def self.open(host, port=DEFAULT_PORT, password:, sentinel_command: "") client = new(host, port, password:, sentinel_command:) client.connect return client unless block_given? begin yield client ensure client.close end end |
Instance Method Details
#close ⇒ Object
Closes the connection.
89 90 91 92 93 94 95 |
# File 'lib/rcon/client.rb', line 89 def close @closing = true @connection&.close @connection = nil @reader_thread&.join @reader_thread = nil end |
#connect ⇒ self
Establishes a TCP connection and authenticates.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rcon/client.rb', line 58 def connect @closing = false @connection = Connection.new(@host, @port).open authenticate @reader_thread = Thread.new { reader_loop } self rescue @connection&.close @connection = nil raise end |
#connected? ⇒ Boolean
98 |
# File 'lib/rcon/client.rb', line 98 def connected? = !@connection.nil? |
#execute(command) ⇒ String
Sends a command and returns the server response.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rcon/client.rb', line 75 def execute(command) cmd_id = next_id sentinel_id = next_id future = Concurrent::Promises.resolvable_future @pending[cmd_id] = [[], future] @pending[sentinel_id] = cmd_id @connection.send_packet(Packet.new(id: cmd_id, type: PacketType::EXECCOMMAND, body: command)) @connection.send_packet(Packet.new(id: sentinel_id, type: PacketType::EXECCOMMAND, body: @sentinel_command)) future.value! end |