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.
@@ -0,0 +1,348 @@
1
+ /**
2
+ @file win32.c
3
+ @brief ENet Win32 system specific functions
4
+ */
5
+ #ifdef WIN32
6
+
7
+ #include <time.h>
8
+ #define ENET_BUILDING_LIB 1
9
+ #include "enet/enet.h"
10
+
11
+ static enet_uint32 timeBase = 0;
12
+
13
+ int
14
+ enet_initialize (void)
15
+ {
16
+ WORD versionRequested = MAKEWORD (1, 1);
17
+ WSADATA wsaData;
18
+
19
+ if (WSAStartup (versionRequested, & wsaData))
20
+ return -1;
21
+
22
+ if (LOBYTE (wsaData.wVersion) != 1||
23
+ HIBYTE (wsaData.wVersion) != 1)
24
+ {
25
+ WSACleanup ();
26
+
27
+ return -1;
28
+ }
29
+
30
+ timeBeginPeriod (1);
31
+
32
+ return 0;
33
+ }
34
+
35
+ void
36
+ enet_deinitialize (void)
37
+ {
38
+ timeEndPeriod (1);
39
+
40
+ WSACleanup ();
41
+ }
42
+
43
+ enet_uint32
44
+ enet_time_get (void)
45
+ {
46
+ return (enet_uint32) timeGetTime () - timeBase;
47
+ }
48
+
49
+ void
50
+ enet_time_set (enet_uint32 newTimeBase)
51
+ {
52
+ timeBase = (enet_uint32) timeGetTime () - newTimeBase;
53
+ }
54
+
55
+ int
56
+ enet_address_set_host (ENetAddress * address, const char * name)
57
+ {
58
+ struct hostent * hostEntry;
59
+
60
+ hostEntry = gethostbyname (name);
61
+ if (hostEntry == NULL ||
62
+ hostEntry -> h_addrtype != AF_INET)
63
+ {
64
+ unsigned long host = inet_addr (name);
65
+ if (host == INADDR_NONE)
66
+ return -1;
67
+ address -> host = host;
68
+ return 0;
69
+ }
70
+
71
+ address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
72
+
73
+ return 0;
74
+ }
75
+
76
+ int
77
+ enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
78
+ {
79
+ char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
80
+ if (addr == NULL)
81
+ return -1;
82
+ strncpy (name, addr, nameLength);
83
+ return 0;
84
+ }
85
+
86
+ int
87
+ enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
88
+ {
89
+ struct in_addr in;
90
+ struct hostent * hostEntry;
91
+
92
+ in.s_addr = address -> host;
93
+
94
+ hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
95
+ if (hostEntry == NULL)
96
+ return enet_address_get_host_ip (address, name, nameLength);
97
+
98
+ strncpy (name, hostEntry -> h_name, nameLength);
99
+
100
+ return 0;
101
+ }
102
+
103
+ int
104
+ enet_socket_bind (ENetSocket socket, const ENetAddress * address)
105
+ {
106
+ struct sockaddr_in sin;
107
+
108
+ memset (& sin, 0, sizeof (struct sockaddr_in));
109
+
110
+ sin.sin_family = AF_INET;
111
+
112
+ if (address != NULL)
113
+ {
114
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
115
+ sin.sin_addr.s_addr = address -> host;
116
+ }
117
+ else
118
+ {
119
+ sin.sin_port = 0;
120
+ sin.sin_addr.s_addr = INADDR_ANY;
121
+ }
122
+
123
+ return bind (socket,
124
+ (struct sockaddr *) & sin,
125
+ sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
126
+ }
127
+
128
+ int
129
+ enet_socket_listen (ENetSocket socket, int backlog)
130
+ {
131
+ return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
132
+ }
133
+
134
+ ENetSocket
135
+ enet_socket_create (ENetSocketType type)
136
+ {
137
+ return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
138
+ }
139
+
140
+ int
141
+ enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
142
+ {
143
+ int result = SOCKET_ERROR;
144
+ switch (option)
145
+ {
146
+ case ENET_SOCKOPT_NONBLOCK:
147
+ {
148
+ u_long nonBlocking = (u_long) value;
149
+ result = ioctlsocket (socket, FIONBIO, & nonBlocking);
150
+ break;
151
+ }
152
+
153
+ case ENET_SOCKOPT_BROADCAST:
154
+ result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
155
+ break;
156
+
157
+ case ENET_SOCKOPT_REUSEADDR:
158
+ result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
159
+ break;
160
+
161
+ case ENET_SOCKOPT_RCVBUF:
162
+ result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
163
+ break;
164
+
165
+ case ENET_SOCKOPT_SNDBUF:
166
+ result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
167
+ break;
168
+
169
+ default:
170
+ break;
171
+ }
172
+ return result == SOCKET_ERROR ? -1 : 0;
173
+ }
174
+
175
+ int
176
+ enet_socket_connect (ENetSocket socket, const ENetAddress * address)
177
+ {
178
+ struct sockaddr_in sin;
179
+
180
+ memset (& sin, 0, sizeof (struct sockaddr_in));
181
+
182
+ sin.sin_family = AF_INET;
183
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
184
+ sin.sin_addr.s_addr = address -> host;
185
+
186
+ return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
187
+ }
188
+
189
+ ENetSocket
190
+ enet_socket_accept (ENetSocket socket, ENetAddress * address)
191
+ {
192
+ SOCKET result;
193
+ struct sockaddr_in sin;
194
+ int sinLength = sizeof (struct sockaddr_in);
195
+
196
+ result = accept (socket,
197
+ address != NULL ? (struct sockaddr *) & sin : NULL,
198
+ address != NULL ? & sinLength : NULL);
199
+
200
+ if (result == INVALID_SOCKET)
201
+ return ENET_SOCKET_NULL;
202
+
203
+ if (address != NULL)
204
+ {
205
+ address -> host = (enet_uint32) sin.sin_addr.s_addr;
206
+ address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
207
+ }
208
+
209
+ return result;
210
+ }
211
+
212
+ void
213
+ enet_socket_destroy (ENetSocket socket)
214
+ {
215
+ closesocket (socket);
216
+ }
217
+
218
+ int
219
+ enet_socket_send (ENetSocket socket,
220
+ const ENetAddress * address,
221
+ const ENetBuffer * buffers,
222
+ size_t bufferCount)
223
+ {
224
+ struct sockaddr_in sin;
225
+ DWORD sentLength;
226
+
227
+ if (address != NULL)
228
+ {
229
+ memset (& sin, 0, sizeof (struct sockaddr_in));
230
+
231
+ sin.sin_family = AF_INET;
232
+ sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
233
+ sin.sin_addr.s_addr = address -> host;
234
+ }
235
+
236
+ if (WSASendTo (socket,
237
+ (LPWSABUF) buffers,
238
+ (DWORD) bufferCount,
239
+ & sentLength,
240
+ 0,
241
+ address != NULL ? (struct sockaddr *) & sin : 0,
242
+ address != NULL ? sizeof (struct sockaddr_in) : 0,
243
+ NULL,
244
+ NULL) == SOCKET_ERROR)
245
+ {
246
+ if (WSAGetLastError () == WSAEWOULDBLOCK)
247
+ return 0;
248
+
249
+ return -1;
250
+ }
251
+
252
+ return (int) sentLength;
253
+ }
254
+
255
+ int
256
+ enet_socket_receive (ENetSocket socket,
257
+ ENetAddress * address,
258
+ ENetBuffer * buffers,
259
+ size_t bufferCount)
260
+ {
261
+ INT sinLength = sizeof (struct sockaddr_in);
262
+ DWORD flags = 0,
263
+ recvLength;
264
+ struct sockaddr_in sin;
265
+
266
+ if (WSARecvFrom (socket,
267
+ (LPWSABUF) buffers,
268
+ (DWORD) bufferCount,
269
+ & recvLength,
270
+ & flags,
271
+ address != NULL ? (struct sockaddr *) & sin : NULL,
272
+ address != NULL ? & sinLength : NULL,
273
+ NULL,
274
+ NULL) == SOCKET_ERROR)
275
+ {
276
+ switch (WSAGetLastError ())
277
+ {
278
+ case WSAEWOULDBLOCK:
279
+ case WSAECONNRESET:
280
+ return 0;
281
+ }
282
+
283
+ return -1;
284
+ }
285
+
286
+ if (flags & MSG_PARTIAL)
287
+ return -1;
288
+
289
+ if (address != NULL)
290
+ {
291
+ address -> host = (enet_uint32) sin.sin_addr.s_addr;
292
+ address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
293
+ }
294
+
295
+ return (int) recvLength;
296
+ }
297
+
298
+ int
299
+ enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
300
+ {
301
+ struct timeval timeVal;
302
+
303
+ timeVal.tv_sec = timeout / 1000;
304
+ timeVal.tv_usec = (timeout % 1000) * 1000;
305
+
306
+ return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
307
+ }
308
+
309
+ int
310
+ enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
311
+ {
312
+ fd_set readSet, writeSet;
313
+ struct timeval timeVal;
314
+ int selectCount;
315
+
316
+ timeVal.tv_sec = timeout / 1000;
317
+ timeVal.tv_usec = (timeout % 1000) * 1000;
318
+
319
+ FD_ZERO (& readSet);
320
+ FD_ZERO (& writeSet);
321
+
322
+ if (* condition & ENET_SOCKET_WAIT_SEND)
323
+ FD_SET (socket, & writeSet);
324
+
325
+ if (* condition & ENET_SOCKET_WAIT_RECEIVE)
326
+ FD_SET (socket, & readSet);
327
+
328
+ selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
329
+
330
+ if (selectCount < 0)
331
+ return -1;
332
+
333
+ * condition = ENET_SOCKET_WAIT_NONE;
334
+
335
+ if (selectCount == 0)
336
+ return 0;
337
+
338
+ if (FD_ISSET (socket, & writeSet))
339
+ * condition |= ENET_SOCKET_WAIT_SEND;
340
+
341
+ if (FD_ISSET (socket, & readSet))
342
+ * condition |= ENET_SOCKET_WAIT_RECEIVE;
343
+
344
+ return 0;
345
+ }
346
+
347
+ #endif
348
+
@@ -0,0 +1,58 @@
1
+ /**
2
+ @file win32.h
3
+ @brief ENet Win32 header
4
+ */
5
+ #ifndef __ENET_WIN32_H__
6
+ #define __ENET_WIN32_H__
7
+
8
+ #ifdef ENET_BUILDING_LIB
9
+ #pragma warning (disable: 4996) // 'strncpy' was declared deprecated
10
+ #pragma warning (disable: 4267) // size_t to int conversion
11
+ #pragma warning (disable: 4244) // 64bit to 32bit int
12
+ #pragma warning (disable: 4018) // signed/unsigned mismatch
13
+ #endif
14
+
15
+ #include <stdlib.h>
16
+ #include <winsock2.h>
17
+
18
+ typedef SOCKET ENetSocket;
19
+
20
+ enum
21
+ {
22
+ ENET_SOCKET_NULL = INVALID_SOCKET
23
+ };
24
+
25
+ #define ENET_HOST_TO_NET_16(value) (htons (value))
26
+ #define ENET_HOST_TO_NET_32(value) (htonl (value))
27
+
28
+ #define ENET_NET_TO_HOST_16(value) (ntohs (value))
29
+ #define ENET_NET_TO_HOST_32(value) (ntohl (value))
30
+
31
+ typedef struct
32
+ {
33
+ size_t dataLength;
34
+ void * data;
35
+ } ENetBuffer;
36
+
37
+ #define ENET_CALLBACK __cdecl
38
+
39
+ #if defined ENET_DLL
40
+ #if defined ENET_BUILDING_LIB
41
+ #define ENET_API __declspec( dllexport )
42
+ #else
43
+ #define ENET_API __declspec( dllimport )
44
+ #endif /* ENET_BUILDING_LIB */
45
+ #else /* !ENET_DLL */
46
+ #define ENET_API extern
47
+ #endif /* ENET_DLL */
48
+
49
+ typedef fd_set ENetSocketSet;
50
+
51
+ #define ENET_SOCKETSET_EMPTY(sockset) FD_ZERO (& (sockset))
52
+ #define ENET_SOCKETSET_ADD(sockset, socket) FD_SET (socket, & (sockset))
53
+ #define ENET_SOCKETSET_REMOVE(sockset, socket) FD_CLEAR (socket, & (sockset))
54
+ #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET (socket, & (sockset))
55
+
56
+ #endif /* __ENET_WIN32_H__ */
57
+
58
+
@@ -0,0 +1,21 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ extension_name = 'renet'
5
+
6
+ # Add the Enet library dependency
7
+ #have_library('enet')
8
+
9
+ # The destination
10
+ dir_config(extension_name)
11
+
12
+ # Do the work
13
+ create_makefile(extension_name)
14
+
15
+ sh "make"
16
+
17
+ if `uname`.chomp == 'Darwin'
18
+ `ln -s renet.bundle ../../lib/renet.bundle`
19
+ else
20
+ `ln -s linux/renet.so ../../lib/renet.so`
21
+ end
data/ext/renet/renet.c ADDED
@@ -0,0 +1,69 @@
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.h"
20
+
21
+ VALUE mENet;
22
+
23
+ void Init_renet()
24
+ {
25
+ mENet = rb_define_module("ENet");
26
+ rb_define_singleton_method(mENet, "initialize", renet_main_initialize, 0);
27
+ rb_define_singleton_method(mENet, "deinitialize", renet_main_deinitialize, 0);
28
+ rb_cv_set(mENet, "@@initialized", Qfalse);
29
+ /*Init_Constants();*/
30
+ init_renet_connection();
31
+ init_renet_server();
32
+ rb_define_const(mENet, "ENET_VERSION", rb_str_new2("1.3.0"));
33
+ rb_define_const(mENet, "RENET_VERSION", rb_str_new2("0.1"));
34
+ }
35
+
36
+ VALUE renet_main_initialize(VALUE self)
37
+ {
38
+ if (rb_cv_get(mENet, "@@initialized") == Qfalse)
39
+ {
40
+ if (enet_initialize() != 0)
41
+ {
42
+ return Qfalse;
43
+ }
44
+ rb_cv_set(mENet, "@@initialized", Qtrue);
45
+ return Qtrue;
46
+ }
47
+ }
48
+
49
+ VALUE renet_main_deinitialize(VALUE self)
50
+ {
51
+ if (rb_cv_get(mENet, "@@initialized") == Qtrue)
52
+ {
53
+ enet_deinitialize();
54
+ }
55
+ rb_cv_set(mENet, "@@initialized", Qfalse);
56
+ return Qtrue;
57
+ }
58
+ /*
59
+ void Init_Constants()
60
+ {
61
+ rb_define_const(mENet, "ENET_HOST_ANY", UINT2NUM(ENET_HOST_ANY));
62
+ rb_define_const(mENet, "ENET_HOST_BROADCAST", UINT2NUM(ENET_HOST_BROADCAST));
63
+ rb_define_const(mENet, "ENET_PORT_ANY", UINT2NUM(ENET_PORT_ANY));
64
+
65
+ rb_define_const(mENet, "ENET_PACKET_FLAG_RELIABLE", UINT2NUM(ENET_PACKET_FLAG_RELIABLE));
66
+ rb_define_const(mENet, "ENET_PACKET_FLAG_UNSEQUENCED", UINT2NUM(ENET_PACKET_FLAG_UNSEQUENCED));
67
+ rb_define_const(mENet, "ENET_PACKET_FLAG_NO_ALLOCATE", UINT2NUM(ENET_PACKET_FLAG_NO_ALLOCATE));
68
+ }
69
+ */
data/ext/renet/renet.h ADDED
@@ -0,0 +1,35 @@
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
20
+ #define RUBY_ENET
21
+
22
+ #include <ruby.h>
23
+ #include <enet/enet.h>
24
+
25
+ #include "renet_connection.h"
26
+ #include "renet_server.h"
27
+
28
+ extern VALUE mENet;
29
+
30
+ void Init_renet();
31
+ /*void Init_Constants();*/
32
+ VALUE renet_main_initialize(VALUE self);
33
+ VALUE renet_main_deinitialize(VALUE self);
34
+
35
+ #endif