Ver Fonte

PtrToStructure的结构要注意:
1.如果c++成员是指针必须用IntPtr
2.如果c++成员是对象,那么c#中对应的结构用struct或者class都可以

tanghai há 11 anos atrás
pai
commit
eff8c88b78

+ 3 - 9
CSharp/Platform/UNet/EService.cs

@@ -30,13 +30,12 @@ namespace UNet
 		}
 
 		private IntPtr host;
-		private bool isRunning = true;
 		private readonly object eventsLock = new object();
 		private Action events;
 
 		public EService(string hostName, ushort port)
 		{
-			var address = new Address { HostName = hostName, Port = port };
+			Address address = new Address { HostName = hostName, Port = port };
 			ENetAddress nativeAddress = address.Struct;
 			this.host = NativeMethods.EnetHostCreate(
 				ref nativeAddress, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
@@ -91,7 +90,7 @@ namespace UNet
 
 		private EEvent GetEvent()
 		{
-			var enetEv = new ENetEvent();
+			ENetEvent enetEv = new ENetEvent();
 			int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv);
 			if (ret <= 0)
 			{
@@ -163,11 +162,6 @@ namespace UNet
 			local();
 		}
 
-		public void Stop()
-		{
-			this.isRunning = false;
-		}
-
 		private int Service(int timeout)
 		{
 			if (timeout < 0)
@@ -272,7 +266,7 @@ namespace UNet
 
 		public void Run(int timeout = 0)
 		{
-			while (this.isRunning)
+			while (true)
 			{
 				this.RunOnce(timeout);
 			}

+ 1 - 2
CSharp/Platform/UNet/NativeMethods.cs

@@ -83,8 +83,7 @@ namespace UNet
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_flush")]
 		internal static extern void EnetHostFlush(IntPtr host);
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events"
-				)]
+		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_check_events")]
 		internal static extern int EnetHostCheckEvents(IntPtr host, ENetEvent ev);
 
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_service")]

+ 12 - 12
CSharp/Platform/UNet/NativeStructs.cs

@@ -45,9 +45,9 @@ namespace UNet
 		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
 		public delegate void NoMemoryCb();
 
-		private readonly IntPtr malloc;
-		private readonly IntPtr free;
-		private readonly IntPtr no_memory;
+		private IntPtr malloc;
+		private IntPtr free;
+		private IntPtr no_memory;
 	}
 
 	[StructLayout(LayoutKind.Sequential)]
@@ -68,8 +68,8 @@ namespace UNet
 	[StructLayout(LayoutKind.Sequential)]
 	public class ENetListNode
 	{
-		public ENetListNode Next;
-		public ENetListNode Previous;
+		public IntPtr Next;
+		public IntPtr Previous;
 	}
 
 	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -90,14 +90,14 @@ namespace UNet
 	public struct ENetPeer
 	{
 		public ENetListNode DispatchList;
-		public readonly IntPtr Host;
-		public readonly ushort OutgoingPeerID;
-		public readonly ushort IncomingPeerID;
-		public readonly uint ConnectID;
-		public readonly byte OutgoingSessionID;
-		public readonly byte IncomingSessionID;
+		public IntPtr Host;
+		public ushort OutgoingPeerID;
+		public ushort IncomingPeerID;
+		public uint ConnectID;
+		public byte OutgoingSessionID;
+		public byte IncomingSessionID;
 		public ENetAddress Address;
 		public IntPtr Data;
-		public readonly PeerState State;
+		public PeerState State;
 	}
 }

+ 0 - 12
CSharp/Platform/UNet/USocket.cs

@@ -70,18 +70,6 @@ namespace UNet
 			}
 		}
 
-		public PeerState State
-		{
-			get
-			{
-				if (this.peerPtr == IntPtr.Zero)
-				{
-					return PeerState.Uninitialized;
-				}
-				return this.Struct.State;
-			}
-		}
-
 		public void Ping()
 		{
 			NativeMethods.EnetPeerPing(this.peerPtr);