maniaplanet-rpc 0.0.4 → 0.1.0

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.
@@ -1,29 +1,29 @@
1
1
  require 'xmlrpc/client'
2
+ require 'thread'
2
3
 
3
- class ManiaplanetClient < XMLRPC::Client
4
+ class ManiaConnection
4
5
 
5
- attr_accessor :request_handle, :ip, :port, :protocol
6
+ include XMLRPC::ParserWriterChooseMixin
6
7
 
7
- def initialize(ip, port)
8
+ def initialize(ip, port, callback)
9
+ @callback = callback
8
10
  @request_handle = 0x80000000
9
11
  @ip = ip
10
12
  @port = port
13
+ @send_queue = Queue.new
14
+ @socket = TCPSocket.new ip, port
15
+ @protocol = 0
16
+ authenticate
11
17
  end
12
18
 
13
- def new_socket(ip, port)
14
- socket = TCPSocket.new ip, port
15
- authenticate socket
16
- socket
17
- end
18
-
19
- def authenticate(socket)
20
- header = socket.recv(4).unpack "Vsize"
19
+ def authenticate
20
+ header = @socket.recv(4).unpack "Vsize"
21
21
 
22
22
  if header[0] > 64
23
23
  raise Exception, "Wrong low-level protocol header!"
24
24
  end
25
25
 
26
- handshake = socket.recv header[0]
26
+ handshake = @socket.recv header[0]
27
27
 
28
28
  if handshake == "GBXRemote 1"
29
29
  @protocol = 1
@@ -32,14 +32,30 @@ class ManiaplanetClient < XMLRPC::Client
32
32
  else
33
33
  raise Exception, "Unknown protocol version!"
34
34
  end
35
- puts "Handshake Complete"
36
35
  end
37
36
 
38
- def read_response(socket)
39
- response = ""
37
+ def start
40
38
  loop do
41
- if protocol == 1
42
- content = socket.recv 4
39
+ sleep(1.0/24.0)
40
+ tick
41
+ end
42
+ end
43
+
44
+ def call(method, *args)
45
+ @request_handle = @request_handle + 1
46
+ @send_queue.push [@request_handle, create().methodCall(method, *args)]
47
+ @request_handle - 0x80000000
48
+ end
49
+
50
+ def tick
51
+ write
52
+ read
53
+ end
54
+
55
+ def read
56
+ if IO.select([@socket], nil, nil, 0) != nil
57
+ if @protocol == 1
58
+ content = @socket.recv 4
43
59
  if content.bytesize == 0
44
60
  raise Exception, "Cannot read size!"
45
61
  end
@@ -47,7 +63,7 @@ class ManiaplanetClient < XMLRPC::Client
47
63
  size = result[0]
48
64
  receive_handle = @request_handle
49
65
  else
50
- content = socket.recv 8
66
+ content = @socket.recv 8
51
67
  if content.bytesize == 0
52
68
  raise Exception, "Cannot read size/handle!"
53
69
  end
@@ -68,34 +84,54 @@ class ManiaplanetClient < XMLRPC::Client
68
84
  response_length = 0
69
85
 
70
86
  while response_length < size
71
- response << socket.recv(size - response_length)
87
+ response << @socket.recv(size - response_length)
72
88
  response_length = response.bytesize
73
89
  end
74
90
 
75
- if @request_handle - 0x80000000 != receive_handle
76
- # TODO: Add support for callbacks here.
91
+ if @callback.response_map[receive_handle] == nil
92
+ @callback.parse_callback parser().parseMethodResponse(response)
93
+ else
94
+ @callback.response_map[receive_handle].call parser().parseMethodResponse(response)
77
95
  end
96
+ end
97
+ end
78
98
 
79
-
80
- break if @request_handle - 0x80000000 == receive_handle
99
+ def write
100
+ while @send_queue.length > 0
101
+ request = @send_queue.pop
102
+ if @protocol == 1
103
+ bytes = [request[1].bytesize, request[1]].pack("Va*")
104
+ else
105
+ bytes = [request[1].bytesize, request[0], request[1]].pack("VVa*")
106
+ end
107
+ @socket.write bytes
81
108
  end
82
- response
83
109
  end
84
110
 
85
- def write_request(socket,request)
86
- @request_handle = @request_handle + 1
87
- if protocol == 1
88
- bytes = [request.bytesize, request].pack("Va*")
89
- else
90
- bytes = [request.bytesize, @request_handle, request].pack("VVa*")
111
+ end
112
+
113
+ class ManiaClient
114
+
115
+ attr_reader :connection
116
+ attr_accessor :response_map
117
+
118
+ def initialize(ip, port)
119
+ @response_map = {}
120
+ @connection = ManiaConnection.new ip, port, self
121
+ Thread.new do
122
+ @connection.start
91
123
  end
92
- socket.write bytes
93
124
  end
94
125
 
95
- def do_rpc(request, async) # TODO: Handle async requests
96
- sock = new_socket ip, port
97
- write_request sock, request
98
- read_response sock
126
+ def parse_callback(message)
127
+ # TODO: Fix
99
128
  end
100
129
 
130
+ def call(method, *args, &block)
131
+ if block_given?
132
+ @response_map[@connection.call(method, *args)] = block
133
+ else
134
+ @response_map[@connection.call(method, *args)] = proc {}
135
+ end
136
+ end
101
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maniaplanet-rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-06 00:00:00.000000000 Z
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: maniaplanet-rpc is a library for interfacing with maniaplanet servers
15
15
  using their custom variant of xml-rpc