瀏覽代碼

【优化】客户端ping包轮询调整

johnclot69 1 年之前
父節點
當前提交
c241885fd1

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

@@ -63,7 +63,7 @@ namespace ET.Client
                 clientScene.AddComponent<SessionComponent>().Session = gateSession;
                 var player = clientScene.GetComponent<PlayerComponent>();
                 //player.Token = r2CLogin.Token;
-                
+
                 //var player = g2CLoginGate.Players[0];
                 //TODO:选角进入
                 //使用简化流程直接进入游戏

+ 51 - 42
Unity/Assets/Scripts/Codes/Hotfix/Client/Ping/PingComponentSystem.cs

@@ -2,63 +2,72 @@ using System;
 
 namespace ET.Client
 {
-    [ObjectSystem]
-    public class PingComponentAwakeSystem: AwakeSystem<PingComponent>
+    [FriendOf(typeof (PingComponent))]
+    public static class PingComponentSystem
     {
-        protected override void Awake(PingComponent self)
+        [Invoke(TimerInvokeType.ClientPing)]
+        public class Ping: ATimer<PingComponent>
         {
-            PingAsync(self).Coroutine();
+            protected override void Run(PingComponent self)
+            {
+                self.PingAsync().Coroutine();
+            }
         }
 
-        private static async ETTask PingAsync(PingComponent self)
+        [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;
+            }
+        }
+
+        private static async ETTask PingAsync(this PingComponent self)
         {
             Session session = self.GetParent<Session>();
             long instanceId = self.InstanceId;
-            
-            while (true)
+
+            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 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;
 
-                    long time2 = TimeHelper.ClientNow();
-                    self.Ping = time2 - time1;
-                    
-                    TimeInfo.Instance.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
+                TimeInfo.Instance.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
 
-                    await TimerComponent.Instance.WaitAsync(2000);
-                }
-                catch (RpcException e)
-                {
-                    // session断开导致ping rpc报错,记录一下即可,不需要打成error
-                    Log.Info($"ping error: {self.Id} {e.Error}");
-                    return;
-                }
-                catch (Exception e)
-                {
-                    Log.Error($"ping error: \n{e}");
-                }
+                await TimerComponent.Instance.WaitAsync(2000);
+            }
+            catch (RpcException e)
+            {
+                // session断开导致ping rpc报错,记录一下即可,不需要打成error
+                Log.Info($"ping error: {self.Id} {e.Error}");
+            }
+            catch (Exception e)
+            {
+                Log.Error($"ping error: \n{e}");
             }
         }
     }
-
-    [ObjectSystem]
-    public class PingComponentDestroySystem: DestroySystem<PingComponent>
-    {
-        protected override void Destroy(PingComponent self)
-        {
-            self.Ping = default;
-        }
-    }
-}
+}

+ 2 - 1
Unity/Assets/Scripts/Codes/Model/Client/Ping/PingComponent.cs

@@ -4,5 +4,6 @@ namespace ET.Client
     public class PingComponent: Entity, IAwake, IDestroy
     {
         public long Ping; //延迟值
+        public long Timer;
     }
-}
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/TimerInvokeType.cs

@@ -10,7 +10,7 @@
         public const int ActorMessageSenderChecker = 103;
 
         // 逻辑层的timer type 200-300
-        public const int MoveTimer = 201;
+        public const int ClientPing = 201;
         public const int AITimer = 202;
         public const int SessionAcceptTimeout = 203;
         public const int HeartBeatTimeout = 204;