Parcourir la source

修复UNet偶尔会crash的bug,该bug是因为UChannle Dispose的时候先将socket Dispose,
之后再从UService中remove UChannel,remove过程中调用了RemoteAddress函数,RemoteAddress函数又调用了socket的RemoteAddress函数,
而此时socket已经被Dispose掉了

tanghai il y a 11 ans
Parent
commit
c81d25b755

+ 3 - 1
CSharp/Platform/TNet/TChannel.cs

@@ -20,12 +20,14 @@ namespace TNet
 		private ObjectId sendTimer = ObjectId.Empty;
 		private Action onParseComplete = () => { };
 		private readonly PacketParser parser;
+		private readonly string remoteAddress;
 
 		public TChannel(TSocket socket, TService service)
 		{
 			this.socket = socket;
 			this.service = service;
 			this.parser = new PacketParser(recvBuffer);
+			this.remoteAddress = this.socket.RemoteAddress;
 		}
 
 		protected virtual void Dispose(bool disposing)
@@ -133,7 +135,7 @@ namespace TNet
 		{
 			get
 			{
-				return this.socket.RemoteAddress;
+				return remoteAddress;
 			}
 		}
 

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

@@ -4,8 +4,7 @@
 	{
 		public static void Initialize()
 		{
-			ENetCallbacks inits = new ENetCallbacks();
-			int ret = NativeMethods.EnetInitializeWithCallbacks(NativeMethods.ENET_VERSION, ref inits);
+			int ret = NativeMethods.EnetInitialize();
 			if (ret < 0)
 			{
 				throw new UException(string.Format("Initialization failed, ret: {0}", ret));

+ 0 - 4
CSharp/Platform/UNet/NativeMethods.cs

@@ -40,10 +40,6 @@ namespace UNet
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_initialize")]
 		internal static extern int EnetInitialize();
 
-		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl,
-				EntryPoint = "enet_initialize_with_callbacks")]
-		internal static extern int EnetInitializeWithCallbacks(uint version, ref ENetCallbacks inits);
-
 		[DllImport(LIB, CallingConvention = CallingConvention.Cdecl, EntryPoint = "enet_host_create")]
 		internal static extern IntPtr EnetHostCreate(
 				ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth,

+ 1 - 27
CSharp/Platform/UNet/NativeStructs.cs

@@ -33,23 +33,6 @@ namespace UNet
 		public ushort Port;
 	}
 
-	[StructLayout(LayoutKind.Sequential)]
-	internal struct ENetCallbacks
-	{
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate IntPtr MallocCb(IntPtr size);
-
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate void FreeCb(IntPtr memory);
-
-		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-		public delegate void NoMemoryCb();
-
-		private IntPtr malloc;
-		private IntPtr free;
-		private IntPtr no_memory;
-	}
-
 	// ENetEvent
 	[StructLayout(LayoutKind.Sequential)]
 	internal class ENetEvent
@@ -61,11 +44,6 @@ namespace UNet
 		public IntPtr Packet;
 	}
 
-	[StructLayout(LayoutKind.Sequential)]
-	internal struct ENetHost
-	{
-	}
-
 	[StructLayout(LayoutKind.Sequential)]
 	internal class ENetListNode
 	{
@@ -73,9 +51,6 @@ namespace UNet
 		public IntPtr Previous;
 	}
 
-	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-	internal delegate void ENetPacketFreeCallback(ref ENetPacket param0);
-
 	[StructLayout(LayoutKind.Sequential)]
 	internal struct ENetPacket
 	{
@@ -83,7 +58,7 @@ namespace UNet
 		public uint Flags;
 		public IntPtr Data;
 		public uint DataLength;
-		public ENetPacketFreeCallback FreeCallback;
+		public IntPtr FreeCallback;
 		public IntPtr UserData;
 	}
 
@@ -99,6 +74,5 @@ namespace UNet
 		public byte IncomingSessionID;
 		public ENetAddress Address;
 		public IntPtr Data;
-		public PeerState State;
 	}
 }

+ 5 - 2
CSharp/Platform/UNet/UChannel.cs

@@ -8,12 +8,14 @@ namespace UNet
 	{
 		private readonly UService service;
 		private USocket socket;
+		private readonly string remoteAddress;
 
 
 		public UChannel(USocket socket, UService service)
 		{
 			this.socket = socket;
 			this.service = service;
+			remoteAddress = this.socket.RemoteAddress;
 		}
 
 		protected void Dispose(bool disposing)
@@ -22,13 +24,14 @@ namespace UNet
 			{
 				return;
 			}
-			
+
 			if (disposing)
 			{
 				socket.Dispose();
 			}
 
 			service.Remove(this);
+
 			this.socket = null;
 		}
 
@@ -58,7 +61,7 @@ namespace UNet
 		{
 			get
 			{
-				return this.socket.RemoteAddress;
+				return remoteAddress;
 			}
 		}
 

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

@@ -72,18 +72,6 @@ namespace UNet
 			}
 		}
 
-		public uint Length
-		{
-			get
-			{
-				if (this.packet == IntPtr.Zero)
-				{
-					return 0;
-				}
-				return this.Struct.DataLength;
-			}
-		}
-
 		public byte[] Bytes
 		{
 			get

+ 3 - 1
CSharp/Platform/UNet/UPoller.cs

@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Threading.Tasks;
 using Common.Base;
+using Common.Logger;
 
 namespace UNet
 {
@@ -180,7 +181,8 @@ namespace UNet
 
 		private int Service()
 		{
-			return NativeMethods.EnetHostService(this.host, null, 0);
+			int ret = NativeMethods.EnetHostService(this.host, null, 0);
+			return ret;
 		}
 
 		public void RunOnce(int timeout = 0)

+ 5 - 5
CSharp/Platform/UNet/USocket.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Threading.Tasks;
+using Common.Logger;
 using Network;
 
 namespace UNet
@@ -9,7 +10,7 @@ namespace UNet
 	internal sealed class USocket : IDisposable
 	{
 		private IntPtr peerPtr;
-		private readonly LinkedList<byte[]> recvBuffer = new LinkedList<byte[]>();
+		private readonly Queue<byte[]> recvQueue = new Queue<byte[]>();
 
 		public Action<ENetEvent> Connected { get; set; }
 		public Action<ENetEvent> Received { get; private set; }
@@ -100,10 +101,9 @@ namespace UNet
 			var tcs = new TaskCompletionSource<byte[]>();
 
 			// 如果有缓存的包,从缓存中取
-			if (this.recvBuffer.Count > 0)
+			if (this.recvQueue.Count > 0)
 			{
-				byte[] bytes = this.recvBuffer.First.Value;
-				this.recvBuffer.RemoveFirst();
+				byte[] bytes = this.recvQueue.Dequeue();
 				tcs.TrySetResult(bytes);
 			}
 			// 没有缓存封包,设置回调等待
@@ -173,7 +173,7 @@ namespace UNet
 				using (UPacket packet = new UPacket(eNetEvent.Packet))
 				{
 					byte[] bytes = packet.Bytes;
-					this.recvBuffer.AddLast(bytes);
+					this.recvQueue.Enqueue(bytes);
 				}
 			}
 			else

+ 2 - 1
CSharp/Platform/UNetTest/UServiceTest.cs

@@ -2,6 +2,7 @@
 using System.Threading;
 using System.Threading.Tasks;
 using Common.Helper;
+using Common.Logger;
 using UNet;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Network;
@@ -31,7 +32,7 @@ namespace UNetTest
 			CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes);
 			Array.Reverse(bytes);
 			channel.SendAsync(bytes);
-
+			
 			barrier.RemoveParticipant();
 		}