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 (2, 2); |
---|
17 | WSADATA wsaData; |
---|
18 | |
---|
19 | if (WSAStartup (versionRequested, & wsaData)) |
---|
20 | return -1; |
---|
21 | |
---|
22 | if (LOBYTE (wsaData.wVersion) != 2|| |
---|
23 | HIBYTE (wsaData.wVersion) != 2) |
---|
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_ = { ENET_HOST_ANY_INIT, 0, 0 }; |
---|
184 | enet_address_set_sin((SOCKADDR *) & sin, & address_, family); |
---|
185 | } |
---|
186 | |
---|
187 | return bind (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
188 | } |
---|
189 | |
---|
190 | int |
---|
191 | enet_socket_listen (ENetSocket socket, int backlog) |
---|
192 | { |
---|
193 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0; |
---|
194 | } |
---|
195 | |
---|
196 | ENetSocket |
---|
197 | enet_socket_create (ENetSocketType type, ENetAddressFamily family) |
---|
198 | { |
---|
199 | return socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); |
---|
200 | } |
---|
201 | |
---|
202 | int |
---|
203 | enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value) |
---|
204 | { |
---|
205 | int result = SOCKET_ERROR; |
---|
206 | switch (option) |
---|
207 | { |
---|
208 | case ENET_SOCKOPT_NONBLOCK: |
---|
209 | { |
---|
210 | u_long nonBlocking = (u_long) value; |
---|
211 | result = ioctlsocket (socket, FIONBIO, & nonBlocking); |
---|
212 | break; |
---|
213 | } |
---|
214 | |
---|
215 | case ENET_SOCKOPT_BROADCAST: |
---|
216 | result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int)); |
---|
217 | break; |
---|
218 | |
---|
219 | case ENET_SOCKOPT_REUSEADDR: |
---|
220 | result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int)); |
---|
221 | break; |
---|
222 | |
---|
223 | case ENET_SOCKOPT_RCVBUF: |
---|
224 | result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int)); |
---|
225 | break; |
---|
226 | |
---|
227 | case ENET_SOCKOPT_SNDBUF: |
---|
228 | result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int)); |
---|
229 | break; |
---|
230 | |
---|
231 | default: |
---|
232 | break; |
---|
233 | } |
---|
234 | return result == SOCKET_ERROR ? -1 : 0; |
---|
235 | } |
---|
236 | |
---|
237 | int |
---|
238 | enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
239 | { |
---|
240 | SOCKADDR_STORAGE sin; |
---|
241 | enet_address_set_sin((SOCKADDR *) & sin, address, family); |
---|
242 | |
---|
243 | return connect (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
244 | } |
---|
245 | |
---|
246 | ENetSocket |
---|
247 | enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) |
---|
248 | { |
---|
249 | SOCKET result; |
---|
250 | SOCKADDR_STORAGE sin; |
---|
251 | int sinLength = enet_sa_size (family); |
---|
252 | |
---|
253 | result = accept (socket, |
---|
254 | address != NULL ? (SOCKADDR *) & sin : NULL, |
---|
255 | address != NULL ? & sinLength : NULL); |
---|
256 | |
---|
257 | if (result == INVALID_SOCKET) |
---|
258 | return ENET_SOCKET_NULL; |
---|
259 | |
---|
260 | if (address != NULL) |
---|
261 | { |
---|
262 | enet_address_set_address(address, (SOCKADDR *) & sin); |
---|
263 | } |
---|
264 | |
---|
265 | return result; |
---|
266 | } |
---|
267 | |
---|
268 | void |
---|
269 | enet_socket_destroy (ENetSocket socket) |
---|
270 | { |
---|
271 | closesocket (socket); |
---|
272 | } |
---|
273 | |
---|
274 | int |
---|
275 | enet_socket_send (ENetSocket socket, |
---|
276 | const ENetAddress * address, |
---|
277 | const ENetBuffer * buffers, |
---|
278 | size_t bufferCount, |
---|
279 | ENetAddressFamily family) |
---|
280 | { |
---|
281 | SOCKADDR_STORAGE sin; |
---|
282 | DWORD sentLength; |
---|
283 | |
---|
284 | if (address != NULL) |
---|
285 | { |
---|
286 | enet_address_set_sin((SOCKADDR *) & sin, address, family); |
---|
287 | } |
---|
288 | |
---|
289 | if (WSASendTo (socket, |
---|
290 | (LPWSABUF) buffers, |
---|
291 | (DWORD) bufferCount, |
---|
292 | & sentLength, |
---|
293 | 0, |
---|
294 | address != NULL ? (SOCKADDR *) & sin : 0, |
---|
295 | address != NULL ? enet_sa_size (family) : 0, |
---|
296 | NULL, |
---|
297 | NULL) == SOCKET_ERROR) |
---|
298 | { |
---|
299 | if (WSAGetLastError () == WSAEWOULDBLOCK) |
---|
300 | return 0; |
---|
301 | |
---|
302 | return -1; |
---|
303 | } |
---|
304 | |
---|
305 | return (int) sentLength; |
---|
306 | } |
---|
307 | |
---|
308 | int |
---|
309 | enet_socket_receive (ENetSocket socket, |
---|
310 | ENetAddress * address, |
---|
311 | ENetBuffer * buffers, |
---|
312 | size_t bufferCount, |
---|
313 | ENetAddressFamily family) |
---|
314 | { |
---|
315 | INT sinLength = enet_sa_size (family); |
---|
316 | DWORD flags = 0, |
---|
317 | recvLength; |
---|
318 | SOCKADDR_STORAGE sin; |
---|
319 | |
---|
320 | if (WSARecvFrom (socket, |
---|
321 | (LPWSABUF) buffers, |
---|
322 | (DWORD) bufferCount, |
---|
323 | & recvLength, |
---|
324 | & flags, |
---|
325 | address != NULL ? (SOCKADDR *) & sin : NULL, |
---|
326 | address != NULL ? & sinLength : NULL, |
---|
327 | NULL, |
---|
328 | NULL) == SOCKET_ERROR) |
---|
329 | { |
---|
330 | switch (WSAGetLastError ()) |
---|
331 | { |
---|
332 | case WSAEWOULDBLOCK: |
---|
333 | case WSAECONNRESET: |
---|
334 | return 0; |
---|
335 | } |
---|
336 | |
---|
337 | return -1; |
---|
338 | } |
---|
339 | |
---|
340 | if (flags & MSG_PARTIAL) |
---|
341 | return -1; |
---|
342 | |
---|
343 | if (address != NULL) |
---|
344 | { |
---|
345 | enet_address_set_address(address, (SOCKADDR *) & sin); |
---|
346 | } |
---|
347 | |
---|
348 | return (int) recvLength; |
---|
349 | } |
---|
350 | |
---|
351 | int |
---|
352 | enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout) |
---|
353 | { |
---|
354 | struct timeval timeVal; |
---|
355 | |
---|
356 | timeVal.tv_sec = timeout / 1000; |
---|
357 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
358 | |
---|
359 | return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal); |
---|
360 | } |
---|
361 | |
---|
362 | int |
---|
363 | enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) |
---|
364 | { |
---|
365 | fd_set readSet, writeSet; |
---|
366 | struct timeval timeVal; |
---|
367 | int selectCount; |
---|
368 | ENetSocket maxSocket; |
---|
369 | |
---|
370 | timeVal.tv_sec = timeout / 1000; |
---|
371 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
372 | |
---|
373 | FD_ZERO (& readSet); |
---|
374 | FD_ZERO (& writeSet); |
---|
375 | |
---|
376 | if (* condition & ENET_SOCKET_WAIT_SEND) |
---|
377 | { |
---|
378 | if (socket4 != ENET_SOCKET_NULL) |
---|
379 | FD_SET (socket4, & writeSet); |
---|
380 | if (socket6 != ENET_SOCKET_NULL) |
---|
381 | FD_SET (socket6, & writeSet); |
---|
382 | } |
---|
383 | |
---|
384 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) |
---|
385 | { |
---|
386 | if (socket4 != ENET_SOCKET_NULL) |
---|
387 | FD_SET (socket4, & readSet); |
---|
388 | if (socket6 != ENET_SOCKET_NULL) |
---|
389 | FD_SET (socket6, & readSet); |
---|
390 | } |
---|
391 | |
---|
392 | maxSocket = 0; |
---|
393 | if (socket4 != ENET_SOCKET_NULL) |
---|
394 | maxSocket = socket4; |
---|
395 | if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) |
---|
396 | maxSocket = socket6; |
---|
397 | |
---|
398 | selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); |
---|
399 | |
---|
400 | if (selectCount < 0) |
---|
401 | return -1; |
---|
402 | |
---|
403 | * condition = ENET_SOCKET_WAIT_NONE; |
---|
404 | |
---|
405 | if (selectCount == 0) |
---|
406 | return 0; |
---|
407 | |
---|
408 | if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || |
---|
409 | (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) |
---|
410 | * condition |= ENET_SOCKET_WAIT_SEND; |
---|
411 | |
---|
412 | if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || |
---|
413 | (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) |
---|
414 | * condition |= ENET_SOCKET_WAIT_RECEIVE; |
---|
415 | |
---|
416 | return 0; |
---|
417 | } |
---|
418 | |
---|
419 | #endif |
---|
420 | |
---|