Class: Minecraft::RToolkit::Connection
- Inherits:
-
Object
- Object
- Minecraft::RToolkit::Connection
- Defined in:
- lib/minecraft_rtoolkit/connection.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #make_request(action) ⇒ Object
- #open ⇒ Object
- #recieve ⇒ Object
- #send(action) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/minecraft_rtoolkit/connection.rb', line 4 def initialize(={}) @user = [:user] @password = [:password] @host = [:host] @port = [:port] @connection = nil #define destructor ObjectSpace.define_finalizer(self, method(:close)) end |
Instance Method Details
#close ⇒ Object
54 55 56 57 58 |
# File 'lib/minecraft_rtoolkit/connection.rb', line 54 def close unless @connection.nil? @connection.close end end |
#make_request(action) ⇒ Object
15 16 17 |
# File 'lib/minecraft_rtoolkit/connection.rb', line 15 def make_request(action) "#{action.upcase}:#{@user}:#{@password}" end |
#open ⇒ Object
19 20 21 22 23 24 |
# File 'lib/minecraft_rtoolkit/connection.rb', line 19 def open if @connection.nil? @connection = UDPSocket.new() @connection.connect(@host, @port) end end |
#recieve ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# 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 |