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.



124
125
126
127
128
129
130
131
# File 'lib/rcon/rcon.rb', line 124

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



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

def authed
  @authed
end

#hostObject (readonly)

Host of connection



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

def host
  @host
end

#packetObject (readonly)

Packet::Source object that was sent as a result of the last query



105
106
107
# File 'lib/rcon/rcon.rb', line 105

def packet
  @packet
end

#portObject (readonly)

Port of connection



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

def port
  @port
end

#return_packetsObject

return full packet, or just data?



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

def return_packets
  @return_packets
end

#socketObject (readonly)

TCPSocket object



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

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.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rcon/rcon.rb', line 178

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.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rcon/rcon.rb', line 150

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.



137
138
139
140
141
142
143
# File 'lib/rcon/rcon.rb', line 137

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.



207
208
209
210
211
212
213
# File 'lib/rcon/rcon.rb', line 207

def disconnect
  if @socket
    @socket.close
    @socket = nil
    @authed = false
  end
end