renet 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,8 +26,8 @@ typedef struct {
26
26
  ENetEvent* event;
27
27
  ENetAddress* address;
28
28
  int channels;
29
- int n_clients;
30
- char* conn_ip;
29
+ int n_clients;
30
+ char* conn_ip;
31
31
  } Server;
32
32
 
33
33
  void init_renet_server();
@@ -44,13 +44,13 @@ VALUE renet_server_update(VALUE self, VALUE timeout);
44
44
  VALUE renet_server_use_compression(VALUE self, VALUE flag);
45
45
 
46
46
  VALUE renet_server_on_connection(VALUE self, VALUE method);
47
- void renet_server_execute_on_connection(VALUE peer_id, VALUE ip);
47
+ void renet_server_execute_on_connection(VALUE self, VALUE peer_id, VALUE ip);
48
48
 
49
49
  VALUE renet_server_on_packet_receive(VALUE self, VALUE method);
50
- void renet_server_execute_on_packet_receive(VALUE peer_id, enet_uint8* data, enet_uint8 channelID);
50
+ void renet_server_execute_on_packet_receive(VALUE self, VALUE peer_id, ENetPacket * const packet, enet_uint8 channelID);
51
51
 
52
52
  VALUE renet_server_on_disconnection(VALUE self, VALUE method);
53
- void renet_server_execute_on_disconnection(VALUE peer_id);
53
+ void renet_server_execute_on_disconnection(VALUE self, VALUE peer_id);
54
54
 
55
55
  VALUE renet_server_max_clients(VALUE self);
56
56
  VALUE renet_server_clients_count(VALUE self);
