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,
sig/rcon/client.rbs,
sig/rcon/client/packet.rbs,
sig/rcon/client/connection.rbs,
sig/rcon/client/packet_type.rbs
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, arg0| ... } ⇒ RCon::Client
Opens a connection, authenticates, and optionally yields the client.
Instance Method Summary collapse
- #authenticate ⇒ void
-
#close ⇒ void
Closes the connection.
-
#connect ⇒ self
Establishes a TCP connection and authenticates.
- #connected? ⇒ Boolean
- #dispatch(packet) ⇒ void
-
#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.
- #next_id ⇒ Integer
- #reader_loop ⇒ void
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, password:, sentinel_command:) ⇒ Client .open(host, port, password:, sentinel_command:) ⇒ void
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
#authenticate ⇒ void
This method returns an undefined value.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rcon/client.rb', line 102 private def authenticate auth_id = next_id @connection.send_packet(Packet.new(id: auth_id, type: PacketType::AUTH, body: @password)) # Standard RCON sends an empty RESPONSE_VALUE before AUTH_RESPONSE; some # implementations (e.g. Factorio) omit it and send AUTH_RESPONSE directly. loop do response = @connection.receive_packet next if response.type == PacketType::RESPONSE_VALUE raise AuthenticationError, "authentication failed" if response.id == -1 break end end |
#close ⇒ void
This method returns an undefined value.
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? |
#dispatch(packet) ⇒ void
This method returns an undefined value.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rcon/client.rb', line 131 private def dispatch(packet) return unless packet.type == PacketType::RESPONSE_VALUE entry = @pending[packet.id] return unless entry if entry.is_a?(Integer) cmd_id = entry parts, future = @pending.delete(cmd_id) @pending.delete(packet.id) future.fulfill(parts.join) else entry[0] << packet.body end end |
#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 |
#next_id ⇒ Integer
100 |
# File 'lib/rcon/client.rb', line 100 private def next_id = @id_counter.increment |
#reader_loop ⇒ void
This method returns an undefined value.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rcon/client.rb', line 116 private def reader_loop loop { dispatch(@connection.receive_packet) } rescue ConnectionError, IOError => e return if @closing error = ConnectionError.new(e.) @pending.each_pair do |_id, entry| next if entry.is_a?(Integer) _, future = entry future.reject(error) end @pending.clear end |