Class: RCON::Source

Inherits:
Query
  • Object
show all
Defined in:
lib/rcon/rcon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#authedObject (readonly)

Authentication Status



112
113
114
# File 'lib/rcon/rcon.rb', line 112

def authed
  @authed
end

#hostObject (readonly)

Host of connection



108
109
110
# File 'lib/rcon/rcon.rb', line 108

def host
  @host
end

#packetObject (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

#portObject (readonly)

Port of connection



110
111
112
# File 'lib/rcon/rcon.rb', line 110

def port
  @port
end

#return_packetsObject

return full packet, or just data?



114
115
116
# File 'lib/rcon/rcon.rb', line 114

def return_packets
  @return_packets
end

#socketObject (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

#disconnectObject

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