浏览代码

Add support for a custom memory allocator

nxrighthere 7 年之前
父节点
当前提交
788b781cfd
共有 1 个文件被更改,包括 41 次插入18 次删除
  1. 41 18
      Source/Managed/ENet.cs

+ 41 - 18
Source/Managed/ENet.cs

@@ -78,15 +78,15 @@ namespace ENet {
 
 
 	[StructLayout(LayoutKind.Sequential)]
 	[StructLayout(LayoutKind.Sequential)]
 	public struct ENetCallbacks {
 	public struct ENetCallbacks {
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate IntPtr malloc_cb(IntPtr size);
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate void free_cb(IntPtr memory);
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate void no_memory_cb();
-		public IntPtr malloc, free, no_memory;
+		public IntPtr malloc;
+		public IntPtr free;
+		public IntPtr no_memory;
 	}
 	}
 
 
+	public delegate IntPtr AllocCallback(IntPtr size);
+	public delegate void FreeCallback(IntPtr memory);
+	public delegate void OutOfMemoryCallback();
+
 	internal static class ArrayPool {
 	internal static class ArrayPool {
 		[ThreadStatic]
 		[ThreadStatic]
 		private static byte[] buffer = null;
 		private static byte[] buffer = null;
@@ -182,6 +182,26 @@ namespace ENet {
 		}
 		}
 	}
 	}
 
 
+	public class Callbacks {
+		private ENetCallbacks nativeCallbacks;
+
+		internal ENetCallbacks NativeData {
+			get {
+				return nativeCallbacks;
+			}
+
+			set {
+				nativeCallbacks = value;
+			}
+		}
+
+		public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, OutOfMemoryCallback outOfMemoryCallback) {
+			nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback);
+			nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback);
+			nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(outOfMemoryCallback);
+		}
+	}
+
 	public struct Packet : IDisposable {
 	public struct Packet : IDisposable {
 		private IntPtr nativePacket;
 		private IntPtr nativePacket;
 
 
@@ -220,7 +240,7 @@ namespace ENet {
 			}
 			}
 		}
 		}
 
 
-		public uint Length {
+		public int Length {
 			get {
 			get {
 				CheckCreated();
 				CheckCreated();
 
 
@@ -672,12 +692,18 @@ 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) | (6);
+		public const uint version = (2 << 16) | (0 << 8) | (7);
 
 
 		public static int Initialize() {
 		public static int Initialize() {
 			return Native.enet_initialize();
 			return Native.enet_initialize();
 		}
 		}
 
 
+		public static int Initialize(ref Callbacks inits) {
+			var nativeCallbacks = inits.NativeData;
+
+			return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks);
+		}
+
 		public static void Deinitialize() {
 		public static void Deinitialize() {
 			Native.enet_deinitialize();
 			Native.enet_deinitialize();
 		}
 		}
@@ -697,13 +723,13 @@ namespace ENet {
 		internal static extern int enet_initialize();
 		internal static extern int enet_initialize();
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_deinitialize();
+		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern uint enet_time_get();
+		internal static extern void enet_deinitialize();
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
+		internal static extern uint enet_time_get();
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
 		internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
@@ -715,16 +741,16 @@ namespace ENet {
 		internal static extern IntPtr enet_packet_get_data(IntPtr packet);
 		internal static extern IntPtr enet_packet_get_data(IntPtr packet);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern uint enet_packet_get_length(IntPtr packet);
+		internal static extern int enet_packet_get_length(IntPtr packet);
 
 
 		[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);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
+		internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
 		internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
@@ -732,9 +758,6 @@ 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 int enet_host_service(IntPtr host, IntPtr @event, uint timeout);
-
 		[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);