Procházet zdrojové kódy

修改ping系统,将ping包嵌入到idleCheck系统中,不抢占繁忙时的session通信

大爷 před 1 rokem
rodič
revize
133b2bc666
20 změnil soubory, kde provedl 80 přidání a 221 odebrání
  1. 5 10
      DotNet/Hotfix/Module/Message/NetServerComponentSystem.cs
  2. 1 3
      DotNet/Hotfix/Scenes/Game/Handler/C2G_PingHandler.cs
  3. 0 57
      DotNet/Hotfix/Scenes/Game/Player/HeartBeatTimeoutComponentSystem.cs
  4. 1 1
      DotNet/Hotfix/Session/SessionIdleCheckerComponentSystem.cs
  5. 2 2
      DotNet/Model/Module/Message/SessionIdleCheckerComponent.cs
  6. 0 8
      DotNet/Model/Scenes/Game/Player/HeartBeatTimeoutComponent.cs
  7. 4 1
      Unity/Assets/Scripts/Codes/Hotfix/Client/Login/LoginHelper.cs
  8. 0 8
      Unity/Assets/Scripts/Codes/Hotfix/Client/Ping.meta
  9. 0 72
      Unity/Assets/Scripts/Codes/Hotfix/Client/Ping/PingComponentSystem.cs
  10. 0 11
      Unity/Assets/Scripts/Codes/Hotfix/Client/Ping/PingComponentSystem.cs.meta
  11. 1 2
      Unity/Assets/Scripts/Codes/Hotfix/Client/Router/RouterHelper.cs
  12. 54 0
      Unity/Assets/Scripts/Codes/Hotfix/Client/Session/SessionIdleCheckerComponentSystem.cs
  13. 0 0
      Unity/Assets/Scripts/Codes/Hotfix/Client/Session/SessionIdleCheckerComponentSystem.cs.meta
  14. 0 8
      Unity/Assets/Scripts/Codes/Model/Client/Ping.meta
  15. 0 9
      Unity/Assets/Scripts/Codes/Model/Client/Ping/PingComponent.cs
  16. 0 11
      Unity/Assets/Scripts/Codes/Model/Client/Ping/PingComponent.cs.meta
  17. 8 0
      Unity/Assets/Scripts/Codes/Model/Client/Session/SessionIdleCheckerComponent.cs
  18. 1 1
      Unity/Assets/Scripts/Codes/Model/Client/Session/SessionIdleCheckerComponent.cs.meta
  19. 2 10
      Unity/Assets/Scripts/Codes/Model/Share/Const/ConstValue.cs
  20. 1 7
      Unity/Assets/Scripts/Codes/Model/Share/Module/Message/Session.cs

+ 5 - 10
DotNet/Hotfix/Module/Message/NetServerComponentSystem.cs

@@ -61,20 +61,15 @@ namespace ET.Server
                 return;
             }
 
-            if (ConstValue.IsDebug == 1)
-            {
-                return;
-            }
-
             // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
             session.AddComponent<SessionAcceptTimeoutComponent>();
-            // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
-            session.AddComponent<SessionIdleCheckerComponent>();
 
-            if (session.DomainScene().SceneType == SceneType.Game)
+            // 客户端连接,检查是否处于空闲,空闲则close
+            if (ConstValue.SessionTimeoutTime > 0)
             {
-                // 挂上心跳超时检测组件
-                session.AddComponent<HeartBeatTimeoutComponent>();
+#pragma warning disable CS0162 // 检测到无法访问的代码
+                session.AddComponent<SessionIdleCheckerComponent>();
+#pragma warning restore CS0162 // 检测到无法访问的代码
             }
         }
 

+ 1 - 3
DotNet/Hotfix/Scenes/Game/Handler/C2G_PingHandler.cs

