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

修复一个协程锁的bug,上次修复并不彻底,顺带加上测试代码,进入场景按Q W进行测试

tanghai 2 лет назад
Родитель
Сommit
0d73d63fb1

+ 4 - 2
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs

@@ -39,7 +39,8 @@ namespace ET
             return self.CurrentCoroutineLock;
         }
 
-        public static void Notify(this CoroutineLockQueue self, int level)
+        // 返回值,是否找到了一个有效的协程锁
+        public static bool Notify(this CoroutineLockQueue self, int level)
         {
             // 有可能WaitCoroutineLock已经超时抛出异常,所以要找到一个未处理的WaitCoroutineLock
             while (self.queue.Count > 0)
@@ -54,8 +55,9 @@ namespace ET
                 CoroutineLock coroutineLock = self.AddChild<CoroutineLock, int, long, int>(self.type, self.Id, level, true);
 
                 waitCoroutineLock.SetResult(coroutineLock);
-                break;
+                return true;
             }
+            return false;
         }
     }
     

+ 1 - 3
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueueType.cs

@@ -41,9 +41,7 @@ namespace ET
                 return;
             }
 
-            queue.Notify(level);
-            
-            if (queue.Count == 0)
+            if (queue.Notify(level))
             {
                 self.Remove(key);
             }

+ 31 - 0
Unity/Assets/Scripts/HotfixView/Client/Demo/Opera/OperaComponentSystem.cs

@@ -26,6 +26,16 @@ namespace ET.Client
                     self.Root().GetComponent<ClientSenderCompnent>().Send(c2MPathfindingResult);
                 }
             }
+            
+            if (Input.GetKeyDown(KeyCode.Q))
+            {
+                self.Test1().Coroutine();
+            }
+                
+            if (Input.GetKeyDown(KeyCode.W))
+            {
+                self.Test2().Coroutine();
+            }
 
             if (Input.GetKeyDown(KeyCode.R))
             {
@@ -39,5 +49,26 @@ namespace ET.Client
                 self.Root().GetComponent<ClientSenderCompnent>().Call(c2MTransferMap).Coroutine();
             }
         }
+        
+        private static async ETTask Test1(this OperaComponent self)
+        {
+            Log.Debug($"Croutine 1 start1 ");
+            using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(1, 20000, 3000))
+            {
+                await self.Root().GetComponent<TimerComponent>().WaitAsync(6000);
+            }
+
+            Log.Debug($"Croutine 1 end1");
+        }
+            
+        private static async ETTask Test2(this OperaComponent self)
+        {
+            Log.Debug($"Croutine 2 start2");
+            using (await self.Root().GetComponent<CoroutineLockComponent>().Wait(1, 20000, 3000))
+            {
+                await self.Root().GetComponent<TimerComponent>().WaitAsync(1000);
+            }
+            Log.Debug($"Croutine 2 end2");
+        }
     }
 }