[7328] | 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 | { |
---|
[7394] | 16 | WORD versionRequested = MAKEWORD (2, 2); |
---|
[7328] | 17 | WSADATA wsaData; |
---|
| 18 | |
---|
| 19 | if (WSAStartup (versionRequested, & wsaData)) |
---|
| 20 | return -1; |
---|
| 21 | |
---|
[7394] | 22 | if (LOBYTE (wsaData.wVersion) != 2|| |
---|
| 23 | HIBYTE (wsaData.wVersion) != 2) |
---|
[7328] | 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 | |
---|
[7390] | 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)); |
---|
[7392] | 82 | /* address -> scopeID = 0; */ |
---|
[7390] | 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 | |
---|
[7328] | 120 | int |
---|
| 121 | enet_address_set_host (ENetAddress * address, const char * name) |
---|
| 122 | { |
---|
[7390] | 123 | enet_uint16 port = address -> port; |
---|
| 124 | ADDRINFO hints; |
---|
| 125 | ADDRINFO * result; |
---|
| 126 | ADDRINFO * res; |
---|
[7328] | 127 | |
---|
[7390] | 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) |
---|
[7328] | 136 | { |
---|
[7390] | 137 | if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) |
---|
| 138 | break; |
---|
[7328] | 139 | } |
---|
| 140 | |
---|
[7390] | 141 | address -> port = port; |
---|
| 142 | freeaddrinfo(result); |
---|
| 143 | if (res == NULL) return -1; |
---|
[7328] | 144 | |
---|
| 145 | return 0; |
---|
| 146 | } |
---|
| 147 | |
---|
[7390] | 148 | static int |
---|
| 149 | enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) |
---|
[7328] | 150 | { |
---|
[7390] | 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)) |
---|
[7328] | 155 | return -1; |
---|
[7390] | 156 | |
---|
[7328] | 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | int |
---|
[7390] | 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 |
---|
[7328] | 167 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
---|
| 168 | { |
---|
[7390] | 169 | return enet_address_get_host_x(address, name, nameLength, 0); |
---|
[7328] | 170 | } |
---|
| 171 | |
---|
| 172 | int |
---|
[7390] | 173 | enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 174 | { |
---|
[7390] | 175 | SOCKADDR_STORAGE sin; |
---|
[7328] | 176 | |
---|
| 177 | if (address != NULL) |
---|
| 178 | { |
---|
[7390] | 179 | enet_address_set_sin((SOCKADDR *) & sin, address, family); |
---|
[7328] | 180 | } |
---|
| 181 | else |
---|
| 182 | { |
---|
[7397] | 183 | ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; |
---|
[7390] | 184 | enet_address_set_sin((SOCKADDR *) & sin, & address_, family); |
---|
[7328] | 185 | } |
---|
| 186 | |
---|
[7390] | 187 | return bind (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 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 |
---|
[7390] | 197 | enet_socket_create (ENetSocketType type, ENetAddressFamily family) |
---|
[7328] | 198 | { |
---|
[7392] | 199 | return socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); |
---|
[7328] | 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 |
---|
[7390] | 238 | enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 239 | { |
---|
[7390] | 240 | SOCKADDR_STORAGE sin; |
---|
| 241 | enet_address_set_sin((SOCKADDR *) & sin, address, family); |
---|
[7328] | 242 | |
---|
[7390] | 243 | return connect (socket, (SOCKADDR *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 244 | } |
---|
| 245 | |
---|
| 246 | ENetSocket |
---|
[7390] | 247 | enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 248 | { |
---|
| 249 | SOCKET result; |
---|
[7390] | 250 | SOCKADDR_STORAGE sin; |
---|
| 251 | int sinLength = enet_sa_size (family); |
---|
[7328] | 252 | |
---|
| 253 | result = accept (socket, |
---|
[7390] | 254 | address != NULL ? (SOCKADDR *) & sin : NULL, |
---|
[7328] | 255 | address != NULL ? & sinLength : NULL); |
---|
| 256 | |
---|
| 257 | if (result == INVALID_SOCKET) |
---|
| 258 | return ENET_SOCKET_NULL; |
---|
| 259 | |
---|
| 260 | if (address != NULL) |
---|
| 261 | { |
---|
[7390] | 262 | enet_address_set_address(address, (SOCKADDR *) & sin); |
---|
[7328] | 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, |
---|
[7390] | 278 | size_t bufferCount, |
---|
| 279 | ENetAddressFamily family) |
---|
[7328] | 280 | { |
---|
[7390] | 281 | SOCKADDR_STORAGE sin; |
---|
[7328] | 282 | DWORD sentLength; |
---|
| 283 | |
---|
| 284 | if (address != NULL) |
---|
| 285 | { |
---|
[7390] | 286 | enet_address_set_sin((SOCKADDR *) & sin, address, family); |
---|
[7328] | 287 | } |
---|
| 288 | |
---|
| 289 | if (WSASendTo (socket, |
---|
| 290 | (LPWSABUF) buffers, |
---|
| 291 | (DWORD) bufferCount, |
---|
| 292 | & sentLength, |
---|
| 293 | 0, |
---|
[7390] | 294 | address != NULL ? (SOCKADDR *) & sin : 0, |
---|
| 295 | address != NULL ? enet_sa_size (family) : 0, |
---|
[7328] | 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, |
---|
[7390] | 312 | size_t bufferCount, |
---|
| 313 | ENetAddressFamily family) |
---|
[7328] | 314 | { |
---|
[7390] | 315 | INT sinLength = enet_sa_size (family); |
---|
[7328] | 316 | DWORD flags = 0, |
---|
| 317 | recvLength; |
---|
[7390] | 318 | SOCKADDR_STORAGE sin; |
---|
[7328] | 319 | |
---|
| 320 | if (WSARecvFrom (socket, |
---|
| 321 | (LPWSABUF) buffers, |
---|
| 322 | (DWORD) bufferCount, |
---|
| 323 | & recvLength, |
---|
| 324 | & flags, |
---|
[7390] | 325 | address != NULL ? (SOCKADDR *) & sin : NULL, |
---|
[7328] | 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 | { |
---|
[7390] | 345 | enet_address_set_address(address, (SOCKADDR *) & sin); |
---|
[7328] | 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 |
---|
[7390] | 363 | enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) |
---|
[7328] | 364 | { |
---|
| 365 | fd_set readSet, writeSet; |
---|
| 366 | struct timeval timeVal; |
---|
| 367 | int selectCount; |
---|
[7391] | 368 | ENetSocket maxSocket; |
---|
[7390] | 369 | |
---|
[7328] | 370 | timeVal.tv_sec = timeout / 1000; |
---|
| 371 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
[7390] | 372 | |
---|
[7328] | 373 | FD_ZERO (& readSet); |
---|
| 374 | FD_ZERO (& writeSet); |
---|
| 375 | |
---|
| 376 | if (* condition & ENET_SOCKET_WAIT_SEND) |
---|
[7390] | 377 | { |
---|
| 378 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 379 | FD_SET (socket4, & writeSet); |
---|
| 380 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 381 | FD_SET (socket6, & writeSet); |
---|
| 382 | } |
---|
[7328] | 383 | |
---|
| 384 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) |
---|
[7390] | 385 | { |
---|
| 386 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 387 | FD_SET (socket4, & readSet); |
---|
| 388 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 389 | FD_SET (socket6, & readSet); |
---|
| 390 | } |
---|
[7328] | 391 | |
---|
[7391] | 392 | maxSocket = 0; |
---|
[7390] | 393 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 394 | maxSocket = socket4; |
---|
| 395 | if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) |
---|
| 396 | maxSocket = socket6; |
---|
[7328] | 397 | |
---|
[7390] | 398 | selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); |
---|
| 399 | |
---|
[7328] | 400 | if (selectCount < 0) |
---|
| 401 | return -1; |
---|
| 402 | |
---|
| 403 | * condition = ENET_SOCKET_WAIT_NONE; |
---|
| 404 | |
---|
| 405 | if (selectCount == 0) |
---|
| 406 | return 0; |
---|
| 407 | |
---|
[7390] | 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; |
---|
[7328] | 411 | |
---|
[7390] | 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 | |
---|
[7328] | 416 | return 0; |
---|
[7390] | 417 | } |
---|
[7328] | 418 | |
---|
| 419 | #endif |
---|
| 420 | |
---|