Browse Source

Fix selective packet broadcast

NX 7 years ago
parent
commit
0054428bf7
1 changed files with 26 additions and 10 deletions
  1. 26 10
      Source/Managed/ENet.cs

+ 26 - 10
Source/Managed/ENet.cs

@@ -91,13 +91,22 @@ namespace ENet {
 
 
 	internal static class ArrayPool {
 	internal static class ArrayPool {
 		[ThreadStatic]
 		[ThreadStatic]
-		private static byte[] buffer;
+		private static byte[] byteBuffer;
+		[ThreadStatic]
+		private static IntPtr[] pointerBuffer;
+
+		public static byte[] GetByteBuffer() {
+			if (byteBuffer == null)
+				byteBuffer = new byte[64];
+
+			return byteBuffer;
+		}
 
 
-		public static byte[] GetBuffer() {
-			if (buffer == null)
-				buffer = new byte[64];
+		public static IntPtr[] GetPointerBuffer() {
+			if (pointerBuffer == null)
+				pointerBuffer = new IntPtr[Library.maxPeers];
 
 
-			return buffer;
+			return pointerBuffer;
 		}
 		}
 	}
 	}
 
 
@@ -455,8 +464,15 @@ namespace ENet {
 
 
 			packet.CheckCreated();
 			packet.CheckCreated();
 
 
-			if (peers.Length > 0)
-				Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, ref peers, (IntPtr)peers.Length);
+			if (peers.Length > 0) {
+				IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
+
+				for (int i = 0; i < peers.Length; i++) {
+					nativePeers[i] = peers[i].NativeData;
+				}
+
+				Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, ref nativePeers, (IntPtr)peers.Length);
+			}
 
 
 			packet.NativeData = IntPtr.Zero;
 			packet.NativeData = IntPtr.Zero;
 		}
 		}
@@ -576,7 +592,7 @@ namespace ENet {
 			get {
 			get {
 				CheckCreated();
 				CheckCreated();
 
 
-				byte[] ip = ArrayPool.GetBuffer();
+				byte[] ip = ArrayPool.GetByteBuffer();
 
 
 				if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
 				if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
 					if (Encoding.ASCII.GetString(ip).Remove(7) != "::ffff:")
 					if (Encoding.ASCII.GetString(ip).Remove(7) != "::ffff:")
@@ -844,7 +860,7 @@ namespace ENet {
 		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)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, ref Peer[] peers, IntPtr peersLength);
+		internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, ref IntPtr[] 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);
@@ -954,4 +970,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);
 	}
 	}
-}
+}