1 | /** |
---|
2 | @file win32.c |
---|
3 | @brief ENet Win32 system specific functions |
---|
4 | */ |
---|
5 | #ifdef WIN32 |
---|
6 | |
---|
7 | #define _WIN32_WINNT 0x0501 |
---|
8 | |
---|
9 | #include <time.h> |
---|
10 | #define ENET_BUILDING_LIB 1 |
---|
11 | #include "enet/enet.h" |
---|
12 | |
---|
13 | static enet_uint32 timeBase = 0; |
---|
14 | |
---|
15 | int |
---|
16 | enet_initialize (void) |
---|
17 | { |
---|
18 | WORD versionRequested = MAKEWORD (2, 2); |
---|
19 | WSADATA wsaData; |
---|
20 | |
---|
21 | if (WSAStartup (versionRequested, & wsaData)) |
---|
22 | return -1; |
---|
23 | |
---|
24 | if (LOBYTE (wsaData.wVersion) != 2|| |
---|
25 | HIBYTE (wsaData.wVersion) != 2) |
---|
26 | { |
---|
27 | WSACleanup (); |
---|
28 | |
---|
29 | return -1; |
---|
30 | } |
---|
31 | |
---|
32 | timeBeginPeriod (1); |
---|
33 | |
---|
34 | return 0; |
---|
35 | } |
---|
36 | |
---|
37 | void |
---|
38 | enet_deinitialize (void) |
---|
39 | { |
---|
40 | timeEndPeriod (1); |
---|
41 | |
---|
42 | WSACleanup (); |
---|
43 | } |
---|
44 | |
---|
45 | enet_uint32 |
---|
46 | enet_time_get (void) |
---|
47 | { |
---|
48 | return (enet_uint32) timeGetTime () - timeBase; |
---|
49 | } |
---|
50 | |
---|
51 | void |
---|
52 | enet_time_set (enet_uint32 newTimeBase) |
---|
53 | { |
---|
54 | timeBase = (enet_uint32) timeGetTime () - newTimeBase; |
---|
55 | } |
---|
56 | |
---|
57 | static enet_uint16 |
---|
58 | enet_af (ENetAddressFamily family) |
---|
59 | { |
---|
60 | if (family == ENET_IPV4) |
---|
61 | return AF_INET; |
---|
62 | if (family == ENET_IPV6) |
---|
63 | return AF_INET6; |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | static socklen_t |
---|
68 | enet_sa_size (ENetAddressFamily family) |
---|
69 | { |
---|
70 | if (family == ENET_IPV4) |
---|
71 | return sizeof (struct sockaddr_in); |
---|
72 | if (family == ENET_IPV6) |
---|
73 | return sizeof (struct sockaddr_in6); |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | static ENetAddressFamily |
---|
78 | enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) |
---|
79 | { |
---|
80 | memset (address, 0, sizeof (ENetAddress)); |
---|
81 | if (sin -> sa_family == AF_INET) |
---|
82 | { |
---|
83 | address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); |
---|
84 | /* address -> scopeID = 0; */ |
---|
85 | address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); |
---|
86 | return ENET_IPV4; |
---|
87 | } |
---|
88 | if (sin -> sa_family == AF_INET6) |
---|
89 | { |
---|
90 | address -> host = * (ENetHostAddress *) & ((struct sockaddr_in6 *) sin) -> sin6_addr; |
---|
91 | address -> scopeID = ((struct sockaddr_in6 *) sin) -> sin6_scope_id; |
---|
92 | address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in6 *) sin) -> sin6_port); |
---|
93 | return ENET_IPV6; |
---|
94 | } |
---|
95 | return ENET_NO_ADDRESS_FAMILY; |
---|
96 | } |
---|
97 | |
---|
98 | static int |
---|
99 | enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) |
---|
100 | { |
---|
101 | memset (sin, 0, enet_sa_size(family)); |
---|
102 | if (family == ENET_IPV4 && |
---|
103 | (enet_get_address_family (address) == ENET_IPV4 || |
---|
104 | !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) |
---|
105 | { |
---|
106 | ((struct sockaddr_in *) sin) -> sin_family = AF_INET; |
---|
107 | ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; |
---|
108 | ((struct sockaddr_in *) sin) -> sin_port = ENET_HOST_TO_NET_16 (address -> port); |
---|
109 | return 0; |
---|
110 | } |
---|
111 | else if (family == ENET_IPV6) |
---|
112 | { |
---|
113 | ((struct sockaddr_in6 *) sin) -> sin6_family = AF_INET6; |
---|
114 | ((struct sockaddr_in6 *) sin) -> sin6_addr = * (struct in6_addr *) & address -> host; |
---|
115 | ((struct sockaddr_in6 *) sin) -> sin6_scope_id = address -> scopeID; |
---|
116 | ((struct sockaddr_in6 *) sin) -> sin6_port = ENET_HOST_TO_NET_16 (address -> port); |
---|
117 | return 0; |
---|
118 | } |
---|
119 | return -1; |
---|
120 | } |
---|
121 | |
---|
122 | int |
---|
123 | enet_address_set_host (ENetAddress * address, const char * name) |
---|
124 | { |
---|
125 | enet_uint16 port = address -> port; |
---|
126 | struct addrinfo hints; |
---|
127 | struct addrinfo * result; |
---|
128 | struct addrinfo * res; |
---|
129 | |
---|
130 | memset(& hints, 0, sizeof (hints)); |
---|
131 | #ifdef AI_ADDRCONFIG |
---|
132 | hints.ai_flags = AI_ADDRCONFIG; |
---|
133 | #else |
---|
134 | hints.ai_flags = 0x0400; // AI_ADDRCONFIG is defined as 0x0400 |
---|
135 | #endif |
---|
136 | hints.ai_family = AF_UNSPEC; |
---|
137 | |
---|
138 | if ( getaddrinfo(name, NULL, &hints, &result) ) |
---|
139 | return -1; |
---|
140 | |
---|
141 | for (res = result; res != NULL; res = res -> ai_next) |
---|
142 | { |
---|
143 | if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) |
---|
144 | break; |
---|
145 | } |
---|
146 | |
---|
147 | address -> port = port; |
---|
148 | freeaddrinfo(result); |
---|
149 | if (res == NULL) return -1; |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | static int |
---|
155 | enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) |
---|
156 | { |
---|
157 | struct sockaddr_storage sin; |
---|
158 | enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); |
---|
159 | |
---|
160 | if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) |
---|
161 | return -1; |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | int |
---|
167 | enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) |
---|
168 | { |
---|
169 | return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); |
---|
170 | } |
---|
171 | |
---|
172 | int |
---|
173 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
---|
174 | { |
---|
175 | return enet_address_get_host_x(address, name, nameLength, 0); |
---|
176 | } |
---|
177 | |
---|
178 | int |
---|
179 | enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
180 | { |
---|
181 | struct sockaddr_storage sin; |
---|
182 | |
---|
183 | if (address != NULL) |
---|
184 | { |
---|
185 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; |
---|
190 | enet_address_set_sin((struct sockaddr *) & sin, & address_, family); |
---|
191 | } |
---|
192 | |
---|
193 | return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
194 | } |
---|
195 | |
---|
196 | int |
---|
197 | enet_socket_listen (ENetSocket socket, int backlog) |
---|
198 | { |
---|
199 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0; |
---|
200 | } |
---|
201 | |
---|
202 | ENetSocket |
---|
203 | enet_socket_create (ENetSocketType type, ENetAddressFamily family) |
---|
204 | { |
---|
205 | ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); |
---|
206 | return sock; |
---|
207 | } |
---|
208 | |
---|
209 | int |
---|
210 | enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) |
---|
211 | { |
---|
212 | int result = SOCKET_ERROR; |
---|
213 | switch (option) |
---|
214 | { |
---|
215 | case ENET_SOCKOPT_NONBLOCK: |
---|
216 | { |
---|
217 | u_long nonBlocking = (u_long) value; |
---|
218 | result = ioctlsocket (socket, FIONBIO, & nonBlocking); |
---|
219 | break; |
---|
220 | } |
---|
221 | |
---|
222 | case ENET_SOCKOPT_BROADCAST: |
---|
223 | result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); |
---|
224 | break; |
---|
225 | |
---|
226 | case ENET_SOCKOPT_REUSEADDR: |
---|
227 | result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int)); |
---|
228 | break; |
---|
229 | |
---|
230 | case ENET_SOCKOPT_RCVBUF: |
---|
231 | result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); |
---|
232 | break; |
---|
233 | |
---|
234 | case ENET_SOCKOPT_SNDBUF: |
---|
235 | result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); |
---|
236 | break; |
---|
237 | |
---|
238 | default: |
---|
239 | break; |
---|
240 | } |
---|
241 | return result == SOCKET_ERROR ? -1 : 0; |
---|
242 | } |
---|
243 | |
---|
244 | int |
---|
245 | enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
246 | { |
---|
247 | struct sockaddr_storage sin; |
---|
248 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
249 | |
---|
250 | return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0; |
---|
251 | } |
---|
252 | |
---|
253 | ENetSocket |
---|
254 | enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) |
---|
255 | { |
---|
256 | SOCKET result; |
---|
257 | struct sockaddr_storage sin; |
---|
258 | socklen_t sinLength = enet_sa_size (family); |
---|
259 | |
---|
260 | result = accept (socket, |
---|
261 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
262 | address != NULL ? & sinLength : NULL); |
---|
263 | |
---|
264 | if (result == INVALID_SOCKET) |
---|
265 | return ENET_SOCKET_NULL; |
---|
266 | |
---|
267 | if (address != NULL) |
---|
268 | { |
---|
269 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
270 | } |
---|
271 | |
---|
272 | return result; |
---|
273 | } |
---|
274 | |
---|
275 | void |
---|
276 | enet_socket_destroy (ENetSocket socket) |
---|
277 | { |
---|
278 | closesocket (socket); |
---|
279 | } |
---|
280 | |
---|
281 | int |
---|
282 | enet_socket_send (ENetSocket socket, |
---|
283 | const ENetAddress * address, |
---|
284 | const ENetBuffer * buffers, |
---|
285 | size_t bufferCount, |
---|
286 | ENetAddressFamily family) |
---|
287 | { |
---|
288 | struct sockaddr_storage sin; |
---|
289 | DWORD sentLength; |
---|
290 | |
---|
291 | if (address != NULL) |
---|
292 | { |
---|
293 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
294 | } |
---|
295 | |
---|
296 | if (WSASendTo (socket, |
---|
297 | (LPWSABUF) buffers, |
---|
298 | (DWORD) bufferCount, |
---|
299 | & sentLength, |
---|
300 | 0, |
---|
301 | address != NULL ? (struct sockaddr *) & sin : 0, |
---|
302 | address != NULL ? enet_sa_size (family) : 0, |
---|
303 | NULL, |
---|
304 | NULL) == SOCKET_ERROR) |
---|
305 | { |
---|
306 | if (WSAGetLastError () == WSAEWOULDBLOCK) |
---|
307 | return 0; |
---|
308 | |
---|
309 | return -1; |
---|
310 | } |
---|
311 | |
---|
312 | return (int) sentLength; |
---|
313 | } |
---|
314 | |
---|
315 | int |
---|
316 | enet_socket_receive (ENetSocket socket, |
---|
317 | ENetAddress * address, |
---|
318 | ENetBuffer * buffers, |
---|
319 | size_t bufferCount, |
---|
320 | ENetAddressFamily family) |
---|
321 | { |
---|
322 | INT sinLength = enet_sa_size (family); |
---|
323 | DWORD flags = 0, |
---|
324 | recvLength; |
---|
325 | struct sockaddr_storage sin; |
---|
326 | |
---|
327 | if (WSARecvFrom (socket, |
---|
328 | (LPWSABUF) buffers, |
---|
329 | (DWORD) bufferCount, |
---|
330 | & recvLength, |
---|
331 | & flags, |
---|
332 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
333 | address != NULL ? & sinLength : NULL, |
---|
334 | NULL, |
---|
335 | NULL) == SOCKET_ERROR) |
---|
336 | { |
---|
337 | switch (WSAGetLastError ()) |
---|
338 | { |
---|
339 | case WSAEWOULDBLOCK: |
---|
340 | case WSAECONNRESET: |
---|
341 | return 0; |
---|
342 | } |
---|
343 | |
---|
344 | return -1; |
---|
345 | } |
---|
346 | |
---|
347 | if (flags & MSG_PARTIAL) |
---|
348 | return -1; |
---|
349 | |
---|
350 | if (address != NULL) |
---|
351 | { |
---|
352 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
353 | } |
---|
354 | |
---|
355 | return (int) recvLength; |
---|
356 | } |
---|
357 | |
---|
358 | int |
---|
359 | enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout) |
---|
360 | { |
---|
361 | struct timeval timeVal; |
---|
362 | |
---|
363 | timeVal.tv_sec = timeout / 1000; |
---|
364 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
365 | |
---|
366 | return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal); |
---|
367 | } |
---|
368 | |
---|
369 | int |
---|
370 | enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) |
---|
371 | { |
---|
372 | fd_set readSet, writeSet; |
---|
373 | struct timeval timeVal; |
---|
374 | int selectCount; |
---|
375 | ENetSocket maxSocket; |
---|
376 | |
---|
377 | timeVal.tv_sec = timeout / 1000; |
---|
378 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
379 | |
---|
380 | FD_ZERO (& readSet); |
---|
381 | FD_ZERO (& writeSet); |
---|
382 | |
---|
383 | if (* condition & ENET_SOCKET_WAIT_SEND) |
---|
384 | { |
---|
385 | if (socket4 != ENET_SOCKET_NULL) |
---|
386 | FD_SET (socket4, & writeSet); |
---|
387 | if (socket6 != ENET_SOCKET_NULL) |
---|
388 | FD_SET (socket6, & writeSet); |
---|
389 | } |
---|
390 | |
---|
391 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) |
---|
392 | { |
---|
393 | if (socket4 != ENET_SOCKET_NULL) |
---|
394 | FD_SET (socket4, & readSet); |
---|
395 | if (socket6 != ENET_SOCKET_NULL) |
---|
396 | FD_SET (socket6, & readSet); |
---|
397 | } |
---|
398 | |
---|
399 | maxSocket = 0; |
---|
400 | if (socket4 != ENET_SOCKET_NULL) |
---|
401 | maxSocket = socket4; |
---|
402 | if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) |
---|
403 | maxSocket = socket6; |
---|
404 | |
---|
405 | selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); |
---|
406 | |
---|
407 | if (selectCount < 0) |
---|
408 | return -1; |
---|
409 | |
---|
410 | * condition = ENET_SOCKET_WAIT_NONE; |
---|
411 | |
---|
412 | if (selectCount == 0) |
---|
413 | return 0; |
---|
414 | |
---|
415 | if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || |
---|
416 | (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) |
---|
417 | * condition |= ENET_SOCKET_WAIT_SEND; |
---|
418 | |
---|
419 | if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || |
---|
420 | (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) |
---|
421 | * condition |= ENET_SOCKET_WAIT_RECEIVE; |
---|
422 | |
---|
423 | return 0; |
---|
424 | } |
---|
425 | |
---|
426 | #endif |
---|
427 | |
---|