mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
126
Assets/GameScripts/ThirdParty/ETTask/ETTaskHelper.cs
vendored
Normal file
126
Assets/GameScripts/ThirdParty/ETTask/ETTaskHelper.cs
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class ETTaskHelper
|
||||
{
|
||||
public static bool IsCancel(this ETCancellationToken self)
|
||||
{
|
||||
if (self == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return self.IsDispose();
|
||||
}
|
||||
|
||||
private class CoroutineBlocker
|
||||
{
|
||||
private int count;
|
||||
|
||||
private ETTask tcs;
|
||||
|
||||
public CoroutineBlocker(int count)
|
||||
{
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public async ETTask RunSubCoroutineAsync(ETTask task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
--this.count;
|
||||
|
||||
if (this.count <= 0 && this.tcs != null)
|
||||
{
|
||||
ETTask t = this.tcs;
|
||||
this.tcs = null;
|
||||
t.SetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async ETTask WaitAsync()
|
||||
{
|
||||
if (this.count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.tcs = ETTask.Create(true);
|
||||
await tcs;
|
||||
}
|
||||
}
|
||||
|
||||
public static async ETTask WaitAny(List<ETTask> tasks)
|
||||
{
|
||||
if (tasks.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
|
||||
|
||||
foreach (ETTask task in tasks)
|
||||
{
|
||||
coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
|
||||
}
|
||||
|
||||
await coroutineBlocker.WaitAsync();
|
||||
}
|
||||
|
||||
public static async ETTask WaitAny(ETTask[] tasks)
|
||||
{
|
||||
if (tasks.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CoroutineBlocker coroutineBlocker = new CoroutineBlocker(1);
|
||||
|
||||
foreach (ETTask task in tasks)
|
||||
{
|
||||
coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
|
||||
}
|
||||
|
||||
await coroutineBlocker.WaitAsync();
|
||||
}
|
||||
|
||||
public static async ETTask WaitAll(ETTask[] tasks)
|
||||
{
|
||||
if (tasks.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Length);
|
||||
|
||||
foreach (ETTask task in tasks)
|
||||
{
|
||||
coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
|
||||
}
|
||||
|
||||
await coroutineBlocker.WaitAsync();
|
||||
}
|
||||
|
||||
public static async ETTask WaitAll(List<ETTask> tasks)
|
||||
{
|
||||
if (tasks.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CoroutineBlocker coroutineBlocker = new CoroutineBlocker(tasks.Count);
|
||||
|
||||
foreach (ETTask task in tasks)
|
||||
{
|
||||
coroutineBlocker.RunSubCoroutineAsync(task).Coroutine();
|
||||
}
|
||||
|
||||
await coroutineBlocker.WaitAsync();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user