Просмотр исходного кода

1.网络层修复一些bug
2.组件Destroy事件应该在释放前触发
3.修复: Actor在删除的时候抛出异常
4.修复: AddComponent(Component)方法没有设置parent
感谢:渐渐,哲学绅士 提出上述bug

tanghai 7 лет назад
Родитель
Сommit
9446cf627d

+ 5 - 0
Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs

@@ -128,6 +128,11 @@ namespace ETHotfix
 						return;
 					}
 
+					if (actorTask.ActorMessage == null)
+					{
+						return;
+					}
+
 					await self.RunTask(actorTask);
 				}
 			}

+ 3 - 3
Unity/Assets/Scripts/Base/Object/Component.cs

@@ -74,6 +74,9 @@ namespace ETModel
 			{
 				return;
 			}
+			
+			// 触发Destroy事件
+			Game.EventSystem.Destroy(this);
 
 			Game.EventSystem.Remove(this.InstanceId);
 
@@ -83,9 +86,6 @@ namespace ETModel
 			{
 				Game.ObjectPool.Recycle(this);
 			}
-
-			// 触发Destroy事件
-			Game.EventSystem.Destroy(this);
 		}
 
 		public virtual void BeginSerialize()

+ 6 - 4
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -33,9 +33,7 @@ namespace ETModel
 			{
 				return;
 			}
-
-			base.Dispose();
-
+			
 			foreach (Component component in this.GetComponents())
 			{
 				try
@@ -47,7 +45,9 @@ namespace ETModel
 					Log.Error(e);
 				}
 			}
-
+			
+			base.Dispose();
+			
 			this.components.Clear();
 			this.componentDict.Clear();
 		}
@@ -59,6 +59,8 @@ namespace ETModel
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
 			}
+			
+			component.Parent = this;
 
 			if (component is ISerializeToEntity)
 			{

+ 1 - 1
Unity/Assets/Scripts/Entity/Hotfix.cs

@@ -39,7 +39,7 @@ namespace ETModel
 #if ILRuntime
 			if (this.appDomain == null)
 			{
-				return new Type[0];
+				return new List<Type>();
 			}
 
 			return this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType);

+ 33 - 14
Unity/Assets/Scripts/Module/Message/ErrorCode.cs

@@ -3,23 +3,42 @@ namespace ETModel
 	public static class ErrorCode
 	{
 		public const int ERR_Success = 0;
-		public const int ERR_NotFoundActor = 2;
-		public const int ERR_ActorNoMailBoxComponent = 3;
-		public const int ERR_ActorTimeOut = 4;
-		public const int ERR_PacketParserError = 5;
+		
+		// 100000 以上,避免跟SocketError冲突
+		public const int ERR_MyErrorCode = 100000;
+		
 
-		public const int ERR_AccountOrPasswordError = 102;
-		public const int ERR_SessionActorError = 103;
-		public const int ERR_NotFoundUnit = 104;
-		public const int ERR_ConnectGateKeyError = 105;
+		
+		public const int ERR_Exception = 200000;
+		
+		public const int ERR_NotFoundActor = 100002;
+		public const int ERR_ActorNoMailBoxComponent = 100003;
+		public const int ERR_ActorTimeOut = 100004;
+		public const int ERR_PacketParserError = 100005;
 
-		public const int ERR_RpcFail = 2001;
-		public const int ERR_SocketDisconnected = 2002;
-		public const int ERR_ReloadFail = 2003;
-		public const int ERR_ActorLocationNotFound = 2004;
+		public const int ERR_AccountOrPasswordError = 100102;
+		public const int ERR_SessionActorError = 100103;
+		public const int ERR_NotFoundUnit = 100104;
+		public const int ERR_ConnectGateKeyError = 100105;
 
-		public const int ERR_Exception = 100000;
+		public const int ERR_RpcFail = 102001;
+		public const int ERR_SocketDisconnected = 102002;
+		public const int ERR_ReloadFail = 102003;
+		public const int ERR_ActorLocationNotFound = 102004;
 
-		public const int ERR_SessionDispose = 100001;
+		public static bool IsRpcNeedThrowException(int error)
+		{
+			if (error == 0)
+			{
+				return false;
+			}
+
+			if (error > ERR_Exception)
+			{
+				return false;
+			}
+
+			return true;
+		}
 	}
 }

+ 8 - 2
Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs

@@ -39,8 +39,6 @@ namespace ETModel
 
 		public uint RemoteConn;
 
-		public uint lastConnectTime;
-
 		// accept
 		public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
 		{
@@ -103,6 +101,8 @@ namespace ETModel
 			kcp.SetOutput(this.Output);
 			kcp.NoDelay(1, 10, 2, 1);  //fast
 
+			this.lastRecvTime = this.GetService().TimeNow;
+
 			HandleSend();
 		}
 
@@ -141,6 +141,12 @@ namespace ETModel
 			// 如果还没连接上,发送连接请求
 			if (!this.isConnected)
 			{
+				// 5秒连接不上,报错
+				if (timeNow - this.lastRecvTime > 5 * 1000)
+				{
+					this.OnError((int)SocketError.ConnectionRefused);
+					return;
+				}
 				Connect(timeNow);
 				return;
 			}

+ 11 - 9
Unity/Assets/Scripts/Module/Message/Network/KCP/KService.cs

