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