Browse Source

Fix array of pointers in selective broadcast

NX 7 years ago
parent
commit
953cf9fc4a
2 changed files with 14 additions and 8 deletions
  1. 7 3
      Source/Managed/ENet.cs
  2. 7 5
      Source/Native/enet.h

+ 7 - 3
Source/Managed/ENet.cs

@@ -466,12 +466,16 @@ namespace ENet {
 
 			if (peers.Length > 0) {
 				IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
+				int nativeCount = 0;
 
 				for (int i = 0; i < peers.Length; i++) {
-					nativePeers[i] = peers[i].NativeData;
+					if (peers[i].NativeData != IntPtr.Zero) {
+						nativePeers[nativeCount] = peers[i].NativeData;
+						nativeCount++;
+					}
 				}
 
-				Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)peers.Length);
+				Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)nativeCount);
 			}
 
 			packet.NativeData = IntPtr.Zero;
@@ -970,4 +974,4 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_peer_reset(IntPtr peer);
 	}
-}
+}

+ 7 - 5
Source/Native/enet.h

@@ -746,7 +746,7 @@ extern "C" {
     ENET_API int                 enet_host_service(ENetHost *, ENetEvent *, enet_uint32);
     ENET_API void                enet_host_flush(ENetHost *);
     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_broadcast_selective(ENetHost *, enet_uint8, ENetPacket *, ENetPeer **, size_t);
     ENET_API void                enet_host_channel_limit(ENetHost *, size_t);
     ENET_API void                enet_host_bandwidth_limit(ENetHost *, enet_uint32, enet_uint32);
     extern   void                enet_host_bandwidth_throttle(ENetHost *);
@@ -4210,14 +4210,16 @@ extern "C" {
         }
     }
 
-    void enet_host_broadcast_selective(ENetHost *host, enet_uint8 channelID, ENetPacket *packet, ENetPeer *peers, size_t length) {
+    void enet_host_broadcast_selective(ENetHost *host, enet_uint8 channelID, ENetPacket *packet, ENetPeer **peers, size_t length) {
         ENetPeer *currentPeer;
 
-        if (host == NULL || peers == NULL) {
+        if (host == NULL) {
             return;
         }
 
-        for (currentPeer = peers; currentPeer < &peers[length]; ++currentPeer) {
+        for (int i = 0; i < length; i++) {
+			currentPeer = peers[i];
+
             if (currentPeer == NULL || currentPeer->state != ENET_PEER_STATE_CONNECTED) {
                 continue;
             }
@@ -5615,4 +5617,4 @@ extern "C" {
 }
 #endif
 #endif
-#endif
+#endif