Parcourir la source

added broadcasting

eihrul il y a 20 ans
Parent
commit
fd57f842d5
4 fichiers modifiés avec 20 ajouts et 8 suppressions
  1. 7 3
      include/enet/enet.h
  2. 6 2
      protocol.c
  3. 3 1
      unix.c
  4. 4 2
      win32.c

+ 7 - 3
include/enet/enet.h

@@ -44,7 +44,8 @@ typedef enum
 
 enum
 {
-   ENET_HOST_ANY = 0
+   ENET_HOST_ANY       = 0,            /**< specifies the default server host */
+   ENET_HOST_BROADCAST = 0xFFFFFFFF    /**< specifies a subnet-wide broadcast */
 };
 
 /**
@@ -52,11 +53,14 @@ enum
  *
  * The host must be specified in network byte-order, and the port must be in host 
  * byte-order. The constant ENET_HOST_ANY may be used to specify the default 
- * server host.
+ * server host. The constant ENET_HOST_BROADCAST may be used to specify the
+ * broadcast address (255.255.255.255).  This makes sense for enet_host_connect,
+ * but not for enet_host_create.  Once a server responds to a broadcast, the
+ * address is updated from ENET_HOST_BROADCAST to the server's actual IP address.
  */
 typedef struct _ENetAddress
 {
-   enet_uint32 host;  /**< may use ENET_HOST_ANY to specify default server host */
+   enet_uint32 host;
    enet_uint16 port;
 } ENetAddress;
 

+ 6 - 2
protocol.c

@@ -643,11 +643,15 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
 
        if (peer -> state == ENET_PEER_STATE_DISCONNECTED ||
            peer -> state == ENET_PEER_STATE_ZOMBIE || 
-           host -> receivedAddress.host != peer -> address.host ||
+           (host -> receivedAddress.host != peer -> address.host &&
+             peer -> address.host != ENET_HOST_BROADCAST) ||
            header -> challenge != peer -> challenge)
          return 0;
        else
-         peer -> address.port = host -> receivedAddress.port;
+       {
+           peer -> address.host = host -> receivedAddress.host;
+           peer -> address.port = host -> receivedAddress.port;
+       }
     }
 
     if (peer != NULL)

+ 3 - 1
unix.c

@@ -128,7 +128,8 @@ ENetSocket
 enet_socket_create (ENetSocketType type, const ENetAddress * address)
 {
     ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
-    int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE;
+    int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
+        allowBroadcasting = 1;
 #ifndef HAS_FCNTL
     int nonBlocking = 1;
 #endif
@@ -146,6 +147,7 @@ enet_socket_create (ENetSocketType type, const ENetAddress * address)
 #endif
 
         setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
+        setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
     }
     
     if (address == NULL)

+ 4 - 2
win32.c

@@ -88,8 +88,9 @@ ENetSocket
 enet_socket_create (ENetSocketType type, const ENetAddress * address)
 {
     ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
-    u_long nonBlocking = 1,
-        receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE;
+    u_long nonBlocking = 1;
+    int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
+        allowBroadcasting = 1;
     struct sockaddr_in sin;
 
     if (newSocket == ENET_SOCKET_NULL)
@@ -100,6 +101,7 @@ enet_socket_create (ENetSocketType type, const ENetAddress * address)
         ioctlsocket (newSocket, FIONBIO, & nonBlocking);
 
         setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
+        setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
     }
 
     memset (& sin, 0, sizeof (struct sockaddr_in));