Class: MinecraftServerStatus::Query
- Inherits:
-
Object
- Object
- MinecraftServerStatus::Query
- Defined in:
- lib/minecraft_server_status/query.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #execute(options = {}) ⇒ Object
-
#initialize(host, port = 25565) ⇒ Query
constructor
A new instance of Query.
Constructor Details
#initialize(host, port = 25565) ⇒ Query
Returns a new instance of Query.
8 9 10 11 12 |
# File 'lib/minecraft_server_status/query.rb', line 8 def initialize(host, port=25565) raise ArgumentError.new('host is required') if host.nil? @host = host @port = port end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/minecraft_server_status/query.rb', line 6 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
6 7 8 |
# File 'lib/minecraft_server_status/query.rb', line 6 def port @port end |
Instance Method Details
#execute(options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/minecraft_server_status/query.rb', line 14 def execute(={}) timeout_sec = .fetch(:timeout_sec, 1) retry_limit = .fetch(:retry_limit, 1) retry_count = 0 errors = [] while retry_count < retry_limit begin response = "" Timeout.timeout(timeout_sec) do begin # Connect socket = TCPSocket.open(host, port) # Send request socket.write(pack_data("\x00\x00" + pack_data(host.encode("UTF-8")) + pack_port(port) + "\x01")) socket.write(pack_data("\x00")) # Read response unpack_varint(socket) unpack_varint(socket) l = unpack_varint(socket) while response.length < l response += socket.recv(1024) end ensure # Close socket.close if socket end end response.force_encoding("UTF-8") json_response = JSON.parse(response.gsub(/\xC2\xA7./, '')) return Result.new(json_response, true, errors) rescue Exception => e errors << e retry_count += 1 end end return Result.new(nil, false, errors) end |