renet 0.1.0-universal-darwin
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.
- data/README +81 -0
- data/examples/Console-Server.rb +47 -0
- data/examples/Gosu-Client.rb +71 -0
- data/ext/renet/enet/callbacks.c +47 -0
- data/ext/renet/enet/callbacks.h +27 -0
- data/ext/renet/enet/compress.c +654 -0
- data/ext/renet/enet/enet.h +540 -0
- data/ext/renet/enet/host.c +479 -0
- data/ext/renet/enet/list.c +75 -0
- data/ext/renet/enet/list.h +43 -0
- data/ext/renet/enet/packet.c +157 -0
- data/ext/renet/enet/peer.c +816 -0
- data/ext/renet/enet/protocol.c +1671 -0
- data/ext/renet/enet/protocol.h +196 -0
- data/ext/renet/enet/time.h +18 -0
- data/ext/renet/enet/types.h +13 -0
- data/ext/renet/enet/unix.c +438 -0
- data/ext/renet/enet/unix.h +45 -0
- data/ext/renet/enet/utility.h +12 -0
- data/ext/renet/enet/win32.c +348 -0
- data/ext/renet/enet/win32.h +58 -0
- data/ext/renet/extconf.rb +21 -0
- data/ext/renet/renet.c +69 -0
- data/ext/renet/renet.h +35 -0
- data/ext/renet/renet_connection.c +322 -0
- data/ext/renet/renet_connection.h +58 -0
- data/ext/renet/renet_server.c +280 -0
- data/ext/renet/renet_server.h +58 -0
- data/lib/renet.rb +10 -0
- metadata +76 -0
@@ -0,0 +1,322 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2011 Dahrkael <dark.wolf.warrior at gmail.com>
|
3
|
+
* Permission is hereby granted, free of charge,
|
4
|
+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
5
|
+
* to deal in the Software without restriction, including without limitation the rights to use,
|
6
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
7
|
+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
8
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
9
|
+
* or substantial portions of the Software.
|
10
|
+
*
|
11
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
12
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
14
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
15
|
+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
16
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include "renet_connection.h"
|
20
|
+
|
21
|
+
VALUE cENetConnection;
|
22
|
+
|
23
|
+
void init_renet_connection()
|
24
|
+
{
|
25
|
+
cENetConnection = rb_define_class_under(mENet, "Connection", rb_cObject);
|
26
|
+
rb_define_alloc_func(cENetConnection, renet_connection_allocate);
|
27
|
+
|
28
|
+
rb_define_method(cENetConnection, "initialize", renet_connection_initialize, 5);
|
29
|
+
rb_define_method(cENetConnection, "connect", renet_connection_connect, 1);
|
30
|
+
rb_define_method(cENetConnection, "disconnect", renet_connection_disconnect, 1);
|
31
|
+
rb_define_method(cENetConnection, "send_packet", renet_connection_send_packet, 3);
|
32
|
+
rb_define_method(cENetConnection, "send_queued_packets", renet_connection_send_queued_packets, 0);
|
33
|
+
rb_define_method(cENetConnection, "flush", renet_connection_send_queued_packets, 0);
|
34
|
+
rb_define_method(cENetConnection, "update", renet_connection_update, 1);
|
35
|
+
rb_define_method(cENetConnection, "use_compression", renet_connection_use_compression, 1);
|
36
|
+
|
37
|
+
rb_define_method(cENetConnection, "on_connection", renet_connection_on_connection, 1);
|
38
|
+
rb_define_method(cENetConnection, "on_packet_receive", renet_connection_on_packet_receive, 1);
|
39
|
+
rb_define_method(cENetConnection, "on_disconnection", renet_connection_on_disconnection, 1);
|
40
|
+
|
41
|
+
rb_define_method(cENetConnection, "online?", renet_connection_online, 0);
|
42
|
+
rb_define_method(cENetConnection, "connected?", renet_connection_online, 0);
|
43
|
+
}
|
44
|
+
|
45
|
+
VALUE renet_connection_allocate(VALUE self)
|
46
|
+
{
|
47
|
+
Connection* connection = malloc(sizeof(Connection));
|
48
|
+
connection->event = malloc(sizeof(ENetEvent));
|
49
|
+
connection->address = malloc(sizeof(ENetAddress));
|
50
|
+
|
51
|
+
return Data_Wrap_Struct(self, NULL, renet_connection_deallocate, connection);
|
52
|
+
}
|
53
|
+
|
54
|
+
void renet_connection_deallocate(void* connection)
|
55
|
+
{
|
56
|
+
free(((Connection*)connection)->event);
|
57
|
+
free(((Connection*)connection)->address);
|
58
|
+
enet_host_destroy(((Connection*)connection)->host);
|
59
|
+
free((Connection*)connection);
|
60
|
+
}
|
61
|
+
|
62
|
+
VALUE renet_connection_initialize(VALUE self, VALUE host, VALUE port, VALUE channels, VALUE download, VALUE upload)
|
63
|
+
{
|
64
|
+
rb_funcall(mENet, rb_intern("initialize"), 0);
|
65
|
+
Connection* connection;
|
66
|
+
Data_Get_Struct(self, Connection, connection);
|
67
|
+
Check_Type(host, T_STRING);
|
68
|
+
if (enet_address_set_host(connection->address, StringValuePtr(host)) != 0)
|
69
|
+
{
|
70
|
+
rb_raise(rb_eStandardError, "Cannot set address");
|
71
|
+
}
|
72
|
+
connection->address->port = NUM2UINT(port);
|
73
|
+
connection->channels = NUM2UINT(channels);
|
74
|
+
connection->online = 0;
|
75
|
+
connection->host = enet_host_create(NULL, 1, connection->channels, NUM2UINT(download), NUM2UINT(upload));
|
76
|
+
if (connection->host == NULL)
|
77
|
+
{
|
78
|
+
rb_raise(rb_eStandardError, "Cannot create host");
|
79
|
+
}
|
80
|
+
rb_iv_set(self, "@total_sent_data", INT2FIX(0));
|
81
|
+
rb_iv_set(self, "@total_received_data", INT2FIX(0));
|
82
|
+
rb_iv_set(self, "@total_sent_packets", INT2FIX(0));
|
83
|
+
rb_iv_set(self, "@total_received_packets", INT2FIX(0));
|
84
|
+
|
85
|
+
rb_define_attr(cENetConnection, "total_sent_data", 1, 1);
|
86
|
+
rb_define_attr(cENetConnection, "total_received_data", 1, 1);
|
87
|
+
rb_define_attr(cENetConnection, "total_sent_packets", 1, 1);
|
88
|
+
rb_define_attr(cENetConnection, "total_received_packets", 1, 1);
|
89
|
+
|
90
|
+
return self;
|
91
|
+
}
|
92
|
+
|
93
|
+
VALUE renet_connection_connect(VALUE self, VALUE timeout)
|
94
|
+
{
|
95
|
+
Connection* connection;
|
96
|
+
Data_Get_Struct(self, Connection, connection);
|
97
|
+
if (connection->online == 1)
|
98
|
+
{
|
99
|
+
return Qfalse;
|
100
|
+
}
|
101
|
+
connection->peer = enet_host_connect(connection->host, connection->address, connection->channels, 0);
|
102
|
+
|
103
|
+
if (connection->peer == NULL)
|
104
|
+
{
|
105
|
+
rb_raise(rb_eStandardError, "Cannot connect to remote host");
|
106
|
+
}
|
107
|
+
|
108
|
+
if (enet_host_service(connection->host, connection->event, NUM2UINT(timeout)) > 0 && connection->event->type == ENET_EVENT_TYPE_CONNECT)
|
109
|
+
{
|
110
|
+
connection->online = 1;
|
111
|
+
renet_connection_execute_on_connection();
|
112
|
+
return Qtrue;
|
113
|
+
}
|
114
|
+
else
|
115
|
+
{
|
116
|
+
enet_peer_reset(connection->peer);
|
117
|
+
return Qfalse;
|
118
|
+
}
|
119
|
+
|
120
|
+
}
|
121
|
+
|
122
|
+
VALUE renet_connection_disconnect(VALUE self, VALUE timeout)
|
123
|
+
{
|
124
|
+
Connection* connection;
|
125
|
+
Data_Get_Struct(self, Connection, connection);
|
126
|
+
|
127
|
+
enet_peer_disconnect(connection->peer, 0);
|
128
|
+
|
129
|
+
while (enet_host_service(connection->host, connection->event, NUM2UINT(timeout)) > 0)
|
130
|
+
{
|
131
|
+
switch (connection->event->type)
|
132
|
+
{
|
133
|
+
case ENET_EVENT_TYPE_RECEIVE:
|
134
|
+
enet_packet_destroy (connection->event->packet);
|
135
|
+
break;
|
136
|
+
|
137
|
+
case ENET_EVENT_TYPE_DISCONNECT:
|
138
|
+
connection->online = 0;
|
139
|
+
return Qtrue;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
enet_peer_disconnect_now(connection->peer, 0);
|
143
|
+
connection->online = 0;
|
144
|
+
return Qfalse;
|
145
|
+
}
|
146
|
+
|
147
|
+
VALUE renet_connection_send_packet(VALUE self, VALUE data, VALUE flag, VALUE channel)
|
148
|
+
{
|
149
|
+
Connection* connection;
|
150
|
+
Data_Get_Struct(self, Connection, connection);
|
151
|
+
Check_Type(data, T_STRING);
|
152
|
+
char* cdata = StringValuePtr(data);
|
153
|
+
ENetPacket* packet;
|
154
|
+
if (flag == Qtrue)
|
155
|
+
{
|
156
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, ENET_PACKET_FLAG_RELIABLE);
|
157
|
+
}
|
158
|
+
else
|
159
|
+
{
|
160
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, 0);
|
161
|
+
}
|
162
|
+
enet_peer_send(connection->peer, NUM2UINT(channel), packet);
|
163
|
+
return Qnil;
|
164
|
+
}
|
165
|
+
|
166
|
+
VALUE renet_connection_send_queued_packets(VALUE self)
|
167
|
+
{
|
168
|
+
Connection* connection;
|
169
|
+
Data_Get_Struct(self, Connection, connection);
|
170
|
+
enet_host_flush(connection->host);
|
171
|
+
return Qnil;
|
172
|
+
}
|
173
|
+
|
174
|
+
VALUE renet_connection_update(VALUE self, VALUE timeout)
|
175
|
+
{
|
176
|
+
Connection* connection;
|
177
|
+
Data_Get_Struct(self, Connection, connection);
|
178
|
+
if (connection->online == 0)
|
179
|
+
{
|
180
|
+
return Qfalse;
|
181
|
+
}
|
182
|
+
while (enet_host_service(connection->host, connection->event, NUM2UINT(timeout)) > 0)
|
183
|
+
{
|
184
|
+
switch (connection->event->type)
|
185
|
+
{
|
186
|
+
case ENET_EVENT_TYPE_NONE:
|
187
|
+
|
188
|
+
break;
|
189
|
+
|
190
|
+
case ENET_EVENT_TYPE_CONNECT:
|
191
|
+
|
192
|
+
break;
|
193
|
+
|
194
|
+
case ENET_EVENT_TYPE_RECEIVE:
|
195
|
+
renet_connection_execute_on_packet_receive(connection->event->packet->data, connection->event->channelID);
|
196
|
+
enet_packet_destroy(connection->event->packet);
|
197
|
+
break;
|
198
|
+
|
199
|
+
case ENET_EVENT_TYPE_DISCONNECT:
|
200
|
+
connection->online = 0;
|
201
|
+
renet_connection_execute_on_disconnection();
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
int tmp;
|
206
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_sent_data"));
|
207
|
+
tmp = tmp + connection->host->totalSentData;
|
208
|
+
connection->host->totalSentData = 0;
|
209
|
+
rb_iv_set(self, "@total_sent_data", UINT2NUM(tmp));
|
210
|
+
|
211
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_received_data"));
|
212
|
+
tmp = tmp + connection->host->totalReceivedData;
|
213
|
+
connection->host->totalReceivedData = 0;
|
214
|
+
rb_iv_set(self, "@total_received_data", UINT2NUM(tmp));
|
215
|
+
|
216
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_sent_packets"));
|
217
|
+
tmp = tmp + connection->host->totalSentPackets;
|
218
|
+
connection->host->totalSentPackets = 0;
|
219
|
+
rb_iv_set(self, "@total_sent_packets", UINT2NUM(tmp));
|
220
|
+
|
221
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_received_packets"));
|
222
|
+
tmp = tmp + connection->host->totalReceivedPackets;
|
223
|
+
connection->host->totalReceivedPackets = 0;
|
224
|
+
rb_iv_set(self, "@total_received_packets", UINT2NUM(tmp));
|
225
|
+
|
226
|
+
return Qtrue;
|
227
|
+
}
|
228
|
+
|
229
|
+
VALUE renet_connection_use_compression(VALUE self, VALUE flag)
|
230
|
+
{
|
231
|
+
Connection* connection;
|
232
|
+
Data_Get_Struct(self, Connection, connection);
|
233
|
+
if (flag == Qtrue)
|
234
|
+
{
|
235
|
+
enet_host_compress_with_range_coder(connection->host);
|
236
|
+
}
|
237
|
+
else
|
238
|
+
{
|
239
|
+
enet_host_compress(connection->host, NULL);
|
240
|
+
}
|
241
|
+
return Qnil;
|
242
|
+
}
|
243
|
+
|
244
|
+
VALUE renet_connection_on_connection(VALUE self, VALUE method)
|
245
|
+
{
|
246
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
247
|
+
rb_iv_set(cENetConnection, "@on_connection", method);
|
248
|
+
return Qnil;
|
249
|
+
}
|
250
|
+
|
251
|
+
void renet_connection_execute_on_connection()
|
252
|
+
{
|
253
|
+
VALUE method = rb_iv_get(cENetConnection, "@on_connection");
|
254
|
+
if (method != Qnil)
|
255
|
+
{
|
256
|
+
rb_funcall(method, rb_intern("call"), 0);
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
VALUE renet_connection_on_packet_receive(VALUE self, VALUE method)
|
261
|
+
{
|
262
|
+
|
263
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
264
|
+
rb_iv_set(cENetConnection, "@on_packet_receive", method);
|
265
|
+
return Qnil;
|
266
|
+
}
|
267
|
+
|
268
|
+
/*VALUE renet_connection_on_packet_receive(int argc, VALUE *argv, VALUE self)
|
269
|
+
{
|
270
|
+
VALUE block = Qnil;
|
271
|
+
|
272
|
+
rb_scan_args(argc, argv, "0&", &block);
|
273
|
+
|
274
|
+
if (RTEST(block))
|
275
|
+
{
|
276
|
+
rb_iv_set(cENetConnection, "@on_packet_receive", block);
|
277
|
+
}
|
278
|
+
else
|
279
|
+
{
|
280
|
+
rb_raise(rb_eArgError, "a block is required");
|
281
|
+
}
|
282
|
+
return Qnil;
|
283
|
+
}*/
|
284
|
+
|
285
|
+
void renet_connection_execute_on_packet_receive(enet_uint8* data, enet_uint8 channelID)
|
286
|
+
{
|
287
|
+
VALUE method = rb_iv_get(cENetConnection, "@on_packet_receive");
|
288
|
+
if (method != Qnil)
|
289
|
+
{
|
290
|
+
rb_funcall(method, rb_intern("call"), 2, rb_str_new2(data), UINT2NUM(channelID));
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
VALUE renet_connection_on_disconnection(VALUE self, VALUE method)
|
295
|
+
{
|
296
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
297
|
+
rb_iv_set(cENetConnection, "@on_disconnection", method);
|
298
|
+
return Qnil;
|
299
|
+
}
|
300
|
+
|
301
|
+
void renet_connection_execute_on_disconnection()
|
302
|
+
{
|
303
|
+
VALUE method = rb_iv_get(cENetConnection, "@on_disconnection");
|
304
|
+
if (method != Qnil)
|
305
|
+
{
|
306
|
+
rb_funcall(method, rb_intern("call"), 0);
|
307
|
+
}
|
308
|
+
}
|
309
|
+
|
310
|
+
VALUE renet_connection_online(VALUE self)
|
311
|
+
{
|
312
|
+
Connection* connection;
|
313
|
+
Data_Get_Struct(self, Connection, connection);
|
314
|
+
if (connection->online == 1)
|
315
|
+
{
|
316
|
+
return Qtrue;
|
317
|
+
}
|
318
|
+
else
|
319
|
+
{
|
320
|
+
return Qfalse;
|
321
|
+
}
|
322
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2011 Dahrkael <dark.wolf.warrior at gmail.com>
|
3
|
+
* Permission is hereby granted, free of charge,
|
4
|
+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
5
|
+
* to deal in the Software without restriction, including without limitation the rights to use,
|
6
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
7
|
+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
8
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
9
|
+
* or substantial portions of the Software.
|
10
|
+
*
|
11
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
12
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
14
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
15
|
+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
16
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#ifndef RUBY_ENET_CONNECTION
|
20
|
+
#define RUBY_ENET_CONNECTION
|
21
|
+
|
22
|
+
#include "renet.h"
|
23
|
+
|
24
|
+
typedef struct {
|
25
|
+
ENetHost* host;
|
26
|
+
ENetPeer* peer;
|
27
|
+
ENetEvent* event;
|
28
|
+
ENetAddress* address;
|
29
|
+
int channels;
|
30
|
+
int online;
|
31
|
+
} Connection;
|
32
|
+
|
33
|
+
void init_renet_connection();
|
34
|
+
|
35
|
+
VALUE renet_connection_allocate(VALUE self);
|
36
|
+
void renet_connection_deallocate(void* connection);
|
37
|
+
|
38
|
+
VALUE renet_connection_initialize(VALUE self, VALUE host, VALUE port, VALUE channels, VALUE download, VALUE upload);
|
39
|
+
VALUE renet_connection_connect(VALUE self, VALUE timeout);
|
40
|
+
VALUE renet_connection_disconnect(VALUE self, VALUE timeout);
|
41
|
+
VALUE renet_connection_send_packet(VALUE self, VALUE data, VALUE flag, VALUE channel);
|
42
|
+
VALUE renet_connection_send_queued_packets(VALUE self);
|
43
|
+
VALUE renet_connection_update(VALUE self, VALUE timeout);
|
44
|
+
VALUE renet_connection_use_compression(VALUE self, VALUE flag);
|
45
|
+
|
46
|
+
VALUE renet_connection_on_connection(VALUE self, VALUE method);
|
47
|
+
void renet_connection_execute_on_connection();
|
48
|
+
|
49
|
+
VALUE renet_connection_on_packet_receive(VALUE self, VALUE method);
|
50
|
+
/*VALUE renet_connection_on_packet_receive(int argc, VALUE *argv, VALUE self);*/
|
51
|
+
void renet_connection_execute_on_packet_receive(enet_uint8* data, enet_uint8 channelID);
|
52
|
+
|
53
|
+
VALUE renet_connection_on_disconnection(VALUE self, VALUE method);
|
54
|
+
void renet_connection_execute_on_disconnection();
|
55
|
+
|
56
|
+
VALUE renet_connection_online(VALUE self);
|
57
|
+
|
58
|
+
#endif
|
@@ -0,0 +1,280 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2011 Dahrkael <dark.wolf.warrior at gmail.com>
|
3
|
+
* Permission is hereby granted, free of charge,
|
4
|
+
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
5
|
+
* to deal in the Software without restriction, including without limitation the rights to use,
|
6
|
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
7
|
+
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
8
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
9
|
+
* or substantial portions of the Software.
|
10
|
+
*
|
11
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
12
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
14
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
15
|
+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
16
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include "renet_server.h"
|
20
|
+
|
21
|
+
VALUE cENetServer;
|
22
|
+
|
23
|
+
void init_renet_server()
|
24
|
+
{
|
25
|
+
cENetServer = rb_define_class_under(mENet, "Server", rb_cObject);
|
26
|
+
rb_define_alloc_func(cENetServer, renet_server_allocate);
|
27
|
+
|
28
|
+
rb_define_method(cENetServer, "initialize", renet_server_initialize, 5);
|
29
|
+
rb_define_method(cENetServer, "disconnect_client", renet_server_disconnect_client, 1);
|
30
|
+
rb_define_method(cENetServer, "send_packet", renet_server_send_packet, 4);
|
31
|
+
rb_define_method(cENetServer, "broadcast_packet", renet_server_broadcast_packet, 3);
|
32
|
+
rb_define_method(cENetServer, "send_queued_packets", renet_server_send_queued_packets, 0);
|
33
|
+
rb_define_method(cENetServer, "flush", renet_server_send_queued_packets, 0);
|
34
|
+
rb_define_method(cENetServer, "update", renet_server_update, 1);
|
35
|
+
rb_define_method(cENetServer, "use_compression", renet_server_use_compression, 1);
|
36
|
+
|
37
|
+
rb_define_method(cENetServer, "on_connection", renet_server_on_connection, 1);
|
38
|
+
rb_define_method(cENetServer, "on_packet_receive", renet_server_on_packet_receive, 1);
|
39
|
+
rb_define_method(cENetServer, "on_disconnection", renet_server_on_disconnection, 1);
|
40
|
+
|
41
|
+
rb_define_method(cENetServer, "max_clients", renet_server_max_clients, 0);
|
42
|
+
rb_define_method(cENetServer, "clients_count", renet_server_clients_count, 0);
|
43
|
+
}
|
44
|
+
|
45
|
+
VALUE renet_server_allocate(VALUE self)
|
46
|
+
{
|
47
|
+
Server* server = malloc(sizeof(Server));
|
48
|
+
server->event = malloc(sizeof(ENetEvent));
|
49
|
+
server->address = malloc(sizeof(ENetAddress));
|
50
|
+
server->conn_ip = malloc(sizeof(char)*30);
|
51
|
+
|
52
|
+
return Data_Wrap_Struct(self, NULL, renet_server_deallocate, server);
|
53
|
+
}
|
54
|
+
|
55
|
+
void renet_server_deallocate(void* server)
|
56
|
+
{
|
57
|
+
free(((Server*)server)->event);
|
58
|
+
free(((Server*)server)->address);
|
59
|
+
enet_host_destroy(((Server*)server)->host);
|
60
|
+
free((Server*)server);
|
61
|
+
}
|
62
|
+
|
63
|
+
VALUE renet_server_initialize(VALUE self, VALUE port, VALUE n_peers, VALUE channels, VALUE download, VALUE upload)
|
64
|
+
{
|
65
|
+
rb_funcall(mENet, rb_intern("initialize"), 0);
|
66
|
+
Server* server;
|
67
|
+
Data_Get_Struct(self, Server, server);
|
68
|
+
|
69
|
+
server->address->host = ENET_HOST_ANY;
|
70
|
+
server->address->port = NUM2UINT(port);
|
71
|
+
server->channels = NUM2UINT(channels);
|
72
|
+
server->host = enet_host_create(server->address, NUM2UINT(n_peers), server->channels, NUM2UINT(download), NUM2UINT(upload));
|
73
|
+
if (server->host == NULL)
|
74
|
+
{
|
75
|
+
rb_raise(rb_eStandardError, "Cannot create server");
|
76
|
+
}
|
77
|
+
rb_iv_set(self, "@total_sent_data", INT2FIX(0));
|
78
|
+
rb_iv_set(self, "@total_received_data", INT2FIX(0));
|
79
|
+
rb_iv_set(self, "@total_sent_packets", INT2FIX(0));
|
80
|
+
rb_iv_set(self, "@total_received_packets", INT2FIX(0));
|
81
|
+
|
82
|
+
rb_define_attr(cENetServer, "total_sent_data", 1, 1);
|
83
|
+
rb_define_attr(cENetServer, "total_received_data", 1, 1);
|
84
|
+
rb_define_attr(cENetServer, "total_sent_packets", 1, 1);
|
85
|
+
rb_define_attr(cENetServer, "total_received_packets", 1, 1);
|
86
|
+
|
87
|
+
return self;
|
88
|
+
}
|
89
|
+
|
90
|
+
VALUE renet_server_disconnect_client(VALUE self, VALUE peer_id)
|
91
|
+
{
|
92
|
+
Server* server;
|
93
|
+
Data_Get_Struct(self, Server, server);
|
94
|
+
enet_peer_disconnect_now(&(server->host->peers[NUM2UINT(peer_id)]), 0);
|
95
|
+
renet_server_execute_on_disconnection(peer_id);
|
96
|
+
return Qtrue;
|
97
|
+
}
|
98
|
+
|
99
|
+
VALUE renet_server_send_packet(VALUE self, VALUE peer_id, VALUE data, VALUE flag, VALUE channel)
|
100
|
+
{
|
101
|
+
Server* server;
|
102
|
+
Data_Get_Struct(self, Server, server);
|
103
|
+
Check_Type(data, T_STRING);
|
104
|
+
char* cdata = StringValuePtr(data);
|
105
|
+
ENetPacket* packet;
|
106
|
+
if (flag == Qtrue)
|
107
|
+
{
|
108
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, ENET_PACKET_FLAG_RELIABLE);
|
109
|
+
}
|
110
|
+
else
|
111
|
+
{
|
112
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, 0);
|
113
|
+
}
|
114
|
+
enet_peer_send(&(server->host->peers[NUM2UINT(peer_id)]), NUM2UINT(channel), packet);
|
115
|
+
return Qnil;
|
116
|
+
}
|
117
|
+
|
118
|
+
VALUE renet_server_broadcast_packet(VALUE self, VALUE data, VALUE flag, VALUE channel)
|
119
|
+
{
|
120
|
+
Server* server;
|
121
|
+
Data_Get_Struct(self, Server, server);
|
122
|
+
Check_Type(data, T_STRING);
|
123
|
+
char* cdata = StringValuePtr(data);
|
124
|
+
ENetPacket* packet;
|
125
|
+
if (flag == Qtrue)
|
126
|
+
{
|
127
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, ENET_PACKET_FLAG_RELIABLE);
|
128
|
+
}
|
129
|
+
else
|
130
|
+
{
|
131
|
+
packet = enet_packet_create(cdata, RSTRING_LEN(data) + 1, 0);
|
132
|
+
}
|
133
|
+
enet_host_broadcast(server->host, NUM2UINT(channel), packet);
|
134
|
+
return Qnil;
|
135
|
+
}
|
136
|
+
|
137
|
+
VALUE renet_server_send_queued_packets(VALUE self)
|
138
|
+
{
|
139
|
+
Server* server;
|
140
|
+
Data_Get_Struct(self, Server, server);
|
141
|
+
enet_host_flush(server->host);
|
142
|
+
return Qnil;
|
143
|
+
}
|
144
|
+
|
145
|
+
VALUE renet_server_update(VALUE self, VALUE timeout)
|
146
|
+
{
|
147
|
+
Server* server;
|
148
|
+
Data_Get_Struct(self, Server, server);
|
149
|
+
int peer_id;
|
150
|
+
|
151
|
+
while (enet_host_service(server->host, server->event, NUM2UINT(timeout)) > 0)
|
152
|
+
{
|
153
|
+
switch (server->event->type)
|
154
|
+
{
|
155
|
+
case ENET_EVENT_TYPE_NONE:
|
156
|
+
|
157
|
+
break;
|
158
|
+
|
159
|
+
case ENET_EVENT_TYPE_CONNECT:
|
160
|
+
server->n_clients += 1;
|
161
|
+
enet_address_get_host_ip(&(server->event->peer->address), server->conn_ip, 20);
|
162
|
+
peer_id = (int)(server->event->peer - server->host->peers);
|
163
|
+
renet_server_execute_on_connection(INT2NUM(peer_id), rb_str_new2(server->conn_ip));
|
164
|
+
break;
|
165
|
+
|
166
|
+
case ENET_EVENT_TYPE_RECEIVE:
|
167
|
+
peer_id = (int)(server->event->peer - server->host->peers);
|
168
|
+
renet_server_execute_on_packet_receive(INT2NUM(peer_id), server->event->packet->data, server->event->channelID);
|
169
|
+
enet_packet_destroy(server->event->packet);
|
170
|
+
break;
|
171
|
+
|
172
|
+
case ENET_EVENT_TYPE_DISCONNECT:
|
173
|
+
server->n_clients -= 1;
|
174
|
+
peer_id = (int)(server->event->peer - server->host->peers);
|
175
|
+
renet_server_execute_on_disconnection(INT2NUM(peer_id));
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
int tmp;
|
180
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_sent_data"));
|
181
|
+
tmp = tmp + server->host->totalSentData;
|
182
|
+
server->host->totalSentData = 0;
|
183
|
+
rb_iv_set(self, "@total_sent_data", UINT2NUM(tmp));
|
184
|
+
|
185
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_received_data"));
|
186
|
+
tmp = tmp + server->host->totalReceivedData;
|
187
|
+
server->host->totalReceivedData = 0;
|
188
|
+
rb_iv_set(self, "@total_received_data", UINT2NUM(tmp));
|
189
|
+
|
190
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_sent_packets"));
|
191
|
+
tmp = tmp + server->host->totalSentPackets;
|
192
|
+
server->host->totalSentPackets = 0;
|
193
|
+
rb_iv_set(self, "@total_sent_packets", UINT2NUM(tmp));
|
194
|
+
|
195
|
+
tmp = NUM2INT(rb_iv_get(self, "@total_received_packets"));
|
196
|
+
tmp = tmp + server->host->totalReceivedPackets;
|
197
|
+
server->host->totalReceivedPackets = 0;
|
198
|
+
rb_iv_set(self, "@total_received_packets", UINT2NUM(tmp));
|
199
|
+
|
200
|
+
return Qtrue;
|
201
|
+
}
|
202
|
+
|
203
|
+
VALUE renet_server_use_compression(VALUE self, VALUE flag)
|
204
|
+
{
|
205
|
+
Server* server;
|
206
|
+
Data_Get_Struct(self, Server, server);
|
207
|
+
if (flag == Qtrue)
|
208
|
+
{
|
209
|
+
enet_host_compress_with_range_coder(server->host);
|
210
|
+
}
|
211
|
+
else
|
212
|
+
{
|
213
|
+
enet_host_compress(server->host, NULL);
|
214
|
+
}
|
215
|
+
return Qnil;
|
216
|
+
}
|
217
|
+
|
218
|
+
VALUE renet_server_on_connection(VALUE self, VALUE method)
|
219
|
+
{
|
220
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
221
|
+
rb_iv_set(cENetServer, "@on_connection", method);
|
222
|
+
return Qnil;
|
223
|
+
}
|
224
|
+
|
225
|
+
void renet_server_execute_on_connection(VALUE peer_id, VALUE ip)
|
226
|
+
{
|
227
|
+
VALUE method = rb_iv_get(cENetServer, "@on_connection");
|
228
|
+
if (method != Qnil)
|
229
|
+
{
|
230
|
+
rb_funcall(method, rb_intern("call"), 2, peer_id, ip);
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
VALUE renet_server_on_packet_receive(VALUE self, VALUE method)
|
235
|
+
{
|
236
|
+
|
237
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
238
|
+
rb_iv_set(cENetServer, "@on_packet_receive", method);
|
239
|
+
return Qnil;
|
240
|
+
}
|
241
|
+
|
242
|
+
void renet_server_execute_on_packet_receive(VALUE peer_id, enet_uint8* data, enet_uint8 channelID)
|
243
|
+
{
|
244
|
+
VALUE method = rb_iv_get(cENetServer, "@on_packet_receive");
|
245
|
+
if (method != Qnil)
|
246
|
+
{
|
247
|
+
rb_funcall(method, rb_intern("call"), 3, peer_id, rb_str_new2(data), UINT2NUM(channelID));
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
VALUE renet_server_on_disconnection(VALUE self, VALUE method)
|
252
|
+
{
|
253
|
+
/*VALUE method = rb_funcall(rb_cObject, rb_intern("method"), 1, symbol);*/
|
254
|
+
rb_iv_set(cENetServer, "@on_disconnection", method);
|
255
|
+
return Qnil;
|
256
|
+
}
|
257
|
+
|
258
|
+
void renet_server_execute_on_disconnection(VALUE peer_id)
|
259
|
+
{
|
260
|
+
VALUE method = rb_iv_get(cENetServer, "@on_disconnection");
|
261
|
+
if (method != Qnil)
|
262
|
+
{
|
263
|
+
rb_funcall(method, rb_intern("call"), 1, peer_id);
|
264
|
+
}
|
265
|
+
}
|
266
|
+
|
267
|
+
VALUE renet_server_max_clients(VALUE self)
|
268
|
+
{
|
269
|
+
Server* server;
|
270
|
+
Data_Get_Struct(self, Server, server);
|
271
|
+
return UINT2NUM(server->host->peerCount);
|
272
|
+
}
|
273
|
+
|
274
|
+
VALUE renet_server_clients_count(VALUE self)
|
275
|
+
{
|
276
|
+
Server* server;
|
277
|
+
Data_Get_Struct(self, Server, server);
|
278
|
+
return UINT2NUM(server->n_clients);
|
279
|
+
}
|
280
|
+
|