Эх сурвалжийг харах

修复Tcp中一个异常,原因是跨线程队列还没执行完,tchannel有可能已经销毁了,所以需要判断是否为null

tanghai 3 жил өмнө
parent
commit
787462d98a

+ 31 - 4
Unity/Assets/Scripts/Core/Module/Network/NetServices.cs

@@ -218,7 +218,9 @@ namespace ET
 
         public AService Get(int id)
         {
-            return this.services[id];
+            AService aService;
+            this.services.TryGetValue(id, out aService);
+            return aService;
         }
         
         private void Remove(int id)
@@ -255,19 +257,28 @@ namespace ET
                         case NetOp.CreateChannel:
                         {
                             AService service = this.Get(op.ServiceId);
-                            service.Create(op.ChannelId, op.Object as IPEndPoint);
+                            if (service != null)
+                            {
+                                service.Create(op.ChannelId, op.Object as IPEndPoint);
+                            }
                             break;
                         }
                         case NetOp.RemoveChannel:
                         {
                             AService service = this.Get(op.ServiceId);
-                            service.Remove(op.ChannelId, (int)op.ActorId);
+                            if (service != null)
+                            {
+                                service.Remove(op.ChannelId, (int)op.ActorId);
+                            }
                             break;
                         }
                         case NetOp.SendMessage:
                         {
                             AService service = this.Get(op.ServiceId);
-                            service.Send(op.ChannelId, op.ActorId, op.Object);
+                            if (service != null)
+                            {
+                                service.Send(op.ChannelId, op.ActorId, op.Object);
+                            }
                             break;
                         }
                         case NetOp.GetKChannelConn:
@@ -276,7 +287,15 @@ namespace ET
                             try
                             {
                                 AService service = this.Get(op.ServiceId);
+                                if (service == null)
+                                {
+                                    break;
+                                }
                                 KChannel kChannel = (service as KService).Get(op.ChannelId);
+                                if (kChannel == null)
+                                {
+                                    break;
+                                }
                                 tcs.SetResult((kChannel.LocalConn, kChannel.RemoteConn));
                             }
                             catch (Exception e)
@@ -288,7 +307,15 @@ namespace ET
                         case NetOp.ChangeAddress:
                         {
                             AService service = this.Get(op.ServiceId);
+                            if (service == null)
+                            {
+                                break;
+                            }
                             KChannel kChannel = (service as KService).Get(op.ChannelId);
+                            if (kChannel == null)
+                            {
+                                break;
+                            }
                             kChannel.RemoteAddress = op.Object as IPEndPoint;
                             break;
                         }

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Network/TChannel.cs

@@ -46,7 +46,7 @@ namespace ET
 			this.isConnected = false;
 			this.isSending = false;
 			
-			this.Service.Queue.Enqueue(new TArgs(){Op = TcpOp.ConnectRemote,ChannelId = this.Id});
+			this.Service.Queue.Enqueue(new TArgs(){Op = TcpOp.Connect,ChannelId = this.Id});
 		}
 		
 		public TChannel(long id, Socket socket, TService service)

+ 31 - 9
Unity/Assets/Scripts/Core/Module/Network/TService.cs

@@ -12,7 +12,7 @@ namespace ET
 	{
 		StartSend,
 		StartRecv,
-		ConnectRemote,
+		Connect,
 	}
 	
 	public struct TArgs
@@ -198,19 +198,28 @@ namespace ET
 						case TcpOp.StartSend:
 						{
 							TChannel tChannel = this.Get(result.ChannelId);
-							tChannel.StartSend();
+							if (tChannel == null)
+							{
+								tChannel.StartSend();
+							}
 							break;
 						}
 						case TcpOp.StartRecv:
 						{
 							TChannel tChannel = this.Get(result.ChannelId);
-							tChannel.StartRecv();
+							if (tChannel == null)
+							{
+								tChannel.StartRecv();
+							}
 							break;
 						}
-						case TcpOp.ConnectRemote:
+						case TcpOp.Connect:
 						{
 							TChannel tChannel = this.Get(result.ChannelId);
-							tChannel.ConnectAsync();
+							if (tChannel != null)
+							{
+								tChannel.ConnectAsync();
+							}
 							break;
 						}
 					}
@@ -229,25 +238,38 @@ namespace ET
 					case SocketAsyncOperation.Connect:
 					{
 						TChannel tChannel = this.Get(result.ChannelId);
-						tChannel.OnConnectComplete(e);
+						if (tChannel != null)
+						{
+							tChannel.OnConnectComplete(e);
+						}
+
 						break;
 					}
 					case SocketAsyncOperation.Disconnect:
 					{
 						TChannel tChannel = this.Get(result.ChannelId);
-						tChannel.OnDisconnectComplete(e);
+						if (tChannel != null)
+						{
+							tChannel.OnDisconnectComplete(e);
+						}
 						break;
 					}
 					case SocketAsyncOperation.Receive:
 					{
 						TChannel tChannel = this.Get(result.ChannelId);
-						tChannel.OnRecvComplete(e);
+						if (tChannel != null)
+						{
+							tChannel.OnRecvComplete(e);
+						}
 						break;
 					}
 					case SocketAsyncOperation.Send:
 					{
 						TChannel tChannel = this.Get(result.ChannelId);
-						tChannel.OnSendComplete(e);
+						if (tChannel != null)
+						{
+							tChannel.OnSendComplete(e);
+						}
 						break;
 					}
 					default: