maniaplanet-rpc 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require_relative 'maniaplanet_rpc/maniaplanet_client'
@@ -0,0 +1,92 @@
1
+ require 'xmlrpc/xmlrpcs'
2
+
3
+ class ManiaplanetClient < XMLRPC::Client
4
+
5
+ attr_accessor :protocol
6
+ attr_accessor :socket
7
+ attr_accessor :request_handle
8
+
9
+ def initialize(ip, port)
10
+ new_socket(ip, port)
11
+ end
12
+
13
+ def new_socket(ip, port)
14
+ @socket = TCPSocket.new(ip, port)
15
+ @request_handle = 0x80000000
16
+ authenticate(@socket)
17
+ @socket
18
+ end
19
+
20
+ def authenticate(socket)
21
+ header = socket.recv(4).unpack("Vsize")
22
+
23
+ if header[0] > 64
24
+ raise Exception, "Wrong low-level protocol header!"
25
+ end
26
+
27
+ handshake = socket.recv(header[0])
28
+
29
+ if handshake == "GBXRemote 1"
30
+ @protocol = 1
31
+ elsif handshake == "GBXRemote 2"
32
+ @protocol = 2
33
+ else
34
+ raise Exception, "Unknown protocol version!"
35
+ end
36
+ end
37
+
38
+ def read_response(socket)
39
+ if protocol == 1
40
+ content = socket.recv(4)
41
+ if content.length == 0
42
+ raise Exception, "Cannot read size!"
43
+ end
44
+ result = content.unpack("Vsize")
45
+ size = result[0]
46
+ receive_handle = request_handle
47
+ else
48
+ content = socket.recv(8)
49
+ if content.length == 0
50
+ raise Exception, "Cannot read size/handle!"
51
+ end
52
+ result = content.unpack("Vsize/Vhandle")
53
+ size = result[0]
54
+ receive_handle = result[1]
55
+ end
56
+
57
+ if receive_handle == 0 || size == 0
58
+ raise Exception, "Connection interrupted!"
59
+ end
60
+
61
+ if size > 4096 * 1024
62
+ raise Exception, "Response too large!"
63
+ end
64
+
65
+ response = ""
66
+ response_length = 0
67
+
68
+ while response_length < size
69
+ response << socket.recv(size - response_length)
70
+ response_length = response.length
71
+ end
72
+ response
73
+ end
74
+
75
+ def write_request(socket,request) # :doc:
76
+ @request_handle = @request_handle + 1
77
+ if protocol == 1
78
+ bytes = [request.length, request].pack("Va*")
79
+ socket.write(bytes)
80
+ else
81
+ bytes = [request.length, request_handle, request].pack("VVa*")
82
+ socket.write(bytes)
83
+ end
84
+ socket.write bytes
85
+ end
86
+
87
+ def do_rpc( request, async )
88
+ write_request(socket,request)
89
+ read_response(socket)
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maniaplanet-rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Johan Ljungberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: maniaplanet-rpc is a library for interfacing with maniaplanet servers
15
+ using their custom variant of xml-rpc
16
+ email: johan.f.ljungberg@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/maniaplanet_rpc.rb
22
+ - lib/maniaplanet_rpc/maniaplanet_client.rb
23
+ homepage: http://rubygems.org/gems/maniaplanet-rpc
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A library for interfacing with maniaplanet servers.
47
+ test_files: []