@@ -0,0 +1,557 @@
1
+ /**
2
+ @file unix.c
3
+ @brief ENet Unix system specific functions
4
+ */
5
+ #ifndef _WIN32
6
+
7
+ #include <sys/types.h>
8
+ #include <sys/socket.h>
9
+ #include <sys/ioctl.h>
10
+ #include <sys/time.h>
11
+ #include <arpa/inet.h>
12
+ #include <netinet/tcp.h>
13
+ #include <netdb.h>
14
+ #include <unistd.h>
15
+ #include <string.h>
16
+ #include <errno.h>
17
+ #include <time.h>
18
+
19
+ #define ENET_BUILDING_LIB 1
20
+ #include "enet/enet.h"
21
+
22
+ #ifdef __APPLE__
23
+ #ifdef HAS_POLL
24
+ #undef HAS_POLL
25
+ #endif
26
+ #ifndef HAS_FCNTL
27
+ #define HAS_FCNTL 1
28
+ #endif
29
+ #ifndef HAS_INET_PTON
30
+ #define HAS_INET_PTON 1
31
+ #endif
32
+ #ifndef HAS_INET_NTOP
33
+ #define HAS_INET_NTOP 1
34
+ #endif
35
+ #ifndef HAS_MSGHDR_FLAGS
36
+ #define HAS_MSGHDR_FLAGS 1
37
+ #endif
38
+ #ifndef HAS_SOCKLEN_T
39
+ #define HAS_SOCKLEN_T 1
40
+ #endif
41
+ #endif
42
+
43
+ #ifdef HAS_FCNTL
44
+ #include <fcntl.h>
45
+ #endif
46
+
47
+ #ifdef HAS_POLL
48
+ #include <sys/poll.h>
49
+ #endif
50
+
51
+ #ifndef HAS_SOCKLEN_T
52
+ typedef int socklen_t;
53
+ #endif
54
+
55
+ #ifndef MSG_NOSIGNAL
56
+ #define MSG_NOSIGNAL 0
57
+ #endif
58
+
59
+ static enet_uint32 timeBase = 0;
60
+
61
+ int
62
+ enet_initialize (void)
63
+ {
64
+ return 0;
65
+ }
66
+
67
+ void
68
+ enet_deinitialize (void)
69
+ {
70
+ }
71
+
72
+ enet_uint32
73
+ enet_host_random_seed (void)
74
+ {
75
+ return (enet_uint32) time (NULL);
76
+ }
77
+
78
+ enet_uint32
79
+ enet_time_get (void)
80
+ {
81
+ struct timeval timeVal;
82
+
83
+ gettimeofday (& timeVal, NULL);
84
+
85
+ return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
86
+ }
87
+
88
+ void
89
+ enet_time_set (enet_uint32 newTimeBase)
90
+ {
91
+ struct timeval timeVal;
92
+
93
+ gettimeofday (& timeVal, NULL);
94
+
95
+ timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
96
+ }
97
+
98
+ int
99
+ enet_address_set_host (ENetAddress * address, const char * name)
100
+ {
101
+ struct hostent * hostEntry = NULL;
102
+ #ifdef HAS_GETHOSTBYNAME_R
103
+ struct hostent hostData;
104
+ char buffer [2048];
105
+ int errnum;
106
+
107
+ #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
108
+ gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
109
+ #else
110
+ hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
111
+ #endif
112
+ #else
113
+ hostEntry = gethostbyname (name);
114
+ #endif
115
+
116
+ if (hostEntry == NULL ||
117
+ hostEntry -> h_addrtype != AF_INET)
118
+ {
119
+ #ifdef HAS_INET_PTON
120
+ if (! inet_pton (AF_INET, name, & address -> host))
121
+ #else
122
+ if (! inet_aton (name, (struct in_addr *) & address -> host))
123
+ #endif
124
+ return -1;
125
+ return 0;
126
+ }
127
+
128
+ address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
129
+
130
+ return 0;
131
+ }
132
+
133
+ int
134
+ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
135
+ {
136
+ #ifdef HAS_INET_NTOP
137
+ if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
138
+ #else
139
+ char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
140
+ if (addr != NULL)
141
+ {
142
+ size_t addrLen = strlen(addr);
143
+ if (addrLen >= nameLength)
144
+ return -1;
145
+ memcpy (name, addr, addrLen + 1);
146
+ }
147
+ else
148
+ #endif
149
+ return -1;
150
+ return 0;
151
+ }
152
+
153
+ int
154
+ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
155
+ {
156
+ struct in_addr in;
157
+ struct hostent * hostEntry = NULL;
158
+ #ifdef HAS_GETHOSTBYADDR_R
159
+ struct hostent hostData;
160
+ char buffer [2048];
161
+ int errnum;
162
+
163
+ in.s_addr = address -> host;
164
+
165
+ #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
166
+ gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
167
+ #else
168
+ hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
169
+ #endif
170
+ #else
171
+ in.s_addr = address -> host;
172
+
173
+ hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
174
+ #endif
175
+
176
+ if (hostEntry == NULL)
177
+ return enet_address_get_host_ip (address, name, nameLength);
178
+ else
179
+ {
180
+ size_t hostLen = strlen (hostEntry -> h_name);
181
+ if (hostLen >= nameLength)
182
+ return -1;
183
+ memcpy (name, hostEntry -> h_name, hostLen + 1);
184
+ }
185
+
186
+ return 0;
187
+ }
188
+
189
+ int
190
+ enet_socket_bind (ENetSocket socket, const ENetAddress * address)
191
+ {
192
+ struct sockaddr_in sin;
193
+
194
+ memset (& sin, 0, sizeof (struct sockaddr_in));
195
+
196
+ sin.sin_family = AF_INET;
197
+
198
+ if (address != NULL)
199
+ {
200
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
201
+ sin.sin_addr.s_addr = address -> host;
202
+ }
203
+ else
204
+ {
205
+ sin.sin_port = 0;
206
+ sin.sin_addr.s_addr = INADDR_ANY;
207
+ }
208
+
209
+ return bind (socket,
210
+ (struct sockaddr *) & sin,
211
+ sizeof (struct sockaddr_in));
212
+ }
213
+
214
+ int
215
+ enet_socket_get_address (ENetSocket socket, ENetAddress * address)
216
+ {
217
+ struct sockaddr_in sin;
218
+ socklen_t sinLength = sizeof (struct sockaddr_in);
219
+
220
+ if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
221
+ return -1;
222
+
223
+ address -> host = (enet_uint32) sin.sin_addr.s_addr;
224
+ address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
225
+
226
+ return 0;
227
+ }
228
+
229
+ int
230
+ enet_socket_listen (ENetSocket socket, int backlog)
231
+ {
232
+ return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
233
+ }
234
+
235
+ ENetSocket
236
+ enet_socket_create (ENetSocketType type)
237
+ {
238
+ return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
239
+ }
240
+
241
+ int
242
+ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
243
+ {
244
+ int result = -1;
245
+ switch (option)
246
+ {
247
+ case ENET_SOCKOPT_NONBLOCK:
248
+ #ifdef HAS_FCNTL
249
+ result = fcntl (socket, F_SETFL, (value ? O_NONBLOCK : 0) | (fcntl (socket, F_GETFL) & ~O_NONBLOCK));
250
+ #else
251
+ result = ioctl (socket, FIONBIO, & value);
252
+ #endif
253
+ break;
254
+
255
+ case ENET_SOCKOPT_BROADCAST:
256
+ result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
257
+ break;
258
+
259
+ case ENET_SOCKOPT_REUSEADDR:
260
+ result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
261
+ break;
262
+
263
+ case ENET_SOCKOPT_RCVBUF:
264
+ result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
265
+ break;
266
+
267
+ case ENET_SOCKOPT_SNDBUF:
268
+ result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
269
+ break;
270
+
271
+ case ENET_SOCKOPT_RCVTIMEO:
272
+ {
273
+ struct timeval timeVal;
274
+ timeVal.tv_sec = value / 1000;
275
+ timeVal.tv_usec = (value % 1000) * 1000;
276
+ result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval));
277
+ break;
278
+ }
279
+
280
+ case ENET_SOCKOPT_SNDTIMEO:
281
+ {
282
+ struct timeval timeVal;
283
+ timeVal.tv_sec = value / 1000;
284
+ timeVal.tv_usec = (value % 1000) * 1000;
285
+ result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval));
286
+ break;
287
+ }
288
+
289
+ case ENET_SOCKOPT_NODELAY:
290
+ result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
291
+ break;
292
+
293
+ default:
294
+ break;
295
+ }
296
+ return result == -1 ? -1 : 0;
297
+ }
298
+
299
+ int
300
+ enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
301
+ {
302
+ int result = -1;
303
+ socklen_t len;
304
+ switch (option)
305
+ {
306
+ case ENET_SOCKOPT_ERROR:
307
+ len = sizeof (int);
308
+ result = getsockopt (socket, SOL_SOCKET, SO_ERROR, value, & len);
309
+ break;
310
+
311
+ default:
312
+ break;
313
+ }
314
+ return result == -1 ? -1 : 0;
315
+ }
316
+
317
+ int
318
+ enet_socket_connect (ENetSocket socket, const ENetAddress * address)
319
+ {
320
+ struct sockaddr_in sin;
321
+ int result;
322
+
323
+ memset (& sin, 0, sizeof (struct sockaddr_in));
324
+
325
+ sin.sin_family = AF_INET;
326
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
327
+ sin.sin_addr.s_addr = address -> host;
328
+
329
+ result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
330
+ if (result == -1 && errno == EINPROGRESS)
331
+ return 0;
332
+
333
+ return result;
334
+ }
335
+
336
+ ENetSocket
337
+ enet_socket_accept (ENetSocket socket, ENetAddress * address)
338
+ {
339
+ int result;
340
+ struct sockaddr_in sin;
341
+ socklen_t sinLength = sizeof (struct sockaddr_in);
342
+
343
+ result = accept (socket,
344
+ address != NULL ? (struct sockaddr *) & sin : NULL,
345
+ address != NULL ? & sinLength : NULL);
346
+
347
+ if (result == -1)
348
+ return ENET_SOCKET_NULL;
349
+
350
+ if (address != NULL)
351
+ {
352
+ address -> host = (enet_uint32) sin.sin_addr.s_addr;
353
+ address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
354
+ }
355
+
356
+ return result;
357
+ }
358
+
359
+ int
360
+ enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
361
+ {
362
+ return shutdown (socket, (int) how);
363
+ }
364
+
365
+ void
366
+ enet_socket_destroy (ENetSocket socket)
367
+ {
368
+ if (socket != -1)
369
+ close (socket);
370
+ }
371
+
372
+ int
373
+ enet_socket_send (ENetSocket socket,
374
+ const ENetAddress * address,
375
+ const ENetBuffer * buffers,
376
+ size_t bufferCount)
377
+ {
378
+ struct msghdr msgHdr;
379
+ struct sockaddr_in sin;
380
+ int sentLength;
381
+
382
+ memset (& msgHdr, 0, sizeof (struct msghdr));
383
+
384
+ if (address != NULL)
385
+ {
386
+ memset (& sin, 0, sizeof (struct sockaddr_in));
387
+
388
+ sin.sin_family = AF_INET;
389
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
390
+ sin.sin_addr.s_addr = address -> host;
391
+
392
+ msgHdr.msg_name = & sin;
393
+ msgHdr.msg_namelen = sizeof (struct sockaddr_in);
394
+ }
395
+
396
+ msgHdr.msg_iov = (struct iovec *) buffers;
397
+ msgHdr.msg_iovlen = bufferCount;
398
+
399
+ sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
400
+
401
+ if (sentLength == -1)
402
+ {
403
+ if (errno == EWOULDBLOCK)
404
+ return 0;
405
+
406
+ return -1;
407
+ }
408
+
409
+ return sentLength;
410
+ }
411
+
412
+ int
413
+ enet_socket_receive (ENetSocket socket,
414
+ ENetAddress * address,
415
+ ENetBuffer * buffers,
416
+ size_t bufferCount)
417
+ {
418
+ struct msghdr msgHdr;
419
+ struct sockaddr_in sin;
420
+ int recvLength;
421
+
422
+ memset (& msgHdr, 0, sizeof (struct msghdr));
423
+
424
+ if (address != NULL)
425
+ {
426
+ msgHdr.msg_name = & sin;
427
+ msgHdr.msg_namelen = sizeof (struct sockaddr_in);
428
+ }
429
+
430
+ msgHdr.msg_iov = (struct iovec *) buffers;
431
+ msgHdr.msg_iovlen = bufferCount;
432
+
433
+ recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
434
+
435
+ if (recvLength == -1)
436
+ {
437
+ if (errno == EWOULDBLOCK)
438
+ return 0;
439
+
440
+ return -1;
441
+ }
442
+
443
+ #ifdef HAS_MSGHDR_FLAGS
444
+ if (msgHdr.msg_flags & MSG_TRUNC)
445
+ return -1;
446
+ #endif
447
+
448
+ if (address != NULL)
449
+ {
450
+ address -> host = (enet_uint32) sin.sin_addr.s_addr;
451
+ address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
452
+ }
453
+
454
+ return recvLength;
455
+ }
456
+
457
+ int
458
+ enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
459
+ {
460
+ struct timeval timeVal;
461
+
462
+ timeVal.tv_sec = timeout / 1000;
463
+ timeVal.tv_usec = (timeout % 1000) * 1000;
464
+
465
+ return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
466
+ }
467
+
468
+ int
469
+ enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
470
+ {
471
+ #ifdef HAS_POLL
472
+ struct pollfd pollSocket;
473
+ int pollCount;
474
+
475
+ pollSocket.fd = socket;
476
+ pollSocket.events = 0;
477
+
478
+ if (* condition & ENET_SOCKET_WAIT_SEND)
479
+ pollSocket.events |= POLLOUT;
480
+
481
+ if (* condition & ENET_SOCKET_WAIT_RECEIVE)
482
+ pollSocket.events |= POLLIN;
483
+
484
+ pollCount = poll (& pollSocket, 1, timeout);
485
+
486
+ if (pollCount < 0)
487
+ {
488
+ if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
489
+ {
490
+ * condition = ENET_SOCKET_WAIT_INTERRUPT;
491
+
492
+ return 0;
493
+ }
494
+
495
+ return -1;
496
+ }
497
+
498
+ * condition = ENET_SOCKET_WAIT_NONE;
499
+
500
+ if (pollCount == 0)
501
+ return 0;
502
+
503
+ if (pollSocket.revents & POLLOUT)
504
+ * condition |= ENET_SOCKET_WAIT_SEND;
505
+
506
+ if (pollSocket.revents & POLLIN)
507
+ * condition |= ENET_SOCKET_WAIT_RECEIVE;
508
+
509
+ return 0;
510
+ #else
511
+ fd_set readSet, writeSet;
512
+ struct timeval timeVal;
513
+ int selectCount;
514
+
515
+ timeVal.tv_sec = timeout / 1000;
516
+ timeVal.tv_usec = (timeout % 1000) * 1000;
517
+
518
+ FD_ZERO (& readSet);
519
+ FD_ZERO (& writeSet);
520
+
521
+ if (* condition & ENET_SOCKET_WAIT_SEND)
522
+ FD_SET (socket, & writeSet);
523
+
524
+ if (* condition & ENET_SOCKET_WAIT_RECEIVE)
525
+ FD_SET (socket, & readSet);
526
+
527
+ selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
528
+
529
+ if (selectCount < 0)
530
+ {
531
+ if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
532
+ {
533
+ * condition = ENET_SOCKET_WAIT_INTERRUPT;
534
+
535
+ return 0;
536
+ }
537
+
538
+ return -1;
539
+ }
540
+
541
+ * condition = ENET_SOCKET_WAIT_NONE;
542
+
543
+ if (selectCount == 0)
544
+ return 0;
545
+
546
+ if (FD_ISSET (socket, & writeSet))
547
+ * condition |= ENET_SOCKET_WAIT_SEND;
548
+
549
+ if (FD_ISSET (socket, & readSet))
550
+ * condition |= ENET_SOCKET_WAIT_RECEIVE;
551
+
552
+ return 0;
553
+ #endif
554
+ }
555
+
556
+ #endif
557
+