Browse Source

Add packet free callback

nxrighthere 7 years ago
parent
commit
e3be6315cc
2 changed files with 21 additions and 6 deletions
  1. 15 5
      Source/Managed/ENet.cs
  2. 6 1
      Source/Native/enet.h

+ 15 - 5
Source/Managed/ENet.cs

@@ -86,7 +86,8 @@ namespace ENet {
 
 
 	public delegate IntPtr AllocCallback(IntPtr size);
 	public delegate IntPtr AllocCallback(IntPtr size);
 	public delegate void FreeCallback(IntPtr memory);
 	public delegate void FreeCallback(IntPtr memory);
-	public delegate void OutOfMemoryCallback();
+	public delegate void NoMemoryCallback();
+	public delegate void PacketFreeCallback(Packet packet);
 
 
 	internal static class ArrayPool {
 	internal static class ArrayPool {
 		[ThreadStatic]
 		[ThreadStatic]
@@ -196,10 +197,10 @@ namespace ENet {
 			}
 			}
 		}
 		}
 
 
-		public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, OutOfMemoryCallback outOfMemoryCallback) {
+		public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback) {
 			nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback);
 			nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback);
 			nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback);
 			nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback);
-			nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(outOfMemoryCallback);
+			nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(noMemoryCallback);
 		}
 		}
 	}
 	}
 
 
@@ -254,6 +255,12 @@ namespace ENet {
 				throw new InvalidOperationException("Packet not created");
 				throw new InvalidOperationException("Packet not created");
 		}
 		}
 
 
+		public void SetFreeCallback(PacketFreeCallback callback) {
+			CheckCreated();
+
+			Native.enet_packet_set_free_callback(nativePacket, Marshal.GetFunctionPointerForDelegate(callback));
+		}
+
 		public void Create(byte[] data) {
 		public void Create(byte[] data) {
 			if (data == null)
 			if (data == null)
 				throw new ArgumentNullException("data");
 				throw new ArgumentNullException("data");
@@ -689,7 +696,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) | (0 << 8) | (7);
+		public const uint version = (2 << 16) | (0 << 8) | (8);
 
 
 		public static int Initialize() {
 		public static int Initialize() {
 			return Native.enet_initialize();
 			return Native.enet_initialize();
@@ -744,6 +751,9 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_packet_get_length(IntPtr packet);
 		internal static extern int enet_packet_get_length(IntPtr packet);
 
 
+		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
+
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_packet_destroy(IntPtr packet);
 		internal static extern void enet_packet_destroy(IntPtr packet);
 
 
@@ -852,4 +862,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);
 	}
 	}
-}
+}

+ 6 - 1
Source/Native/enet.h

@@ -32,7 +32,7 @@
 
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 0
 #define ENET_VERSION_MINOR 0
-#define ENET_VERSION_PATCH 7
+#define ENET_VERSION_PATCH 8
 #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)
@@ -771,6 +771,7 @@ extern "C" {
     /* Extended API for easier binding in other programming languages */
     /* Extended API for easier binding in other programming languages */
     ENET_API void *              enet_packet_get_data (ENetPacket *);
     ENET_API void *              enet_packet_get_data (ENetPacket *);
     ENET_API int                 enet_packet_get_length (ENetPacket *);
     ENET_API int                 enet_packet_get_length (ENetPacket *);
+    ENET_API void                enet_packet_set_free_callback (ENetPacket *, const void *);
 
 
     ENET_API enet_uint32         enet_host_get_peers_count (ENetHost *);
     ENET_API enet_uint32         enet_host_get_peers_count (ENetHost *);
     ENET_API enet_uint32         enet_host_get_packets_sent (ENetHost *);
     ENET_API enet_uint32         enet_host_get_packets_sent (ENetHost *);
@@ -4408,6 +4409,10 @@ extern "C" {
         return packet->dataLength;
         return packet->dataLength;
     }
     }
 
 
+    void enet_packet_set_free_callback(ENetPacket *packet, const void *callback) {
+        packet->freeCallback = callback;
+    }
+
     enet_uint32 enet_host_get_peers_count(ENetHost *host) {
     enet_uint32 enet_host_get_peers_count(ENetHost *host) {
         return host->connectedPeers;
         return host->connectedPeers;
     }
     }