From 9446cf627d1338c0b1af43a4615e1d553ac1322c Mon Sep 17 00:00:00 2001 From: tanghai Date: Wed, 20 Jun 2018 21:07:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=E7=BD=91=E7=BB=9C=E5=B1=82=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=80=E4=BA=9Bbug=202.=E7=BB=84=E4=BB=B6Destroy=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E5=BA=94=E8=AF=A5=E5=9C=A8=E9=87=8A=E6=94=BE=E5=89=8D?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=203.=E4=BF=AE=E5=A4=8D:=20Actor=E5=9C=A8?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=9A=84=E6=97=B6=E5=80=99=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=204.=E4=BF=AE=E5=A4=8D:=20AddComponent(Compo?= =?UTF-8?q?nent)=E6=96=B9=E6=B3=95=E6=B2=A1=E6=9C=89=E8=AE=BE=E7=BD=AEpare?= =?UTF-8?q?nt=20=E6=84=9F=E8=B0=A2:=E6=B8=90=E6=B8=90=EF=BC=8C=E5=93=B2?= =?UTF-8?q?=E5=AD=A6=E7=BB=85=E5=A3=AB=20=E6=8F=90=E5=87=BA=E4=B8=8A?= =?UTF-8?q?=E8=BF=B0bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Module/Actor/ActorMessageSenderSystem.cs | 5 ++ Unity/Assets/Scripts/Base/Object/Component.cs | 6 +-- Unity/Assets/Scripts/Base/Object/Entity.cs | 10 ++-- Unity/Assets/Scripts/Entity/Hotfix.cs | 2 +- .../Scripts/Module/Message/ErrorCode.cs | 47 +++++++++++++------ .../Module/Message/Network/KCP/KChannel.cs | 10 +++- .../Module/Message/Network/KCP/KService.cs | 20 ++++---- .../Module/Message/Network/TCP/TChannel.cs | 16 +------ .../Assets/Scripts/Module/Message/Session.cs | 6 +-- Unity/Hotfix/Base/Object/Component.cs | 6 +-- Unity/Hotfix/Base/Object/Entity.cs | 8 ++-- Unity/Hotfix/Module/Message/Session.cs | 6 +-- 12 files changed, 82 insertions(+), 60 deletions(-) diff --git a/Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs b/Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs index e3c4cde0..77872f73 100644 --- a/Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs +++ b/Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs @@ -128,6 +128,11 @@ namespace ETHotfix return; } + if (actorTask.ActorMessage == null) + { + return; + } + await self.RunTask(actorTask); } } diff --git a/Unity/Assets/Scripts/Base/Object/Component.cs b/Unity/Assets/Scripts/Base/Object/Component.cs index 012a4f0d..2a131791 100644 --- a/Unity/Assets/Scripts/Base/Object/Component.cs +++ b/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() diff --git a/Unity/Assets/Scripts/Base/Object/Entity.cs b/Unity/Assets/Scripts/Base/Object/Entity.cs index c876332e..f19972fa 100644 --- a/Unity/Assets/Scripts/Base/Object/Entity.cs +++ b/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) { diff --git a/Unity/Assets/Scripts/Entity/Hotfix.cs b/Unity/Assets/Scripts/Entity/Hotfix.cs index 08289ddf..259f2d5d 100644 --- a/Unity/Assets/Scripts/Entity/Hotfix.cs +++ b/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(); } return this.appDomain.LoadedTypes.Values.Select(x => x.ReflectionType); diff --git a/Unity/Assets/Scripts/Module/Message/ErrorCode.cs b/Unity/Assets/Scripts/Module/Message/ErrorCode.cs index d7e63e84..548b408a 100644 --- a/Unity/Assets/Scripts/Module/Message/ErrorCode.cs +++ b/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; + } } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs b/Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs index cefbad51..42ab3582 100644 --- a/Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs +++ b/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; } diff --git a/Unity/Assets/Scripts/Module/Message/Network/KCP/KService.cs b/Unity/Assets/Scripts/Module/Message/Network/KCP/KService.cs index c841c882..46aabb1d 100644 --- a/Unity/Assets/Scripts/Module/Message/Network/KCP/KService.cs +++ b/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) { diff --git a/Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs b/Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs index d8a7ab88..4fe91c09 100644 --- a/Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs +++ b/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; } diff --git a/Unity/Assets/Scripts/Module/Message/Session.cs b/Unity/Assets/Scripts/Module/Message/Session.cs index 3534ee8d..31f18c49 100644 --- a/Unity/Assets/Scripts/Module/Message/Session.cs +++ b/Unity/Assets/Scripts/Module/Message/Session.cs @@ -62,7 +62,7 @@ namespace ETModel foreach (Action 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); } diff --git a/Unity/Hotfix/Base/Object/Component.cs b/Unity/Hotfix/Base/Object/Component.cs index 843a2dba..d621f1b5 100644 --- a/Unity/Hotfix/Base/Object/Component.cs +++ b/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() diff --git a/Unity/Hotfix/Base/Object/Entity.cs b/Unity/Hotfix/Base/Object/Entity.cs index 41051089..dc46e54d 100644 --- a/Unity/Hotfix/Base/Object/Entity.cs +++ b/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) { diff --git a/Unity/Hotfix/Module/Message/Session.cs b/Unity/Hotfix/Module/Message/Session.cs index c350add1..b57c91b0 100644 --- a/Unity/Hotfix/Module/Message/Session.cs +++ b/Unity/Hotfix/Module/Message/Session.cs @@ -40,7 +40,7 @@ namespace ETHotfix foreach (Action 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); } -- GitLab