@@ -10,9 +10,7 @@ namespace ET.Server
 	{
 		protected override async ETTask Run(Session session, C2G_Ping request, G2C_Ping response, Action reply)
         {
-            session.LastPingTime = TimeHelper.ClientNow();
-
-			response.Time = TimeHelper.ServerNow();
+			response.Time = TimeHelper.ClientNow();
 			reply();
 			await ETTask.CompletedTask;
 		}

+ 0 - 57
DotNet/Hotfix/Scenes/Game/Player/HeartBeatTimeoutComponentSystem.cs

@@ -1,57 +0,0 @@
-using System;
-
-namespace ET.Server
-{
-    [Invoke(TimerInvokeType.HeartBeatTimeout)]
-    public class HeartBeatCheck: ATimer<HeartBeatTimeoutComponent>
-    {
-        protected override void Run(HeartBeatTimeoutComponent self)
-        {
-            try
-            {
-                self.Check();
-            }
-            catch (Exception e)
-            {
-                Log.Error($"move timer error: {self.Id}\n{e}");
-            }
-        }
-    }
-
-    [ObjectSystem]
-    public class HeartBeatTimeoutComponentAwakeSystem: AwakeSystem<HeartBeatTimeoutComponent>
-    {
-        protected override void Awake(HeartBeatTimeoutComponent self)
-        {
-            self.Timer = TimerComponent.Instance.NewRepeatedTimer( Math.Max( ConstValue.HeartBeatTimeoutTime, 5 ), TimerInvokeType.HeartBeatTimeout, self);
-        }
-    }
-
-    [ObjectSystem]
-    public class HeartBeatTimeoutComponentDestroySystem: DestroySystem<HeartBeatTimeoutComponent>
-    {
-        protected override void Destroy(HeartBeatTimeoutComponent self)
-        {
-            TimerComponent.Instance?.Remove(ref self.Timer);
-        }
-    }
-
-    public static class HeartBeatTimeoutComponentSystem
-    {
-        public static void Check(this HeartBeatTimeoutComponent self)
-        {
-            Session session = self.GetParent<Session>();
-            long timeNow = TimeHelper.ClientNow();
-
-            if (session.LastPingTime == 0 || timeNow - session.LastPingTime < ConstValue.HeartBeatTimeoutTime)
-            {
-                return;
-            }
-
-            Log.Info($"heartBeat timeout: {session.Id} {timeNow} {session.LastPingTime} {timeNow - session.LastPingTime}");
-            session.Error = ErrorCore.ERR_HeartBeatTimeout;
-
-            session.Dispose();
-        }
-    }
-}

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs → DotNet/Hotfix/Session/SessionIdleCheckerComponentSystem.cs

@@ -23,7 +23,7 @@ namespace ET
     {
         protected override void Awake(SessionIdleCheckerComponent self)
         {
-            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer( Math.Max( ConstValue.SessionTimeoutTime / 3, 5 ), TimerInvokeType.SessionIdleChecker, self);
+            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(ConstValue.SessionTimeoutTime / 2 + 100, TimerInvokeType.SessionIdleChecker, self);
         }
     }
 

+ 2 - 2
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionIdleCheckerComponent.cs → DotNet/Model/Module/Message/SessionIdleCheckerComponent.cs

@@ -1,8 +1,8 @@
-namespace ET
+namespace ET
 {
     [ComponentOf(typeof(Session))]
     public class SessionIdleCheckerComponent: Entity, IAwake, IDestroy
     {
         public long RepeatedTimer;
     }
-}
+}

+ 0 - 8
DotNet/Model/Scenes/Game/Player/HeartBeatTimeoutComponent.cs

@@ -1,8 +0,0 @@
-namespace ET.Server
-{
-    [ComponentOf(typeof (Session))]
-    public class HeartBeatTimeoutComponent: Entity, IAwake, IDestroy
-    {
-        public long Timer;
-    }
-}

+ 4 - 1
Unity/Assets/Scripts/Codes/Hotfix/Client/Login/LoginHelper.cs

@@ -57,9 +57,12 @@ namespace ET.Client
                     Log.Error($"登陆game错误...errCode={g2CLoginGate.Error}");
                     return g2CLoginGate.Error;
                 }
