[ENet-discuss] broadcast patch

LoneSock lonesock at gmail.com
Tue May 31 16:55:21 PDT 2005


Hi, All.

here is the patch for adding in the ability to broadcast (specifically
when connecting to an unknown server).  This is patched against
today's CVS snapshot.

Jonathan
-- 
Piranha are people too.



Index: protocol.c
===================================================================
RCS file: /var/lib/cvs/enet/enet/protocol.c,v
retrieving revision 1.5
diff -u -r1.5 protocol.c
--- protocol.c	24 May 2005 06:08:30 -0000	1.5
+++ protocol.c	31 May 2005 18:12:44 -0000
@@ -92,7 +92,7 @@
 static ENetProtocolCommand
 enet_protocol_remove_sent_reliable_command (ENetPeer * peer, enet_uint32 

reliableSequenceNumber, enet_uint8 channelID)
 {
-    ENetOutgoingCommand * outgoingCommand;
+    ENetOutgoingCommand * outgoingCommand = NULL;
     ENetListIterator currentCommand;
     ENetProtocolCommand commandNumber;
 
@@ -340,7 +340,7 @@
            totalLength;
     ENetChannel * channel;
     ENetListIterator currentCommand;
-    ENetIncomingCommand * startCommand;
+    ENetIncomingCommand * startCommand = NULL;
 
     if (command -> header.commandLength <= sizeof (ENetProtocolSendFragment) ||
         command -> header.channelID >= peer -> channelCount ||
@@ -643,11 +643,15 @@
 
        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) || //	allow any addr if 

broadcast
            header -> challenge != peer -> challenge)
          return 0;
        else
+       {
+         peer -> address.host = host -> receivedAddress.host;	//	just
in case of

broadcast
          peer -> address.port = host -> receivedAddress.port;
+		}         
     }
 
     if (peer != NULL)
Index: unix.c
===================================================================
RCS file: /var/lib/cvs/enet/enet/unix.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 unix.c
--- unix.c	20 Oct 2004 17:17:51 -0000	1.1.1.1
+++ unix.c	31 May 2005 00:41:14 -0000
@@ -129,6 +129,7 @@
 {
     ENetSocket newSocket = socket (PF_INET, type ==
ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM

: SOCK_STREAM, 0);
     int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE;
+    int allowBroadcasting = 1;	//	this is new!
 #ifndef HAS_FCNTL
     int nonBlocking = 1;
 #endif
@@ -146,6 +147,8 @@
 #endif
 
         setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) &
receiveBufferSize, sizeof

(int));
+        //	allow broadcasting
+        setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) &
allowBroadcasting,

sizeof (int));
     }
     
     if (address == NULL)
Index: win32.c
===================================================================
RCS file: /var/lib/cvs/enet/enet/win32.c,v
retrieving revision 1.2
diff -u -r1.2 win32.c
--- win32.c	23 Nov 2004 16:23:36 -0000	1.2
+++ win32.c	31 May 2005 00:41:22 -0000
@@ -91,6 +91,7 @@
     u_long nonBlocking = 1,
         receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE;
     struct sockaddr_in sin;
+    int allowBroadcasting = 1;	//	this is new!
 
     if (newSocket == ENET_SOCKET_NULL)
       return ENET_SOCKET_NULL;
@@ -100,6 +101,8 @@
         ioctlsocket (newSocket, FIONBIO, & nonBlocking);
 
         setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) &
receiveBufferSize, sizeof

(int));
+        //	allow broadcasting
+        setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) &
allowBroadcasting,

sizeof (int));
     }
 
     memset (& sin, 0, sizeof (struct sockaddr_in));
Index: include/enet/enet.h
===================================================================
RCS file: /var/lib/cvs/enet/enet/include/enet/enet.h,v
retrieving revision 1.3
diff -u -r1.3 enet.h
--- include/enet/enet.h	24 May 2005 06:08:30 -0000	1.3
+++ include/enet/enet.h	31 May 2005 00:39:46 -0000
@@ -44,7 +44,8 @@
 
 enum
 {
-   ENET_HOST_ANY = 0
+   ENET_HOST_ANY = 0,                   /**< specifies the default
server host */
+   ENET_HOST_BROADCAST = 0xFFFFFFFFL    /**< specifies a subnet-wide
broadcast */
 };
 
 /**
@@ -52,7 +53,10 @@
  *
  * 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
 {



More information about the ENet-discuss mailing list