Explorar el Código

TimerComponent加回了NewOnceTimer,主要原因是业务可能有长时间的计时器需求,长时间等待又需要热更

tanghai hace 4 años
padre
commit
c24f91b636

+ 2 - 0
Server/Hotfix/Demo/C2G_LoginGateHandler.cs

@@ -17,6 +17,8 @@ namespace ET
 				reply();
 				return;
 			}
+			
+			session.RemoveComponent<SessionAcceptTimeoutComponent>();
 
 			PlayerComponent playerComponent = scene.GetComponent<PlayerComponent>();
 			Player player = playerComponent.AddChild<Player, string>(account);

+ 1 - 0
Unity/Codes/Hotfix/Module/Message/NetKcpComponentSystem.cs

@@ -77,6 +77,7 @@ namespace ET
             Session session = self.AddChildWithId<Session, AService>(channelId, self.Service);
             session.RemoteAddress = ipEndPoint;
 
+            // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
             session.AddComponent<SessionAcceptTimeoutComponent>();
             // 客户端连接,2秒检查一次recv消息,10秒没有消息则断开
             session.AddComponent<SessionIdleCheckerComponent, int>(NetThreadComponent.checkInteral);

+ 20 - 7
Unity/Codes/Hotfix/Module/Message/SessionAcceptTimeoutComponentSystem.cs

@@ -1,11 +1,29 @@
-namespace ET
+using System;
+
+namespace ET
 {
+    [Timer(TimerType.SessionAcceptTimeout)]
+    public class SessionAcceptTimeout: ATimer<SessionAcceptTimeoutComponent>
+    {
+        public override void Run(SessionAcceptTimeoutComponent self)
+        {
+            try
+            {
+                self.Parent.Dispose();
+            }
+            catch (Exception e)
+            {
+                Log.Error($"move timer error: {self.Id}\n{e}");
+            }
+        }
+    }
+    
     [ObjectSystem]
     public class SessionAcceptTimeoutComponentAwakeSystem: AwakeSystem<SessionAcceptTimeoutComponent>
     {
         public override void Awake(SessionAcceptTimeoutComponent self)
         {
-            //self.Timer = TimerComponent.Instance.NewOnceTimer(5000, () => { self.Parent.Dispose(); });
+            self.Timer = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + 5000, TimerType.SessionAcceptTimeout, self);
         }
     }
 
@@ -17,9 +35,4 @@
             TimerComponent.Instance.Remove(ref self.Timer);
         }
     }
-    
-    public static class SessionAcceptTimeoutComponentSystem
-    {
-        
-    }
 }

+ 29 - 2
Unity/Codes/Model/Core/Timer/TimerComponent.cs

@@ -7,6 +7,7 @@ namespace ET
     {
         None,
         OnceTimer,
+        OnceWaitTimer,
         RepeatedTimer,
     }
     
@@ -181,6 +182,18 @@ namespace ET
             switch (timerAction.TimerClass)
             {
                 case TimerClass.OnceTimer:
+                {
+                    int type = timerAction.Type;
+                    ITimer timer = this.timerActions[type];
+                    if (timer == null)
+                    {
+                        Log.Error($"not found timer action: {type}");
+                        return;
+                    }
+                    timer.Handle(timerAction.Object);
+                    break;
+                }
+                case TimerClass.OnceWaitTimer:
                 {
                     ETTask<bool> tcs = timerAction.Object as ETTask<bool>;
                     this.Remove(timerAction.Id);
@@ -246,7 +259,7 @@ namespace ET
             }
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, tillTime - timeNow, 0, tcs, true);
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - timeNow, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -286,7 +299,7 @@ namespace ET
 
             ETTask<bool> tcs = ETTask<bool>.Create(true);
             
-            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, time, 0, tcs, true);
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, time, 0, tcs, true);
             this.AddTimer(tillTime, timer);
             long timerId = timer.Id;
 
@@ -310,6 +323,20 @@ namespace ET
             }
             return ret;
         }
+        
+        // 用这个优点是可以热更,缺点是回调式的写法,逻辑不连贯。WaitTillAsync不能热更,优点是逻辑连贯。
+        // wait时间短并且逻辑需要连贯的建议WaitTillAsync
+        // wait时间长不需要逻辑连贯的建议用NewOnceTimer
+        public long NewOnceTimer(long tillTime, int type, object args)
+        {
+            if (tillTime < TimeHelper.ServerNow())
+            {
+                Log.Warning($"new once time too small: {tillTime}");
+            }
+            TimerAction timer = this.AddChild<TimerAction, TimerClass, long, int, object>(TimerClass.OnceTimer, tillTime, type, args, true);
+            this.AddTimer(tillTime, timer);
+            return timer.Id;
+        }
 
         public long NewFrameTimer(int type, object args)
         {

+ 1 - 1
Unity/Codes/Model/Demo/TimerType.cs

@@ -5,7 +5,7 @@
         // 框架层0-1000,逻辑层的timer type 1000-9999
         public const int MoveTimer = 1001;
         public const int AITimer = 1002;
-        
+        public const int SessionAcceptTimeout = 1003;
         // 不能超过10000
     }
 }