+                if(ConstValue.SessionTimeoutTime > 0)
+                {
+                    gateSession.AddComponent<SessionIdleCheckerComponent>();
+                }
 
                 // 保存账号信息
-                gateSession.AddComponent<PingComponent>();
                 clientScene.AddComponent<SessionComponent>().Session = gateSession;
                 var player = clientScene.GetComponent<PlayerComponent>();
 

+ 0 - 8
Unity/Assets/Scripts/Codes/Hotfix/Client/Ping.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: fdd19f6ca1a030947b7d6982dd0655dd
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 72
Unity/Assets/Scripts/Codes/Hotfix/Client/Ping/PingComponentSystem.cs

@@ -1,72 +0,0 @@
-using System;
-
-namespace ET.Client
-{
-    [FriendOf(typeof (PingComponent))]
-    public static class PingComponentSystem
-    {
-        [Invoke(TimerInvokeType.ClientPing)]
-        public class Ping: ATimer<PingComponent>
-        {
-            protected override void Run(PingComponent self)
-            {
-                self.PingAsync().Coroutine();
-            }
-        }
-
-        [ObjectSystem]
-        public class PingComponentAwakeSystem: AwakeSystem<PingComponent>
-        {
-            protected override void Awake(PingComponent self)
-            {
-                self.Timer = TimerComponent.Instance.NewRepeatedTimer( Math.Max( 3000, 5 ), TimerInvokeType.ClientPing, self);
-            }
-        }
-
-        [ObjectSystem]
-        public class PingComponentDestroySystem: DestroySystem<PingComponent>
-        {
-            protected override void Destroy(PingComponent self)
-            {
-                self.Ping = default;
-                TimerComponent.Instance?.Remove(ref self.Timer);
-            }
-        }
-
-        private static async ETTask PingAsync(this PingComponent self)
-        {
-            Session session = self.GetParent<Session>();
-            long instanceId = self.InstanceId;
-
-            if (self.InstanceId != instanceId)
-            {
-                return;
-            }
-
-            long time1 = TimeHelper.ClientNow();
-            try
-            {
-                G2C_Ping response = await session.Call(new C2G_Ping()) as G2C_Ping;
-
-                if (self.InstanceId != instanceId)
-                {
-                    return;
-                }
-
-                long time2 = TimeHelper.ClientNow();
-                self.Ping = time2 - time1;
-
-                TimeInfo.Instance.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
-            }
-            catch (RpcException e)
-            {
-                // session断开导致ping rpc报错,记录一下即可,不需要打成error
-                Log.Info($"ping error: {self.Id} {e.Error}");
-            }
-            catch (Exception e)
-            {
-                Log.Error($"ping error: \n{e}");
-            }
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Hotfix/Client/Ping/PingComponentSystem.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: df3747692b8ee23439aa241d5dcfabdc
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 1 - 2
Unity/Assets/Scripts/Codes/Hotfix/Client/Router/RouterHelper.cs

@@ -19,7 +19,6 @@ namespace ET.Client
             Log.Info($"get router: {recvLocalConn} {routerAddress}");
 
             Session routerSession = clientScene.GetComponent<NetClientComponent>().Create(routerAddress, address, recvLocalConn);
-            routerSession.AddComponent<PingComponent>();
             routerSession.AddComponent<RouterCheckComponent>();
             
             return routerSession;
@@ -105,4 +104,4 @@ namespace ET.Client
             }
         }
     }
-}
+}

+ 54 - 0
Unity/Assets/Scripts/Codes/Hotfix/Client/Session/SessionIdleCheckerComponentSystem.cs

