class Minecraft::RToolkit::Connection

Public Class Methods

new(options={}) click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 4
def initialize(options={})
    @user = options[:user]
    @password = options[:password]
    @host = options[:host]
    @port = options[:port]
    @connection = nil

    #define destructor
    ObjectSpace.define_finalizer(self, method(:close))
end

Public Instance Methods

close() click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 54
def close
    unless @connection.nil?
        @connection.close
    end
end
make_request(action) click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 15
def make_request(action)
    "#{action.upcase}:#{@user}:#{@password}"
end
open() click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 19
def open
    if @connection.nil?
        @connection = UDPSocket.new()
        @connection.connect(@host, @port)
    end
end
recieve() click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 38
def recieve
    unless @connection.nil?
        begin
            response = @connection.recvfrom(32)
            case response[0]
            when 'response:success' then true
            when /^response:.*/ then false
            else
                response [0]
            end
        rescue
            raise SocketError
        end
    end
end
send(action) click to toggle source
# File lib/minecraft_rtoolkit/connection.rb, line 26
def send(action)
    open if @connection.nil?
    unless @connection.nil?
        begin
            @connection.send(make_request(action), 0)
            recieve
        rescue
            raise SocketError
        end
    end
end