class RCON::Minecraft

Attributes

authed[R]

Authentication Status

host[R]

Host of connection

packet[R]

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

port[R]

Port of connection

return_packets[RW]

return full packet, or just data?

socket[R]

TCPSocket object

Public Class Methods

new(host = 'localhost', port = 25575) click to toggle source

Given a host and a port (dotted-quad or hostname OK), creates a Query::Minecraft object. Note that this will still require an authentication packet (see the auth() method) before commands can be sent.

# File lib/rcon/rcon.rb, line 297
 def initialize(host = 'localhost', port = 25575)
   @host = host
   @port = port
   @socket = nil
   @packet = nil
   @authed = false
   @return_packets = false
end

Public Instance Methods

auth(password) click to toggle source

Requests authentication from the RCon server, given a password. Is only expected to be used once.

# File lib/rcon/rcon.rb, line 352
def auth(password)
  establish_connection

  @packet = Packet::Source.new
  @packet.auth(password)

  @socket.print @packet.to_s
  rpacket = nil
  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
Also aliased as: authenticate
authenticate(password)
Alias for: auth
command(command) click to toggle source

Sends a RCon command to the server. May be used multiple times after an authentication is successful.

# File lib/rcon/rcon.rb, line 323
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) click to toggle source

See Query#cvar.

Calls superclass method RCON::Query#cvar
# File lib/rcon/rcon.rb, line 310
def cvar(cvar_name)
  return_packets = @return_packets
  @return_packets = false
  response = super
  @return_packets = return_packets
  return response
end
disconnect() click to toggle source

Disconnects from the Minecraft server.

# File lib/rcon/rcon.rb, line 380
def disconnect
  if @socket
    @socket.close
    @socket = nil
    @authed = false
  end
end

Protected Instance Methods

build_response_packet() click to toggle source

Builds a Packet::Source packet based on the response given by the server.

# File lib/rcon/rcon.rb, line 394
def build_response_packet
  rpacket = Packet::Source.new
  total_size = 0
  request_id = 0
  type = 0
  response = ""
  message = ""
  message2 = ""

  tmp = @socket.recv(4)
  if tmp.nil?
    return nil
  end
  size = tmp.unpack("V1")
  tmp = @socket.recv(size[0])
  request_id, type, message, message2 = tmp.unpack("V1V1a*a*")
  total_size = size[0]

  rpacket.packet_size = total_size
  rpacket.request_id = request_id
  rpacket.command_type = type

  # strip nulls (this is actually the end of string1 and string2)
  message.sub! /\x00\x00$/, ""
  message2.sub! /\x00\x00$/, ""
  rpacket.string1 = message
  rpacket.string2 = message2
  return rpacket
end
establish_connection() click to toggle source

establishes a connection to the server.

# File lib/rcon/rcon.rb, line 425
def establish_connection
  if @socket.nil?
    @socket = TCPSocket.new(@host, @port)
  end
end