[7328] | 1 | /** |
---|
| 2 | @file win32.c |
---|
| 3 | @brief ENet Win32 system specific functions |
---|
| 4 | */ |
---|
| 5 | #ifdef WIN32 |
---|
| 6 | |
---|
[7438] | 7 | #define _WIN32_WINNT 0x0501 |
---|
| 8 | |
---|
[7328] | 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 | { |
---|
[7394] | 18 | WORD versionRequested = MAKEWORD (2, 2); |
---|
[7328] | 19 | WSADATA wsaData; |
---|
| 20 | |
---|
| 21 | if (WSAStartup (versionRequested, & wsaData)) |
---|
| 22 | return -1; |
---|
| 23 | |
---|
[7394] | 24 | if (LOBYTE (wsaData.wVersion) != 2|| |
---|
| 25 | HIBYTE (wsaData.wVersion) != 2) |
---|
[7328] | 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 | |
---|
[7390] | 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) |
---|
[7433] | 71 | return sizeof (struct sockaddr_in); |
---|
[7390] | 72 | if (family == ENET_IPV6) |
---|
[7433] | 73 | return sizeof (struct sockaddr_in6); |
---|
[7390] | 74 | return 0; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static ENetAddressFamily |
---|
[7433] | 78 | enet_address_set_address (ENetAddress * address, const struct sockaddr * sin) |
---|
[7390] | 79 | { |
---|
| 80 | memset (address, 0, sizeof (ENetAddress)); |
---|
| 81 | if (sin -> sa_family == AF_INET) |
---|
| 82 | { |
---|
[7433] | 83 | address -> host = enet_address_map4 ((((struct sockaddr_in *) sin) -> sin_addr.s_addr)); |
---|
[7392] | 84 | /* address -> scopeID = 0; */ |
---|
[7433] | 85 | address -> port = ENET_NET_TO_HOST_16 (((struct sockaddr_in *) sin) -> sin_port); |
---|
[7390] | 86 | return ENET_IPV4; |
---|
| 87 | } |
---|
| 88 | if (sin -> sa_family == AF_INET6) |
---|
| 89 | { |
---|
[7433] | 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); |
---|
[7390] | 93 | return ENET_IPV6; |
---|
| 94 | } |
---|
| 95 | return ENET_NO_ADDRESS_FAMILY; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static int |
---|
[7433] | 99 | enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) |
---|
[7390] | 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 | { |
---|
[7433] | 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); |
---|
[7390] | 109 | return 0; |
---|
| 110 | } |
---|
| 111 | else if (family == ENET_IPV6) |
---|
| 112 | { |
---|
[7433] | 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); |
---|
[7390] | 117 | return 0; |
---|
| 118 | } |
---|
| 119 | return -1; |
---|
| 120 | } |
---|
| 121 | |
---|
[7328] | 122 | int |
---|
| 123 | enet_address_set_host (ENetAddress * address, const char * name) |
---|
| 124 | { |
---|
[7390] | 125 | enet_uint16 port = address -> port; |
---|
[7433] | 126 | struct addrinfo hints; |
---|
| 127 | struct addrinfo * result; |
---|
| 128 | struct addrinfo * res; |
---|
[7328] | 129 | |
---|
[7438] | 130 | memset(& hints, 0, sizeof (hints)); |
---|
| 131 | hints.ai_flags = AI_ADDRCONFIG; |
---|
[7390] | 132 | hints.ai_family = AF_UNSPEC; |
---|
| 133 | |
---|
| 134 | if ( getaddrinfo(name, NULL, &hints, &result) ) |
---|
| 135 | return -1; |
---|
| 136 | |
---|
| 137 | for (res = result; res != NULL; res = res -> ai_next) |
---|
[7328] | 138 | { |
---|
[7390] | 139 | if ( enet_address_set_address(address, res -> ai_addr) != ENET_NO_ADDRESS_FAMILY ) |
---|
| 140 | break; |
---|
[7328] | 141 | } |
---|
| 142 | |
---|
[7390] | 143 | address -> port = port; |
---|
| 144 | freeaddrinfo(result); |
---|
| 145 | if (res == NULL) return -1; |
---|
[7328] | 146 | |
---|
| 147 | return 0; |
---|
| 148 | } |
---|
| 149 | |
---|
[7390] | 150 | static int |
---|
| 151 | enet_address_get_host_x (const ENetAddress * address, char * name, size_t nameLength, int flags) |
---|
[7328] | 152 | { |
---|
[7433] | 153 | struct sockaddr_storage sin; |
---|
| 154 | enet_address_set_sin((struct sockaddr *) & sin, address, ENET_IPV6); |
---|
[7390] | 155 | |
---|
[7433] | 156 | if ( getnameinfo((struct sockaddr *) & sin, enet_sa_size (ENET_IPV6), name, nameLength, NULL, 0, flags)) |
---|
[7328] | 157 | return -1; |
---|
[7390] | 158 | |
---|
[7328] | 159 | return 0; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | int |
---|
[7390] | 163 | enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) |
---|
| 164 | { |
---|
| 165 | return enet_address_get_host_x(address, name, nameLength, NI_NUMERICHOST); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | int |
---|
[7328] | 169 | enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength) |
---|
| 170 | { |
---|
[7390] | 171 | return enet_address_get_host_x(address, name, nameLength, 0); |
---|
[7328] | 172 | } |
---|
| 173 | |
---|
| 174 | int |
---|
[7390] | 175 | enet_socket_bind (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 176 | { |
---|
[7433] | 177 | struct sockaddr_storage sin; |
---|
[7328] | 178 | |
---|
| 179 | if (address != NULL) |
---|
| 180 | { |
---|
[7433] | 181 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
[7397] | 185 | ENetAddress address_ = { ENET_HOST_ANY_INIT, 0, 0 }; |
---|
[7433] | 186 | enet_address_set_sin((struct sockaddr *) & sin, & address_, family); |
---|
[7328] | 187 | } |
---|
| 188 | |
---|
[7433] | 189 | return bind (socket, (struct sockaddr *) & sin, enet_sa_size(family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 190 | } |
---|
| 191 | |
---|
| 192 | int |
---|
| 193 | enet_socket_listen (ENetSocket socket, int backlog) |
---|
| 194 | { |
---|
| 195 | return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | ENetSocket |
---|
[7390] | 199 | enet_socket_create (ENetSocketType type, ENetAddressFamily family) |
---|
[7328] | 200 | { |
---|
[7433] | 201 | ENetSocket sock = socket (enet_af (family), type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0); |
---|
| 202 | return sock; |
---|
[7328] | 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 |
---|
[7390] | 241 | enet_socket_connect (ENetSocket socket, const ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 242 | { |
---|
[7433] | 243 | struct sockaddr_storage sin; |
---|
| 244 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 245 | |
---|
[7433] | 246 | return connect (socket, (struct sockaddr *) & sin, enet_sa_size (family)) == SOCKET_ERROR ? -1 : 0; |
---|
[7328] | 247 | } |
---|
| 248 | |
---|
| 249 | ENetSocket |
---|
[7390] | 250 | enet_socket_accept (ENetSocket socket, ENetAddress * address, ENetAddressFamily family) |
---|
[7328] | 251 | { |
---|
| 252 | SOCKET result; |
---|
[7433] | 253 | struct sockaddr_storage sin; |
---|
| 254 | socklen_t sinLength = enet_sa_size (family); |
---|
[7328] | 255 | |
---|
| 256 | result = accept (socket, |
---|
[7433] | 257 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
[7328] | 258 | address != NULL ? & sinLength : NULL); |
---|
| 259 | |
---|
| 260 | if (result == INVALID_SOCKET) |
---|
| 261 | return ENET_SOCKET_NULL; |
---|
| 262 | |
---|
| 263 | if (address != NULL) |
---|
| 264 | { |
---|
[7433] | 265 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
[7328] | 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, |
---|
[7390] | 281 | size_t bufferCount, |
---|
| 282 | ENetAddressFamily family) |
---|
[7328] | 283 | { |
---|
[7433] | 284 | struct sockaddr_storage sin; |
---|
[7328] | 285 | DWORD sentLength; |
---|
| 286 | |
---|
| 287 | if (address != NULL) |
---|
| 288 | { |
---|
[7433] | 289 | enet_address_set_sin((struct sockaddr *) & sin, address, family); |
---|
[7328] | 290 | } |
---|
| 291 | |
---|
| 292 | if (WSASendTo (socket, |
---|
| 293 | (LPWSABUF) buffers, |
---|
| 294 | (DWORD) bufferCount, |
---|
| 295 | & sentLength, |
---|
| 296 | 0, |
---|
[7433] | 297 | address != NULL ? (struct sockaddr *) & sin : 0, |
---|
[7390] | 298 | address != NULL ? enet_sa_size (family) : 0, |
---|
[7328] | 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, |
---|
[7390] | 315 | size_t bufferCount, |
---|
| 316 | ENetAddressFamily family) |
---|
[7328] | 317 | { |
---|
[7390] | 318 | INT sinLength = enet_sa_size (family); |
---|
[7328] | 319 | DWORD flags = 0, |
---|
| 320 | recvLength; |
---|
[7433] | 321 | struct sockaddr_storage sin; |
---|
[7328] | 322 | |
---|
| 323 | if (WSARecvFrom (socket, |
---|
| 324 | (LPWSABUF) buffers, |
---|
| 325 | (DWORD) bufferCount, |
---|
| 326 | & recvLength, |
---|
| 327 | & flags, |
---|
[7433] | 328 | address != NULL ? (struct sockaddr *) & sin : NULL, |
---|
[7328] | 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 | { |
---|
[7433] | 348 | enet_address_set_address(address, (struct sockaddr *) & sin); |
---|
[7328] | 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 |
---|
[7390] | 366 | enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) |
---|
[7328] | 367 | { |
---|
| 368 | fd_set readSet, writeSet; |
---|
| 369 | struct timeval timeVal; |
---|
| 370 | int selectCount; |
---|
[7391] | 371 | ENetSocket maxSocket; |
---|
[7390] | 372 | |
---|
[7328] | 373 | timeVal.tv_sec = timeout / 1000; |
---|
| 374 | timeVal.tv_usec = (timeout % 1000) * 1000; |
---|
[7390] | 375 | |
---|
[7328] | 376 | FD_ZERO (& readSet); |
---|
| 377 | FD_ZERO (& writeSet); |
---|
| 378 | |
---|
| 379 | if (* condition & ENET_SOCKET_WAIT_SEND) |
---|
[7390] | 380 | { |
---|
| 381 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 382 | FD_SET (socket4, & writeSet); |
---|
| 383 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 384 | FD_SET (socket6, & writeSet); |
---|
| 385 | } |
---|
[7328] | 386 | |
---|
| 387 | if (* condition & ENET_SOCKET_WAIT_RECEIVE) |
---|
[7390] | 388 | { |
---|
| 389 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 390 | FD_SET (socket4, & readSet); |
---|
| 391 | if (socket6 != ENET_SOCKET_NULL) |
---|
| 392 | FD_SET (socket6, & readSet); |
---|
| 393 | } |
---|
[7328] | 394 | |
---|
[7391] | 395 | maxSocket = 0; |
---|
[7390] | 396 | if (socket4 != ENET_SOCKET_NULL) |
---|
| 397 | maxSocket = socket4; |
---|
| 398 | if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) |
---|
| 399 | maxSocket = socket6; |
---|
[7328] | 400 | |
---|
[7390] | 401 | selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); |
---|
| 402 | |
---|
[7328] | 403 | if (selectCount < 0) |
---|
| 404 | return -1; |
---|
| 405 | |
---|
| 406 | * condition = ENET_SOCKET_WAIT_NONE; |
---|
| 407 | |
---|
| 408 | if (selectCount == 0) |
---|
| 409 | return 0; |
---|
| 410 | |
---|
[7390] | 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; |
---|
[7328] | 414 | |
---|
[7390] | 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 | |
---|
[7328] | 419 | return 0; |
---|
[7390] | 420 | } |
---|
[7328] | 421 | |
---|
| 422 | #endif |
---|
| 423 | |
---|