@@ -96,21 +96,22 @@ namespace ETModel
 
 		public void Recv()
 		{
-			while (this.socket.Available > 0)
+			if (this.socket == null)
 			{
-				if (this.IsDisposed)
-				{
-					return;
-				}
+				return;
+			}
 
+			while (this.socket.Available > 0)
+			{
 				int messageLength = 0;
 				try
 				{
 					messageLength = this.socket.ReceiveFrom(this.cache, ref this.ipEndPoint);
 				}
-				catch (Exception)
+				catch (Exception e)
 				{
-					return;
+					Log.Error(e);
+					continue;
 				}
 
 				// 长度小于4,不是正常的消息
@@ -283,6 +284,8 @@ namespace ETModel
 		
 		public override void Update()
 		{
+			this.TimeNow = (uint)TimeHelper.ClientNow();
+			
 			this.Recv();
 			
 			this.TimerOut();
@@ -321,8 +324,7 @@ namespace ETModel
 				return;
 			}
 
-			long timeNow = TimeHelper.ClientNow();
-			this.TimeNow = (uint)timeNow;
+			long timeNow = this.TimeNow;
 			
 			if (timeNow < this.minTime)
 			{

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

@@ -76,6 +76,7 @@ namespace ETModel
 				this.ConnectAsync(this.RemoteAddress);
 				return;
 			}
+			
 			this.StartRecv();
 			this.StartSend();
 		}
@@ -147,15 +148,10 @@ namespace ETModel
 
 		private void OnConnectComplete(object o)
 		{
-			if (this.IsDisposed)
-			{
-				throw new Exception("TChannel已经被Dispose, 不能发送消息");
-			}
 			SocketAsyncEventArgs e = (SocketAsyncEventArgs) o;
 			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
 			if (userTokenInfo.InstanceId != this.InstanceId)
 			{
-				Log.Error($"session disposed!");
 				return;
 			}
 
@@ -203,15 +199,10 @@ namespace ETModel
 
 		private void OnRecvComplete(object o)
 		{
-			if (this.IsDisposed)
-			{
-				throw new Exception("TChannel已经被Dispose, 不能发送消息");
-			}
 			SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
 			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
 			if (userTokenInfo.InstanceId != this.InstanceId)
 			{
-				Log.Error($"session disposed!");
 				return;
 			}
 
@@ -299,15 +290,10 @@ namespace ETModel
 
 		private void OnSendComplete(object o)
 		{
-			if (this.IsDisposed)
-			{
-				throw new Exception("TChannel已经被Dispose, 不能发送消息");
-			}
 			SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
 			UserTokenInfo userTokenInfo = (UserTokenInfo) e.UserToken;
 			if (userTokenInfo.InstanceId != this.InstanceId)
 			{
-				Log.Error($"session disposed!");
 				return;
 			}
 

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

@@ -62,7 +62,7 @@ namespace ETModel
 			
 			foreach (Action<IResponse> action in this.requestCallback.Values.ToArray())
 			{
-				action.Invoke(new ResponseMessage { Error = ErrorCode.ERR_SessionDispose });
+				action.Invoke(new ResponseMessage { Error = this.Error });
 			}
 			
 			this.Error = 0;
@@ -160,7 +160,7 @@ namespace ETModel
 			{
 				try
 				{
-					if (response.Error > ErrorCode.ERR_Exception)
+					if (ErrorCode.IsRpcNeedThrowException(response.Error))
 					{
 						throw new RpcException(response.Error, response.Message);
 					}
@@ -187,7 +187,7 @@ namespace ETModel
 			{
 				try
 				{
-					if (response.Error > ErrorCode.ERR_Exception)
+					if (ErrorCode.IsRpcNeedThrowException(response.Error))
 					{
 						throw new RpcException(response.Error, response.Message);
 					}

+ 3 - 3
Unity/Hotfix/Base/Object/Component.cs

@@ -74,6 +74,9 @@ namespace ETHotfix
 			{
 				return;
 			}
+			
+			// 触发Destroy事件
+			Game.EventSystem.Destroy(this);
 
 			Game.EventSystem.Remove(this.InstanceId);
 
@@ -83,9 +86,6 @@ namespace ETHotfix
 			{
 				Game.ObjectPool.Recycle(this);
 			}
-
-			// 触发Destroy事件
-			Game.EventSystem.Destroy(this);
 		}
 
 		public virtual void BeginSerialize()

+ 5 - 3
Unity/Hotfix/Base/Object/Entity.cs

@@ -33,9 +33,7 @@ namespace ETHotfix
 			{
 				return;
 			}
-
-			base.Dispose();
-
+			
 			foreach (Component component in this.GetComponents())
 			{
 				try
@@ -48,6 +46,8 @@ namespace ETHotfix
 				}
 			}
 
+			base.Dispose();
+
 			this.components.Clear();
 			this.componentDict.Clear();
 		}
@@ -59,6 +59,8 @@ namespace ETHotfix
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
 			}
+			
+			component.Parent = this;
 
 			if (component is ISerializeToEntity)
 			{

+ 3 - 3
Unity/Hotfix/Module/Message/Session.cs

@@ -40,7 +40,7 @@ namespace ETHotfix
 
 			foreach (Action<IResponse> action in this.requestCallback.Values.ToArray())
 			{
-				action.Invoke(new ResponseMessage { Error = ErrorCode.ERR_SessionDispose });
+				action.Invoke(new ResponseMessage { Error = this.session.Error });
 			}
 
 			this.requestCallback.Clear();
@@ -105,7 +105,7 @@ namespace ETHotfix
 			{
 				try
 				{
-					if (response.Error > ErrorCode.ERR_Exception)
+					if (ErrorCode.IsRpcNeedThrowException(response.Error))
 					{
 						throw new RpcException(response.Error, response.Message);
 					}
@@ -133,7 +133,7 @@ namespace ETHotfix
 			{
 				try
 				{
-					if (response.Error > ErrorCode.ERR_Exception)
+					if (ErrorCode.IsRpcNeedThrowException(response.Error))
 					{
 						throw new RpcException(response.Error, response.Message);
 					}