Browse Source

修复一个网络层的bug,Session的callback可能会删错session

tanghai 7 years ago
parent
commit
a8bed22340

+ 0 - 5
Unity/Assets/Scripts/Module/Message/Network/AChannel.cs

@@ -19,11 +19,6 @@ namespace ETModel
 		Accept,
 	}
 
-	public class UserTokenInfo
-	{
-		public long InstanceId;
-	}
-
 	public abstract class AChannel: ComponentWithId
 	{
 		public ChannelType ChannelType { get; }

+ 9 - 15
Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs

@@ -31,8 +31,6 @@ namespace ETModel
 			this.parser = new PacketParser(this.recvBuffer);
 			this.innArgs.Completed += this.OnComplete;
 			this.outArgs.Completed += this.OnComplete;
-			this.innArgs.UserToken = new UserTokenInfo() { InstanceId = this.InstanceId };
-			this.outArgs.UserToken = new UserTokenInfo() { InstanceId = this.InstanceId };
 
 			this.RemoteAddress = ipEndPoint;
 		}
@@ -44,8 +42,6 @@ namespace ETModel
 			this.parser = new PacketParser(this.recvBuffer);
 			this.innArgs.Completed += this.OnComplete;
 			this.outArgs.Completed += this.OnComplete;
-			this.innArgs.UserToken = new UserTokenInfo() { InstanceId = this.InstanceId };
-			this.outArgs.UserToken = new UserTokenInfo() { InstanceId = this.InstanceId };
 
 			this.RemoteAddress = (IPEndPoint)socket.RemoteEndPoint;
 			
@@ -148,13 +144,12 @@ namespace ETModel
 
 		private void OnConnectComplete(object o)
 		{
-			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
-			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.socket == null)
 			{
 				return;
 			}
-
+			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
+			
 			if (e.SocketError != SocketError.Success)
 			{
 				this.OnError((int)e.SocketError);	
@@ -199,12 +194,11 @@ namespace ETModel
 
 		private void OnRecvComplete(object o)
 		{
-			SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
-			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.socket == null)
 			{
 				return;
 			}
+			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
 
 			if (e.SocketError != SocketError.Success)
 			{
@@ -244,10 +238,11 @@ namespace ETModel
 				}
 			}
 
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.socket == null)
 			{
 				return;
 			}
+			
 			this.StartRecv();
 		}
 
@@ -290,12 +285,11 @@ namespace ETModel
 
 		private void OnSendComplete(object o)
 		{
-			SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
-			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.socket == null)
 			{
 				return;
 			}
+			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
 
 			if (e.SocketError != SocketError.Success)
 			{

+ 3 - 6
Unity/Assets/Scripts/Module/Message/Network/TCP/TService.cs

@@ -22,7 +22,6 @@ namespace ETModel
 			this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 			this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 			this.innArgs.Completed += this.OnAcceptComplete;
-			this.innArgs.UserToken = new UserTokenInfo() { InstanceId = this.InstanceId };
 			
 			this.acceptor.Bind(ipEndPoint);
 			this.acceptor.Listen(1000);
@@ -71,13 +70,11 @@ namespace ETModel
 
 		private void OnAcceptComplete(object sender, SocketAsyncEventArgs o)
 		{
-			SocketAsyncEventArgs e = o;
-			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.acceptor == null)
 			{
-				Log.Error($"session disposed!");
 				return;
 			}
+			SocketAsyncEventArgs e = o;
 			
 			if (e.SocketError != SocketError.Success)
 			{
@@ -96,7 +93,7 @@ namespace ETModel
 				Log.Error(exception);
 			}
 
-			if (userTokenInfo.InstanceId != this.InstanceId)
+			if (this.acceptor == null)
 			{
 				return;
 			}

+ 2 - 2
Unity/Assets/Scripts/Module/Message/Session.cs

@@ -39,11 +39,11 @@ namespace ETModel
 			this.Error = 0;
 			this.channel = aChannel;
 			this.requestCallback.Clear();
-			
+			long id = this.Id;
 			channel.ErrorCallback += (c, e) =>
 			{
 				this.Error = e;
-				this.Network.Remove(this.Id); 
+				this.Network.Remove(id); 
 			};
 			channel.ReadCallback += this.OnRead;