Browse Source

Add selective packet broadcast

NX 7 years ago
parent
commit
30dfa399a1
2 changed files with 40 additions and 5 deletions
  1. 16 2
      Source/Managed/ENet.cs
  2. 24 3
      Source/Native/enet.h

+ 16 - 2
Source/Managed/ENet.cs

@@ -450,6 +450,17 @@ namespace ENet {
 			packet.NativeData = IntPtr.Zero;
 			packet.NativeData = IntPtr.Zero;
 		}
 		}
 
 
+		public void Broadcast(byte channelID, ref Packet packet, ref Peer[] peers) {
+			CheckCreated();
+
+			packet.CheckCreated();
+
+			if (peers.Length > 0)
+				Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, ref peers, (IntPtr)peers.Length);
+
+			packet.NativeData = IntPtr.Zero;
+		}
+
 		public int CheckEvents(out Event @event) {
 		public int CheckEvents(out Event @event) {
 			CheckCreated();
 			CheckCreated();
 
 
@@ -756,7 +767,7 @@ namespace ENet {
 		public const uint timeoutLimit = 32;
 		public const uint timeoutLimit = 32;
 		public const uint timeoutMinimum = 5000;
 		public const uint timeoutMinimum = 5000;
 		public const uint timeoutMaximum = 30000;
 		public const uint timeoutMaximum = 30000;
-		public const uint version = (2 << 16) | (1 << 8) | (3);
+		public const uint version = (2 << 16) | (1 << 8) | (4);
 
 
 		public static bool Initialize() {
 		public static bool Initialize() {
 			return Native.enet_initialize() == 0;
 			return Native.enet_initialize() == 0;
@@ -832,6 +843,9 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
 		internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
 
 
+		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, ref Peer[] peers, IntPtr peersLength);
+
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
 		internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
 
 
@@ -940,4 +954,4 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_peer_reset(IntPtr peer);
 		internal static extern void enet_peer_reset(IntPtr peer);
 	}
 	}
-}
+}

+ 24 - 3
Source/Native/enet.h

@@ -35,7 +35,7 @@
 
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 1
 #define ENET_VERSION_MINOR 1
-#define ENET_VERSION_PATCH 3
+#define ENET_VERSION_PATCH 4
 #define ENET_VERSION_CREATE(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
 #define ENET_VERSION_CREATE(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
 #define ENET_VERSION_GET_MAJOR(version) (((version) >> 16) & 0xFF)
 #define ENET_VERSION_GET_MAJOR(version) (((version) >> 16) & 0xFF)
 #define ENET_VERSION_GET_MINOR(version) (((version) >> 8) & 0xFF)
 #define ENET_VERSION_GET_MINOR(version) (((version) >> 8) & 0xFF)
@@ -746,10 +746,11 @@ extern "C" {
     ENET_API int                 enet_host_service(ENetHost *, ENetEvent *, enet_uint32);
     ENET_API int                 enet_host_service(ENetHost *, ENetEvent *, enet_uint32);
     ENET_API void                enet_host_flush(ENetHost *);
     ENET_API void                enet_host_flush(ENetHost *);
     ENET_API void                enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *);
     ENET_API void                enet_host_broadcast(ENetHost *, enet_uint8, ENetPacket *);
+    ENET_API void                enet_host_broadcast_selective(ENetHost *, enet_uint8, ENetPacket *, ENetPeer *, size_t);
     ENET_API void                enet_host_channel_limit(ENetHost *, size_t);
     ENET_API void                enet_host_channel_limit(ENetHost *, size_t);
     ENET_API void                enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32);
     ENET_API void                enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32);
     extern   void                enet_host_bandwidth_throttle(ENetHost *);
     extern   void                enet_host_bandwidth_throttle(ENetHost *);
-    extern  enet_uint64          enet_host_random_seed(void);
+    extern   enet_uint64         enet_host_random_seed(void);
 
 
     ENET_API int                 enet_peer_send(ENetPeer *, enet_uint8, ENetPacket *);
     ENET_API int                 enet_peer_send(ENetPeer *, enet_uint8, ENetPacket *);
     ENET_API ENetPacket *        enet_peer_receive(ENetPeer *, enet_uint8 * channelID);
     ENET_API ENetPacket *        enet_peer_receive(ENetPeer *, enet_uint8 * channelID);
@@ -4209,6 +4210,26 @@ extern "C" {
         }
         }
     }
     }
 
 
+    void enet_host_broadcast_selective(ENetHost *host, enet_uint8 channelID, ENetPacket *packet, ENetPeer *peers, size_t length) {
+        ENetPeer *currentPeer;
+
+        if (host == NULL) {
+            return;
+        }
+
+        for (currentPeer = peers; currentPeer < &peers[length]; ++currentPeer) {
+            if (currentPeer->state != ENET_PEER_STATE_CONNECTED) {
+                continue;
+            }
+
+            enet_peer_send(currentPeer, channelID, packet);
+        }
+
+        if (packet->referenceCount == 0) {
+            enet_packet_destroy(packet);
+        }
+    }
+
     void enet_host_channel_limit(ENetHost *host, size_t channelLimit) {
     void enet_host_channel_limit(ENetHost *host, size_t channelLimit) {
         if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
         if (!channelLimit || channelLimit > ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) {
             channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
             channelLimit = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT;
@@ -5594,4 +5615,4 @@ extern "C" {
 }
 }
 #endif
 #endif
 #endif
 #endif
-#endif
+#endif