source:
code/branches/ipv6/src/external/enet/patches/0004-using-two-separate-sockets-for-IPv4-and-IPv6.patch
@
7390
Last change on this file since 7390 was 7390, checked in by adrfried, 14 years ago | |
---|---|
File size: 13.7 KB |
-
host.c
From 73fa28a84ce456561d3912578121a30bc8263c50 Mon Sep 17 00:00:00 2001 From: Adrian Friedli <adi@koalatux.ch> Date: Wed, 8 Sep 2010 12:50:04 +0200 Subject: [PATCH 4/5] using two separate sockets for IPv4 and IPv6 --- host.c | 53 +++++++++++++++++++++++++++--------- include/enet/enet.h | 11 ++++++- protocol.c | 47 ++++++++++++++++++++++++++++---- unix.c | 74 +++++++++++++++++++++++++++++++++++++------------- 4 files changed, 144 insertions(+), 41 deletions(-) diff --git a/host.c b/host.c index 9ccf894..46e52c9 100644
a b 7 7 #include <time.h> 8 8 #include "enet/enet.h" 9 9 10 static ENetSocket 11 enet_socket_create_bind (const ENetAddress * address, ENetAddressFamily family) 12 { 13 ENetSocket socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, family); 14 if (socket == ENET_SOCKET_NULL) 15 return ENET_SOCKET_NULL; 16 17 if (address != NULL && enet_socket_bind (socket, address, family) < 0) 18 { 19 enet_socket_destroy (socket); 20 return ENET_SOCKET_NULL; 21 } 22 23 enet_socket_set_option (socket, ENET_SOCKOPT_NONBLOCK, 1); 24 enet_socket_set_option (socket, ENET_SOCKOPT_BROADCAST, 1); 25 enet_socket_set_option (socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); 26 enet_socket_set_option (socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 27 28 return socket; 29 } 30 10 31 /** @defgroup host ENet host functions 11 32 @{ 12 33 */ … … enet_host_create (const ENetAddress * address, size_t peerCount, size_t channelL 48 69 } 49 70 memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); 50 71 51 host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, ENET_IPV6); 52 if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address, ENET_IPV6) < 0)) 53 { 54 if (host -> socket != ENET_SOCKET_NULL) 55 enet_socket_destroy (host -> socket); 72 int family = (address == NULL || !memcmp (& address -> host, & ENET_HOST_ANY, sizeof (ENetHostAddress))) ? 73 ENET_IPV4 | ENET_IPV6 : 74 enet_get_address_family (address); 56 75 57 enet_free (host -> peers); 58 enet_free (host); 76 host -> socket4 = (family & ENET_IPV4) ? 77 enet_socket_create_bind (address, ENET_IPV4) : 78 ENET_SOCKET_NULL; 79 host -> socket6 = (family & ENET_IPV6) ? 80 enet_socket_create_bind (address, ENET_IPV6) : 81 ENET_SOCKET_NULL; 59 82 60 return NULL; 83 if (host -> socket4 == ENET_SOCKET_NULL && host -> socket6 == ENET_SOCKET_NULL) 84 { 85 enet_free (host -> peers); 86 enet_free (host); 87 return NULL; 61 88 } 62 89 63 enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1);64 enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1);65 enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE);66 enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE);67 68 90 if (address != NULL) 69 91 host -> address = * address; 70 92 … … enet_host_destroy (ENetHost * host) 133 155 { 134 156 ENetPeer * currentPeer; 135 157 136 enet_socket_destroy (host -> socket); 158 if (host -> socket4 != ENET_SOCKET_NULL) 159 enet_socket_destroy (host -> socket4); 160 if (host -> socket6 != ENET_SOCKET_NULL) 161 enet_socket_destroy (host -> socket6); 137 162 138 163 for (currentPeer = host -> peers; 139 164 currentPeer < & host -> peers [host -> peerCount]; -
include/enet/enet.h
diff --git a/include/enet/enet.h b/include/enet/enet.h index 7f5876f..616fe7f 100644
a b typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * b 335 335 */ 336 336 typedef struct _ENetHost 337 337 { 338 ENetSocket socket; 338 ENetSocket socket4; 339 ENetSocket socket6; 339 340 ENetAddress address; /**< Internet address of the host */ 340 341 enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ 341 342 enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ … … ENET_API ENetSocket enet_socket_accept (ENetSocket, ENetAddress *, ENetAddressFa 462 463 ENET_API int enet_socket_connect (ENetSocket, const ENetAddress *, ENetAddressFamily); 463 464 ENET_API int enet_socket_send (ENetSocket, const ENetAddress *, const ENetBuffer *, size_t, ENetAddressFamily); 464 465 ENET_API int enet_socket_receive (ENetSocket, ENetAddress *, ENetBuffer *, size_t, ENetAddressFamily); 465 ENET_API int enet_socket_wait (ENetSocket, enet_uint32 *, enet_uint32);466 ENET_API int enet_socket_wait (ENetSocket, ENetSocket, enet_uint32 *, enet_uint32); 466 467 ENET_API int enet_socket_set_option (ENetSocket, ENetSocketOption, int); 467 468 ENET_API void enet_socket_destroy (ENetSocket); 468 469 ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSocketSet *, enet_uint32); … … ENET_API int enet_address_get_host (const ENetAddress * address, char * hostName 508 509 */ 509 510 ENET_API ENetHostAddress enet_address_map4 (enet_uint32 address); 510 511 512 /** Returns the Address family of an (IPv4-mapped) IPv6 address. 513 @param address IPv6 address 514 @returns address family 515 */ 516 ENET_API ENetAddressFamily enet_get_address_family (const ENetAddress * address); 517 511 518 /** @} */ 512 519 513 520 ENET_API ENetPacket * enet_packet_create (const void *, size_t, enet_uint32); -
protocol.c
diff --git a/protocol.c b/protocol.c index 4c4850a..505c684 100644
a b enet_address_map4 (enet_uint32 address) 37 37 return addr; 38 38 } 39 39 40 ENetAddressFamily 41 enet_get_address_family (const ENetAddress * address) 42 { 43 if (!memcmp(& address->host, & ENET_IPV4MAPPED_PREFIX, ENET_IPV4MAPPED_PREFIX_LEN)) 44 return ENET_IPV4; 45 return ENET_IPV6; 46 } 47 40 48 size_t 41 49 enet_protocol_command_size (enet_uint8 commandNumber) 42 50 { … … commandError: 1033 1041 } 1034 1042 1035 1043 static int 1036 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event )1044 enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event, ENetAddressFamily family) 1037 1045 { 1038 1046 for (;;) 1039 1047 { … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1043 1051 buffer.data = host -> packetData [0]; 1044 1052 buffer.dataLength = sizeof (host -> packetData [0]); 1045 1053 1046 receivedLength = enet_socket_receive ( host -> socket,1054 receivedLength = enet_socket_receive (family == ENET_IPV4 ? host -> socket4 : host -> socket6, 1047 1055 & host -> receivedAddress, 1048 1056 & buffer, 1049 1057 1, 1050 ENET_IPV6);1058 family); 1051 1059 1052 1060 if (receivedLength < 0) 1053 1061 return -1; … … enet_protocol_receive_incoming_commands (ENetHost * host, ENetEvent * event) 1055 1063 if (receivedLength == 0) 1056 1064 return 0; 1057 1065 1066 if (enet_get_address_family (& host -> receivedAddress) != family) 1067 return -1; 1068 1058 1069 host -> receivedData = host -> packetData [0]; 1059 1070 host -> receivedDataLength = receivedLength; 1060 1071 … … enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch 1510 1521 1511 1522 currentPeer -> lastSendTime = host -> serviceTime; 1512 1523 1513 sentLength = enet_socket_send (host -> socket, & currentPeer -> address, host -> buffers, host -> bufferCount, ENET_IPV6); 1524 ENetAddressFamily family = enet_get_address_family (& currentPeer -> address); 1525 ENetSocket socket = family == ENET_IPV4 ? host -> socket4 : host -> socket6; 1526 if (socket == ENET_SOCKET_NULL) 1527 return -1; 1528 sentLength = enet_socket_send (socket, 1529 & currentPeer -> address, 1530 host -> buffers, 1531 host -> bufferCount, 1532 family); 1514 1533 1515 1534 enet_protocol_remove_sent_unreliable_commands (currentPeer); 1516 1535 … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1621 1640 break; 1622 1641 } 1623 1642 1624 switch (enet_protocol_receive_incoming_commands (host, event)) 1643 if (host -> socket4 != ENET_SOCKET_NULL) 1644 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV4)) 1645 { 1646 case 1: 1647 return 1; 1648 1649 case -1: 1650 perror ("Error receiving incoming packets"); 1651 1652 return -1; 1653 1654 default: 1655 break; 1656 } 1657 1658 if (host -> socket6 != ENET_SOCKET_NULL) 1659 switch (enet_protocol_receive_incoming_commands (host, event, ENET_IPV6)) 1625 1660 { 1626 1661 case 1: 1627 1662 return 1; … … enet_host_service (ENetHost * host, ENetEvent * event, enet_uint32 timeout) 1673 1708 1674 1709 waitCondition = ENET_SOCKET_WAIT_RECEIVE; 1675 1710 1676 if (enet_socket_wait (host -> socket , & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0)1711 if (enet_socket_wait (host -> socket4, host -> socket6, & waitCondition, ENET_TIME_DIFFERENCE (timeout, host -> serviceTime)) != 0) 1677 1712 return -1; 1678 1713 1679 1714 host -> serviceTime = enet_time_get (); -
unix.c
diff --git a/unix.c b/unix.c index 475c6e3..751cb6f 100644
a b static int 116 116 enet_address_set_sin (struct sockaddr * sin, const ENetAddress * address, ENetAddressFamily family) 117 117 { 118 118 memset (sin, 0, enet_sa_size(family)); 119 if (family == ENET_IPV4) 119 if (family == ENET_IPV4 && 120 (enet_get_address_family (address) == ENET_IPV4 || 121 !memcmp (& address -> host, & ENET_HOST_ANY, sizeof(ENetHostAddress)))) 120 122 { 121 123 ((struct sockaddr_in *) sin) -> sin_family = AF_INET; 122 124 ((struct sockaddr_in *) sin) -> sin_addr = * (struct in_addr *) & address -> host.addr[12]; … … enet_socket_create (ENetSocketType type, ENetAddressFamily family) 218 220 #ifdef IPV6_V6ONLY 219 221 if (family == ENET_IPV6) 220 222 { 221 int value = 0;223 int value = 1; 222 224 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, & value, sizeof (int)); 223 225 } 224 226 #endif // IPV6_V6ONLY … … enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocket 392 394 } 393 395 394 396 int 395 enet_socket_wait (ENetSocket socket , enet_uint32 * condition, enet_uint32 timeout)397 enet_socket_wait (ENetSocket socket4, ENetSocket socket6, enet_uint32 * condition, enet_uint32 timeout) 396 398 { 397 399 #ifdef HAS_POLL 398 struct pollfd pollSocket ;400 struct pollfd pollSocket[2]; 399 401 int pollCount; 400 401 pollSocket.fd = socket; 402 pollSocket.events = 0; 402 403 pollSocket[0].fd = socket4; 404 pollSocket[1].fd = socket6; 405 pollSocket[0].events = 0; 406 pollSocket[1].events = 0; 407 //pollSocket[0].revents = 0; 408 pollSocket[1].revents = 0; 409 410 if (pollSocket[0].fd == ENET_SOCKET_NULL) 411 { 412 pollSocket[0].fd = pollSocket[1].fd; 413 pollSocket[1].fd = ENET_SOCKET_NULL; 414 } 403 415 404 416 if (* condition & ENET_SOCKET_WAIT_SEND) 405 pollSocket.events |= POLLOUT; 417 { 418 pollSocket[0].events |= POLLOUT; 419 pollSocket[1].events |= POLLOUT; 420 } 406 421 407 422 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 408 pollSocket.events |= POLLIN; 423 { 424 pollSocket[0].events |= POLLIN; 425 pollSocket[1].events |= POLLIN; 426 } 409 427 410 pollCount = poll ( & pollSocket,1, timeout);428 pollCount = poll (pollSocket, pollSocket[1].fd != ENET_SOCKET_NULL ? 2 : 1, timeout); 411 429 412 430 if (pollCount < 0) 413 431 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 417 435 if (pollCount == 0) 418 436 return 0; 419 437 420 if ( pollSocket.revents& POLLOUT)438 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLOUT) 421 439 * condition |= ENET_SOCKET_WAIT_SEND; 422 440 423 if ( pollSocket.revents& POLLIN)441 if ((pollSocket[0].revents | pollSocket[1].revents) & POLLIN) 424 442 * condition |= ENET_SOCKET_WAIT_RECEIVE; 425 443 426 444 return 0; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 436 454 FD_ZERO (& writeSet); 437 455 438 456 if (* condition & ENET_SOCKET_WAIT_SEND) 439 FD_SET (socket, & writeSet); 457 { 458 if (socket4 != ENET_SOCKET_NULL) 459 FD_SET (socket4, & writeSet); 460 if (socket6 != ENET_SOCKET_NULL) 461 FD_SET (socket6, & writeSet); 462 } 440 463 441 464 if (* condition & ENET_SOCKET_WAIT_RECEIVE) 442 FD_SET (socket, & readSet); 465 { 466 if (socket4 != ENET_SOCKET_NULL) 467 FD_SET (socket4, & readSet); 468 if (socket6 != ENET_SOCKET_NULL) 469 FD_SET (socket6, & readSet); 470 } 471 472 ENetSocket maxSocket = 0; 473 if (socket4 != ENET_SOCKET_NULL) 474 maxSocket = socket4; 475 if (socket6 != ENET_SOCKET_NULL && socket6 > maxSocket) 476 maxSocket = socket6; 443 477 444 selectCount = select ( socket + 1, & readSet, & writeSet, NULL, & timeVal);478 selectCount = select (maxSocket + 1, & readSet, & writeSet, NULL, & timeVal); 445 479 446 480 if (selectCount < 0) 447 481 return -1; … … enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeou 451 485 if (selectCount == 0) 452 486 return 0; 453 487 454 if (FD_ISSET (socket, & writeSet)) 455 * condition |= ENET_SOCKET_WAIT_SEND; 488 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & writeSet)) || 489 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & writeSet)) ) 490 * condition |= ENET_SOCKET_WAIT_SEND; 456 491 457 if (FD_ISSET (socket, & readSet)) 458 * condition |= ENET_SOCKET_WAIT_RECEIVE; 492 if ( (socket4 != ENET_SOCKET_NULL && FD_ISSET (socket4, & readSet)) || 493 (socket6 != ENET_SOCKET_NULL && FD_ISSET (socket6, & readSet)) ) 494 * condition |= ENET_SOCKET_WAIT_RECEIVE; 459 495 460 496 return 0; 461 497 #endif
Note: See TracBrowser
for help on using the repository browser.