Răsfoiți Sursa

简化ETTaskHelper

tanghai 2 ani în urmă
părinte
comite
5305bc6f6e

+ 8 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Scenes/Benchmark/BenchmarkClientComponentSystem.cs

@@ -27,13 +27,19 @@ namespace ET.Server
             NetClientComponent netClientComponent = scene.AddComponent<NetClientComponent, AddressFamily>(AddressFamily.InterNetwork);
 
             using Session session = netClientComponent.Create(StartSceneConfigCategory.Instance.BenchmarkServer.OuterIPPort);
-            List<ETTask<IResponse>> list = new List<ETTask<IResponse>>(100000);
+            List<ETTask> list = new List<ETTask>(100000);
+
+            async ETTask Call(Session s)
+            {
+                await s.Call(new C2G_Benchmark());
+            }
+            
             for (int j = 0; j < 100000000; ++j)
             {
                 list.Clear();
                 for (int i = 0; i < list.Capacity; ++i)
                 {
-                    list.Add(session.Call(new C2G_Benchmark()));
+                    list.Add(Call(session));
                 }
                 await ETTaskHelper.WaitAll(list);
             }

+ 8 - 0
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Scenes/Robot/Case/RobotCase_FirstCase.cs

@@ -12,6 +12,14 @@ namespace ET.Server
             // 创建了两个机器人,生命周期是RobotCase,RobotCase_FirstCase.Run执行结束,机器人就会删除
             await robotCase.NewRobot(2, robots);
 
