[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -0,0 +1,63 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security;
namespace ET
{
public struct AsyncETTaskCompletedMethodBuilder
{
// 1. Static Create method.
[DebuggerHidden]
public static AsyncETTaskCompletedMethodBuilder Create()
{
AsyncETTaskCompletedMethodBuilder builder = new AsyncETTaskCompletedMethodBuilder();
return builder;
}
// 2. TaskLike Task property(void)
public ETTaskCompleted Task => default;
// 3. SetException
[DebuggerHidden]
public void SetException(Exception e)
{
ETTask.ExceptionHandler.Invoke(e);
}
// 4. SetResult
[DebuggerHidden]
public void SetResult()
{
// do nothing
}
// 5. AwaitOnCompleted
[DebuggerHidden]
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 6. AwaitUnsafeOnCompleted
[DebuggerHidden]
[SecuritySafeCritical]
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
}
// 7. Start
[DebuggerHidden]
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
{
stateMachine.MoveNext();
}
// 8. SetStateMachine
[DebuggerHidden]
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf2682587b9394aa89238524a4ef1ef9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,125 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security;
namespace ET
{
public struct ETAsyncTaskMethodBuilder
{
private ETTask tcs;
// 1. Static Create method.
[DebuggerHidden]
public static ETAsyncTaskMethodBuilder Create()
{
ETAsyncTaskMethodBuilder builder = new ETAsyncTaskMethodBuilder() { tcs = ETTask.Create(true) };
return builder;
}
// 2. TaskLike Task property.
[DebuggerHidden]
public ETTask Task => this.tcs;
// 3. SetException
[DebuggerHidden]
public void SetException(Exception exception)
{
this.tcs.SetException(exception);
}
// 4. SetResult
[DebuggerHidden]
public void SetResult()
{
this.tcs.SetResult();
}
// 5. AwaitOnCompleted
[DebuggerHidden]
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 6. AwaitUnsafeOnCompleted
[DebuggerHidden]
[SecuritySafeCritical]
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 7. Start
[DebuggerHidden]
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
{
stateMachine.MoveNext();
}
// 8. SetStateMachine
[DebuggerHidden]
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
public struct ETAsyncTaskMethodBuilder<T>
{
private ETTask<T> tcs;
// 1. Static Create method.
[DebuggerHidden]
public static ETAsyncTaskMethodBuilder<T> Create()
{
ETAsyncTaskMethodBuilder<T> builder = new ETAsyncTaskMethodBuilder<T>() { tcs = ETTask<T>.Create(true) };
return builder;
}
// 2. TaskLike Task property.
[DebuggerHidden]
public ETTask<T> Task => this.tcs;
// 3. SetException
[DebuggerHidden]
public void SetException(Exception exception)
{
this.tcs.SetException(exception);
}
// 4. SetResult
[DebuggerHidden]
public void SetResult(T ret)
{
this.tcs.SetResult(ret);
}
// 5. AwaitOnCompleted
[DebuggerHidden]
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 6. AwaitUnsafeOnCompleted
[DebuggerHidden]
[SecuritySafeCritical]
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 7. Start
[DebuggerHidden]
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
{
stateMachine.MoveNext();
}
// 8. SetStateMachine
[DebuggerHidden]
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a61c2db12429afb40b78a3f953e46510
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security;
namespace ET
{
internal struct AsyncETVoidMethodBuilder
{
// 1. Static Create method.
[DebuggerHidden]
public static AsyncETVoidMethodBuilder Create()
{
AsyncETVoidMethodBuilder builder = new AsyncETVoidMethodBuilder();
return builder;
}
// 2. TaskLike Task property(void)
[DebuggerHidden]
public ETVoid Task => default;
// 3. SetException
[DebuggerHidden]
public void SetException(Exception e)
{
ETTask.ExceptionHandler.Invoke(e);
}
// 4. SetResult
[DebuggerHidden]
public void SetResult()
{
// do nothing
}
// 5. AwaitOnCompleted
[DebuggerHidden]
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
// 6. AwaitUnsafeOnCompleted
[DebuggerHidden]
[SecuritySafeCritical]
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
{
awaiter.UnsafeOnCompleted(stateMachine.MoveNext);
}
// 7. Start
[DebuggerHidden]
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
{
stateMachine.MoveNext();
}
// 8. SetStateMachine
[DebuggerHidden]
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9a7cde4b1a6c34b49a309698cdfb233f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace ET
{
public class ETCancellationToken
{
private HashSet<Action> actions = new HashSet<Action>();
public void Add(Action callback)
{
// 如果action是null绝对不能添加,要抛异常,说明有协程泄漏
this.actions.Add(callback);
}
public void Remove(Action callback)
{
this.actions?.Remove(callback);
}
public bool IsDispose()
{
return this.actions == null;
}
public void Cancel()
{
if (this.actions == null)
{
return;
}
this.Invoke();
}
private void Invoke()
{
HashSet<Action> runActions = this.actions;
this.actions = null;
try
{
foreach (Action action in runActions)
{
action.Invoke();
}
}
catch (Exception e)
{
ETTask.ExceptionHandler.Invoke(e);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 841f2f7b2ba44e24689645e00f9ed245
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,313 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
namespace ET
{
[AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder))]
public class ETTask: ICriticalNotifyCompletion
{
public static Action<Exception> ExceptionHandler;
public static ETTaskCompleted CompletedTask
{
get
{
return new ETTaskCompleted();
}
}
private static readonly ConcurrentQueue<ETTask> queue = new();
/// <summary>
/// 请不要随便使用ETTask的对象池除非你完全搞懂了ETTask!!!
/// 假如开启了池,await之后不能再操作ETTask否则可能操作到再次从池中分配出来的ETTask产生灾难性的后果
/// SetResult的时候请现将tcs置空避免多次对同一个ETTask SetResult
/// </summary>
public static ETTask Create(bool fromPool = false)
{
if (!fromPool)
{
return new ETTask();
}
if (!queue.TryDequeue(out ETTask task))
{
return new ETTask() {fromPool = true};
}
return task;
}
private void Recycle()
{
if (!this.fromPool)
{
return;
}
this.state = AwaiterStatus.Pending;
this.callback = null;
// 太多了
if (queue.Count > 1000)
{
return;
}
queue.Enqueue(this);
}
private bool fromPool;
private AwaiterStatus state;
private object callback; // Action or ExceptionDispatchInfo
private ETTask()
{
}
[DebuggerHidden]
private async ETVoid InnerCoroutine()
{
await this;
}
[DebuggerHidden]
public void Coroutine()
{
InnerCoroutine().Coroutine();
}
[DebuggerHidden]
public ETTask GetAwaiter()
{
return this;
}
public bool IsCompleted
{
[DebuggerHidden]
get
{
return this.state != AwaiterStatus.Pending;
}
}
[DebuggerHidden]
public void UnsafeOnCompleted(Action action)
{
if (this.state != AwaiterStatus.Pending)
{
action?.Invoke();
return;
}
this.callback = action;
}
[DebuggerHidden]
public void OnCompleted(Action action)
{
this.UnsafeOnCompleted(action);
}
[DebuggerHidden]
public void GetResult()
{
switch (this.state)
{
case AwaiterStatus.Succeeded:
this.Recycle();
break;
case AwaiterStatus.Faulted:
ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
this.callback = null;
this.Recycle();
c?.Throw();
break;
default:
throw new NotSupportedException("ETTask does not allow call GetResult directly when task not completed. Please use 'await'.");
}
}
[DebuggerHidden]
public void SetResult()
{
if (this.state != AwaiterStatus.Pending)
{
throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
}
this.state = AwaiterStatus.Succeeded;
Action c = this.callback as Action;
this.callback = null;
c?.Invoke();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerHidden]
public void SetException(Exception e)
{
if (this.state != AwaiterStatus.Pending)
{
throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
}
this.state = AwaiterStatus.Faulted;
Action c = this.callback as Action;
this.callback = ExceptionDispatchInfo.Capture(e);
c?.Invoke();
}
}
[AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder<>))]
public class ETTask<T>: ICriticalNotifyCompletion
{
private static readonly ConcurrentQueue<ETTask<T>> queue = new();
/// <summary>
/// 请不要随便使用ETTask的对象池除非你完全搞懂了ETTask!!!
/// 假如开启了池,await之后不能再操作ETTask否则可能操作到再次从池中分配出来的ETTask产生灾难性的后果
/// SetResult的时候请现将tcs置空避免多次对同一个ETTask SetResult
/// </summary>
public static ETTask<T> Create(bool fromPool = false)
{
if (!fromPool)
{
return new ETTask<T>();
}
if (!queue.TryDequeue(out ETTask<T> task))
{
return new ETTask<T>() {fromPool = true};
}
return task;
}
private void Recycle()
{
if (!this.fromPool)
{
return;
}
this.callback = null;
this.value = default;
this.state = AwaiterStatus.Pending;
// 太多了
if (queue.Count > 1000)
{
return;
}
queue.Enqueue(this);
}
private bool fromPool;
private AwaiterStatus state;
private T value;
private object callback; // Action or ExceptionDispatchInfo
private ETTask()
{
}
[DebuggerHidden]
private async ETVoid InnerCoroutine()
{
await this;
}
[DebuggerHidden]
public void Coroutine()
{
InnerCoroutine().Coroutine();
}
[DebuggerHidden]
public ETTask<T> GetAwaiter()
{
return this;
}
[DebuggerHidden]
public T GetResult()
{
switch (this.state)
{
case AwaiterStatus.Succeeded:
T v = this.value;
this.Recycle();
return v;
case AwaiterStatus.Faulted:
ExceptionDispatchInfo c = this.callback as ExceptionDispatchInfo;
this.callback = null;
this.Recycle();
c?.Throw();
return default;
default:
throw new NotSupportedException("ETask does not allow call GetResult directly when task not completed. Please use 'await'.");
}
}
public bool IsCompleted
{
[DebuggerHidden]
get
{
return state != AwaiterStatus.Pending;
}
}
[DebuggerHidden]
public void UnsafeOnCompleted(Action action)
{
if (this.state != AwaiterStatus.Pending)
{
action?.Invoke();
return;
}
this.callback = action;
}
[DebuggerHidden]
public void OnCompleted(Action action)
{
this.UnsafeOnCompleted(action);
}
[DebuggerHidden]
public void SetResult(T result)
{
if (this.state != AwaiterStatus.Pending)
{
throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
}
this.state = AwaiterStatus.Succeeded;
this.value = result;
Action c = this.callback as Action;
this.callback = null;
c?.Invoke();
}
[DebuggerHidden]
public void SetException(Exception e)
{
if (this.state != AwaiterStatus.Pending)
{
throw new InvalidOperationException("TaskT_TransitionToFinal_AlreadyCompleted");
}
this.state = AwaiterStatus.Faulted;
Action c = this.callback as Action;
this.callback = ExceptionDispatchInfo.Capture(e);
c?.Invoke();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 624636d415540b94b934cf1931b5c281
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace ET
{
[AsyncMethodBuilder(typeof (AsyncETTaskCompletedMethodBuilder))]
public struct ETTaskCompleted: ICriticalNotifyCompletion
{
[DebuggerHidden]
public ETTaskCompleted GetAwaiter()
{
return this;
}
[DebuggerHidden]
public bool IsCompleted => true;
[DebuggerHidden]
public void GetResult()
{
}
[DebuggerHidden]
public void OnCompleted(Action continuation)
{
}
[DebuggerHidden]
public void UnsafeOnCompleted(Action continuation)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c27123994d9644acf9b27e884c5fdf1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 918113fec35224afa958d442f09ba720
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,28 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace ET
{
[AsyncMethodBuilder(typeof (AsyncETVoidMethodBuilder))]
internal struct ETVoid: ICriticalNotifyCompletion
{
[DebuggerHidden]
public void Coroutine()
{
}
[DebuggerHidden]
public bool IsCompleted => true;
[DebuggerHidden]
public void OnCompleted(Action continuation)
{
}
[DebuggerHidden]
public void UnsafeOnCompleted(Action continuation)
{
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c1427531a30db254e8587be525470b6c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
namespace ET
{
public enum AwaiterStatus: byte
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d48c67734d0468148be856dbd3051d19
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: