Class: RCON::Source
Instance Attribute Summary collapse
-
#authed ⇒ Object
readonly
Authentication Status.
-
#host ⇒ Object
readonly
Host of connection.
-
#packet ⇒ Object
readonly
Packet::Source object that was sent as a result of the last query.
-
#port ⇒ Object
readonly
Port of connection.
-
#return_packets ⇒ Object
return full packet, or just data?.
-
#socket ⇒ Object
readonly
TCPSocket object.
Instance Method Summary collapse
-
#auth(password) ⇒ Object
(also: #authenticate)
Requests authentication from the RCon server, given a password.
-
#command(command) ⇒ Object
Sends a RCon command to the server.
-
#cvar(cvar_name) ⇒ Object
See Query#cvar.
-
#disconnect ⇒ Object
Disconnects from the Source server.
-
#initialize(host = 'localhost', port = 25575) ⇒ Source
constructor
Given a host and a port (dotted-quad or hostname OK), creates a Query::Source object.
Constructor Details
#initialize(host = 'localhost', port = 25575) ⇒ Source
Given a host and a port (dotted-quad or hostname OK), creates a Query::Source object. Note that this will still require an authentication packet (see the auth() method) before commands can be sent.
123 124 125 126 127 128 129 130 |
# File 'lib/rcon/rcon.rb', line 123 def initialize(host = 'localhost', port = 25575) @host = host @port = port @socket = nil @packet = nil @authed = false @return_packets = false end |
Instance Attribute Details
#authed ⇒ Object (readonly)
Authentication Status
112 113 114 |
# File 'lib/rcon/rcon.rb', line 112 def authed @authed end |
#host ⇒ Object (readonly)
Host of connection
108 109 110 |
# File 'lib/rcon/rcon.rb', line 108 def host @host end |
#packet ⇒ Object (readonly)
Packet::Source object that was sent as a result of the last query
104 105 106 |
# File 'lib/rcon/rcon.rb', line 104 def packet @packet end |
#port ⇒ Object (readonly)
Port of connection
110 111 112 |
# File 'lib/rcon/rcon.rb', line 110 def port @port end |
#return_packets ⇒ Object
return full packet, or just data?
114 115 116 |
# File 'lib/rcon/rcon.rb', line 114 def return_packets @return_packets end |
#socket ⇒ Object (readonly)
TCPSocket object
106 107 108 |
# File 'lib/rcon/rcon.rb', line 106 def socket @socket end |
Instance Method Details
#auth(password) ⇒ Object Also known as: authenticate
Requests authentication from the RCon server, given a password. Is only expected to be used once.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rcon/rcon.rb', line 177 def auth(password) establish_connection @packet = Packet::Source.new @packet.auth(password) @socket.print @packet.to_s # on auth, one junk packet is sent rpacket = nil 2.times { rpacket = build_response_packet } if rpacket.command_type != Packet::Source::RESPONSE_AUTH raise NetworkException.new("error authenticating: #{rpacket.command_type}") end @authed = true if @return_packets return rpacket else return true end end |
#command(command) ⇒ Object
Sends a RCon command to the server. May be used multiple times after an authentication is successful.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rcon/rcon.rb', line 149 def command(command) if ! @authed raise NetworkException.new("You must authenticate the connection successfully before sending commands.") end @packet = Packet::Source.new @packet.command(command) @socket.print @packet.to_s rpacket = build_response_packet if rpacket.command_type != Packet::Source::RESPONSE_NORM raise NetworkException.new("error sending command: #{rpacket.command_type}") end if @return_packets return rpacket else return rpacket.string1 end end |
#cvar(cvar_name) ⇒ Object
See Query#cvar.
136 137 138 139 140 141 142 |
# File 'lib/rcon/rcon.rb', line 136 def cvar(cvar_name) return_packets = @return_packets @return_packets = false response = super @return_packets = return_packets return response end |
#disconnect ⇒ Object
Disconnects from the Source server.
206 207 208 209 210 211 212 |
# File 'lib/rcon/rcon.rb', line 206 def disconnect if @socket @socket.close @socket = nil @authed = false end end |