+            using ListComponent<ETTask> robotsTasks = ListComponent<ETTask>.Create();
+            for (int i = 0; i < 50; ++i)
+            {
+                robotsTasks.Add(robotCase.NewRobot(i, robots));
+            }
+
+            await ETTaskHelper.WaitAll(robotsTasks);
+
             foreach (Scene robotScene in robots)
             {
                 M2C_TestRobotCase response = await robotScene.GetComponent<Client.SessionComponent>().Session.Call(new C2M_TestRobotCase() {N = robotScene.Zone}) as M2C_TestRobotCase;

+ 37 - 139
Unity/Assets/Scripts/ThirdParty/ETTask/ETTaskHelper.cs

@@ -18,211 +18,109 @@ namespace ET
         {
             private int count;
 
-            private List<ETTask> tcss = new List<ETTask>();
+            private ETTask tcs;
 
             public CoroutineBlocker(int count)
             {
                 this.count = count;
             }
-
-            public async ETTask WaitAsync()
+            
+            public async ETTask RunSubCoroutineAsync(ETTask task)
             {
-                --this.count;
-                if (this.count < 0)
+                try
                 {
-                    return;
+                    await task;
                 }
-                if (this.count == 0)
+                finally
                 {
-                    List<ETTask> t = this.tcss;
-                    this.tcss = null;
-                    foreach (ETTask ttcs in t)
+                    --this.count;
+                
+                    if (this.count <= 0 && this.tcs != null)
                     {
-                        ttcs.SetResult();
+                        ETTask t = this.tcs;
+                        this.tcs = null;
+                        t.SetResult();
                     }
-
-                    return;
                 }
-                ETTask tcs = ETTask.Create(true);
-
-                tcss.Add(tcs);
-                await tcs;
-            }
-        }
-
-        public static async ETTask<bool> WaitAny<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
-        {
-            if (tasks.Length == 0)
-            {
-                return false;
-            }
-
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
-
-            foreach (ETTask<T> task in tasks)
-            {
-                RunOneTask(task).Coroutine();
-            }
-
-            async ETVoid RunOneTask(ETTask<T> task)
-            {
-                await task;
-                await coroutineBlocker.WaitAsync();
             }
 
-            await coroutineBlocker.WaitAsync();
-
-            if (cancellationToken == null)
+            public async ETTask WaitAsync()
             {
-                return true;
+                if (this.count <= 0)
+                {
+                    return;
+                }
+                this.tcs = ETTask.Create(true);
+                await tcs;
             }
-
-            return !cancellationToken.IsCancel();
         }
 
-        public static async ETTask<bool> WaitAny(ETTask[] tasks, ETCancellationToken cancellationToken = null)
+        public static async ETTask WaitAny(List<ETTask> tasks)
         {
-            if (tasks.Length == 0)
+            if (tasks.Count == 0)
             {
-                return false;
+                return;
             }
 
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(2);
+            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
 
             foreach (ETTask task in tasks)
             {
-                RunOneTask(task).Coroutine();
-            }
-
-            async ETVoid RunOneTask(ETTask task)
-            {
-                await task;
-                await coroutineBlocker.WaitAsync();
+                coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
             }
 
             await coroutineBlocker.WaitAsync();
-
-            if (cancellationToken == null)
-            {
-                return true;
-            }
-
-            return !cancellationToken.IsCancel();
         }
 
-        public static async ETTask<bool> WaitAll<T>(ETTask<T>[] tasks, ETCancellationToken cancellationToken = null)
+        public static async ETTask WaitAny(ETTask[] tasks)
         {
             if (tasks.Length == 0)
             {
-                return false;
+                return;
             }
 
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
+            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
 
-            foreach (ETTask<T> task in tasks)
-            {
-                RunOneTask(task).Coroutine();
-            }
-
-            async ETVoid RunOneTask(ETTask<T> task)
-            {
-                await task;
-                await coroutineBlocker.WaitAsync();
-            }
-
-            await coroutineBlocker.WaitAsync();
-
-            if (cancellationToken == null)
-            {
-                return true;
-            }
-
-            return !cancellationToken.IsCancel();
-        }
-
-        public static async ETTask<bool> WaitAll<T>(List<ETTask<T>> tasks, ETCancellationToken cancellationToken = null)
-        {
-            if (tasks.Count == 0)
-            {
-                return false;
-            }
-
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
-
-            foreach (ETTask<T> task in tasks)
-            {
-                RunOneTask(task).Coroutine();
-            }
-
-            async ETVoid RunOneTask(ETTask<T> task)
+            foreach (ETTask task in tasks)
             {
-                await task;
-                await coroutineBlocker.WaitAsync();
+                coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
             }
 
             await coroutineBlocker.WaitAsync();
-
-            if (cancellationToken == null)
-            {
-                return true;
-            }
-
-            return !cancellationToken.IsCancel();
         }
 
-        public static async ETTask<bool> WaitAll(ETTask[] tasks, ETCancellationToken cancellationToken = null)
+        public static async ETTask WaitAll(ETTask[] tasks)
         {
             if (tasks.Length == 0)
             {
-                return false;
+                return;
             }
 
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length + 1);
+            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length);
 
             foreach (ETTask task in tasks)
             {
-                RunOneTask(task).Coroutine();
+                coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
             }
 
             await coroutineBlocker.WaitAsync();
-
-            async ETVoid RunOneTask(ETTask task)
-            {
-                await task;
-                await coroutineBlocker.WaitAsync();
-            }
-
-            if (cancellationToken == null)
-            {
-                return true;
-            }
-
-            return !cancellationToken.IsCancel();
         }
 
-        public static async ETTask<bool> WaitAll(List<ETTask> tasks, ETCancellationToken cancellationToken = null)
+        public static async ETTask WaitAll(List<ETTask> tasks)
         {
             if (tasks.Count == 0)
             {
-                return false;
+                return;
             }
 
-            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count + 1);
+            CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count);
 
             foreach (ETTask task in tasks)
             {
-                RunOneTask(task).Coroutine();
+                coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
             }
 
             await coroutineBlocker.WaitAsync();
-
-            async ETVoid RunOneTask(ETTask task)
-            {
-                await task;
-                await coroutineBlocker.WaitAsync();
-            }
-
-            return !cancellationToken.IsCancel();
         }
     }
 }