@@ -0,0 +1,54 @@
+using System;
+
+namespace ET.Client
+{
+    [Invoke(TimerInvokeType.SessionIdleChecker)]
+    public class SessionIdleChecker: ATimer<SessionIdleCheckerComponent>
+    {
+        protected override void Run(SessionIdleCheckerComponent self)
+        {
+            try
+            {
+                self.Check();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"idle check error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
+    [ObjectSystem]
+    public class SessionIdleCheckerComponentAwakeSystem: AwakeSystem<SessionIdleCheckerComponent>
+    {
+        protected override void Awake(SessionIdleCheckerComponent self)
+        {
+            self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(ConstValue.SessionTimeoutTime / 2 +100, TimerInvokeType.SessionIdleChecker, self);
+        }
+    }
+
+    [ObjectSystem]
+    public class SessionIdleCheckerComponentDestroySystem: DestroySystem<SessionIdleCheckerComponent>
+    {
+        protected override void Destroy(SessionIdleCheckerComponent self)
+        {
+            TimerComponent.Instance?.Remove(ref self.RepeatedTimer);
+        }
+    }
+
+    public static class SessionIdleCheckerComponentSystem
+    {
+        public static void Check(this SessionIdleCheckerComponent self)
+        {
+            Session session = self.GetParent<Session>();
+            long timeNow = TimeHelper.ClientNow();
+
+            if (timeNow - session.LastRecvTime < ConstValue.SessionTimeoutTime/2 && timeNow - session.LastSendTime < ConstValue.SessionTimeoutTime/2)
+            {
+                return;
+            }
+
+            session.Send(new C2G_Ping());
+        }
+    }
+}

+ 0 - 0
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/SessionIdleCheckerComponentSystem.cs.meta → Unity/Assets/Scripts/Codes/Hotfix/Client/Session/SessionIdleCheckerComponentSystem.cs.meta


+ 0 - 8
Unity/Assets/Scripts/Codes/Model/Client/Ping.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: c0c2b8f9a2a9a3d448fe70c168f31902
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 9
Unity/Assets/Scripts/Codes/Model/Client/Ping/PingComponent.cs

@@ -1,9 +0,0 @@
-namespace ET.Client
-{
-    [ComponentOf(typeof(Session))]
-    public class PingComponent: Entity, IAwake, IDestroy
-    {
-        public long Ping; //延迟值
-        public long Timer;
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Model/Client/Ping/PingComponent.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: f524589ddb21a4646bfd9cea4ba9bb1b
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 8 - 0
Unity/Assets/Scripts/Codes/Model/Client/Session/SessionIdleCheckerComponent.cs

@@ -0,0 +1,8 @@
+namespace ET.Client
+{
+    [ComponentOf(typeof(Session))]
+    public class SessionIdleCheckerComponent: Entity, IAwake, IDestroy
+    {
+        public long RepeatedTimer;
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/SessionIdleCheckerComponent.cs.meta → Unity/Assets/Scripts/Codes/Model/Client/Session/SessionIdleCheckerComponent.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 1d309c1ef46c0eb4bb58c14b6716c032
+guid: 0a300da748711e440bc0b3198abb6cd0
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 2 - 10
Unity/Assets/Scripts/Codes/Model/Share/Const/ConstValue.cs

@@ -5,16 +5,8 @@
      */
     public static class ConstValue
     {
-        /** session过期时间 **/
-        public const int SessionTimeoutTime = 30 * 1000;
-        /** 心跳超时时间 **/
-        public const int HeartBeatTimeoutTime = 3 * 1000;
-        /** 是否调试 (1=是,关闭服务器心跳检测和服务器session超时检测) **/
-#if DEBUG
-        public static int IsDebug = 1;
-#else
-        public static int IsDebug = 0;
-#endif
+        //session过期时间(毫秒),设置小于等于0时,session永不过期
+        public const int SessionTimeoutTime = 5 * 1000;
 
         public static int MoneyShowVipName = 1040;  //10.4元享受vip待遇
     }

+ 1 - 7
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/Session.cs

@@ -166,11 +166,5 @@ namespace ET
             get;
             set;
         }
-
-        public long LastPingTime
-        {
-            get;
-            set;
-        }
     }
-}
+}