mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] TEngineServer
[+] TEngineServer
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18fd7fd9904bb6a448e89c953dbfa432
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,4 @@
|
||||
namespace TEngine
|
||||
{
|
||||
public abstract class TimerHandler<T> : EventSystem<T> { }
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b512d458640f63d43983b335c0c71f12
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
31
Assets/GameScripts/DotNet/Core/TimerScheduler/TimerAction.cs
Normal file
31
Assets/GameScripts/DotNet/Core/TimerScheduler/TimerAction.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using TEngine.Core;
|
||||
#pragma warning disable CS8625
|
||||
#pragma warning disable CS8618
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public sealed class TimerAction : IDisposable
|
||||
{
|
||||
public long Id;
|
||||
public long Time;
|
||||
public object Callback;
|
||||
public TimerType TimerType;
|
||||
|
||||
public static TimerAction Create()
|
||||
{
|
||||
var timerAction = Pool<TimerAction>.Rent();
|
||||
timerAction.Id = IdFactory.NextRunTimeId();
|
||||
return timerAction;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Id = 0;
|
||||
Time = 0;
|
||||
Callback = null;
|
||||
TimerType = TimerType.None;
|
||||
Pool<TimerAction>.Return(this);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c079aa22ce14d364689992037fcb7184
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,21 @@
|
||||
using TEngine.Core;
|
||||
#if TENGINE_UNITY
|
||||
using UnityEngine;
|
||||
#endif
|
||||
namespace TEngine
|
||||
{
|
||||
public sealed class TimerScheduler : Singleton<TimerScheduler>, IUpdateSingleton
|
||||
{
|
||||
public readonly TimerSchedulerCore Core = new TimerSchedulerCore(() => TimeHelper.Now);
|
||||
#if TENGINE_UNITY
|
||||
public readonly TimerSchedulerCore Unity = new TimerSchedulerCore(() => (long) (Time.time * 1000));
|
||||
#endif
|
||||
public void Update()
|
||||
{
|
||||
Core.Update();
|
||||
#if TENGINE_UNITY
|
||||
Unity.Update();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3565301ee18ba443b84827cd7a38d5a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,264 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TEngine.DataStructure;
|
||||
using TEngine.Core;
|
||||
#pragma warning disable CS8625
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class TimerSchedulerCore
|
||||
{
|
||||
private long _minTime;
|
||||
private readonly Func<long> _now;
|
||||
private readonly Queue<long> _timeOutTime = new();
|
||||
private readonly Queue<long> _timeOutTimerIds = new();
|
||||
private readonly Dictionary<long, TimerAction> _timers = new();
|
||||
private readonly SortedOneToManyList<long, long> _timeId = new();
|
||||
|
||||
public TimerSchedulerCore(Func<long> now)
|
||||
{
|
||||
_now = now;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
try
|
||||
{
|
||||
var currentTime = _now();
|
||||
|
||||
if (_timeId.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentTime < _minTime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_timeOutTime.Clear();
|
||||
_timeOutTimerIds.Clear();
|
||||
|
||||
foreach (var (key, _) in _timeId)
|
||||
{
|
||||
if (key > currentTime)
|
||||
{
|
||||
_minTime = key;
|
||||
break;
|
||||
}
|
||||
|
||||
_timeOutTime.Enqueue(key);
|
||||
}
|
||||
|
||||
while (_timeOutTime.TryDequeue(out var time))
|
||||
{
|
||||
foreach (var timerId in _timeId[time])
|
||||
{
|
||||
_timeOutTimerIds.Enqueue(timerId);
|
||||
}
|
||||
|
||||
_timeId.RemoveKey(time);
|
||||
}
|
||||
|
||||
while (_timeOutTimerIds.TryDequeue(out var timerId))
|
||||
{
|
||||
if (!_timers.TryGetValue(timerId, out var timer))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_timers.Remove(timer.Id);
|
||||
|
||||
switch (timer.TimerType)
|
||||
{
|
||||
case TimerType.OnceWaitTimer:
|
||||
{
|
||||
var tcs = (FTask<bool>) timer.Callback;
|
||||
timer.Dispose();
|
||||
tcs.SetResult(true);
|
||||
break;
|
||||
}
|
||||
case TimerType.OnceTimer:
|
||||
{
|
||||
var action = (Action) timer.Callback;
|
||||
timer.Dispose();
|
||||
|
||||
if (action == null)
|
||||
{
|
||||
Log.Error($"timer {timer.ToJson()}");
|
||||
break;
|
||||
}
|
||||
|
||||
action();
|
||||
break;
|
||||
}
|
||||
case TimerType.RepeatedTimer:
|
||||
{
|
||||
var action = (Action) timer.Callback;
|
||||
AddTimer(_now() + timer.Time, timer);
|
||||
|
||||
if (action == null)
|
||||
{
|
||||
Log.Error($"timer {timer.ToJson()}");
|
||||
break;
|
||||
}
|
||||
|
||||
action();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddTimer(long tillTime, TimerAction timer)
|
||||
{
|
||||
_timers.Add(timer.Id, timer);
|
||||
_timeId.Add(tillTime, timer.Id);
|
||||
|
||||
if (tillTime < _minTime)
|
||||
{
|
||||
_minTime = tillTime;
|
||||
}
|
||||
}
|
||||
|
||||
public async FTask<bool> WaitFrameAsync()
|
||||
{
|
||||
return await WaitAsync(1);
|
||||
}
|
||||
|
||||
public async FTask<bool> WaitAsync(long time, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
return await WaitTillAsync(_now() + time, cancellationToken);
|
||||
}
|
||||
|
||||
public async FTask<bool> WaitTillAsync(long tillTime, FCancellationToken cancellationToken = null)
|
||||
{
|
||||
if (_now() > tillTime)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var tcs = FTask<bool>.Create();
|
||||
var timerAction = TimerAction.Create();
|
||||
var timerId = timerAction.Id;
|
||||
timerAction.Callback = tcs;
|
||||
timerAction.TimerType = TimerType.OnceWaitTimer;
|
||||
|
||||
void CancelActionVoid()
|
||||
{
|
||||
if (!_timers.ContainsKey(timerId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Remove(timerId);
|
||||
tcs.SetResult(false);
|
||||
}
|
||||
|
||||
bool b;
|
||||
try
|
||||
{
|
||||
cancellationToken?.Add(CancelActionVoid);
|
||||
AddTimer(tillTime, timerAction);
|
||||
b = await tcs;
|
||||
}
|
||||
finally
|
||||
{
|
||||
cancellationToken?.Remove(CancelActionVoid);
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
public long NewFrameTimer(Action action)
|
||||
{
|
||||
return RepeatedTimer(100, action);
|
||||
}
|
||||
|
||||
public long RepeatedTimer(long time, Action action)
|
||||
{
|
||||
if (time <= 0)
|
||||
{
|
||||
throw new Exception("repeated time <= 0");
|
||||
}
|
||||
|
||||
var tillTime = _now() + time;
|
||||
var timer = TimerAction.Create();
|
||||
timer.TimerType = TimerType.RepeatedTimer;
|
||||
timer.Time = time;
|
||||
timer.Callback = action;
|
||||
AddTimer(tillTime, timer);
|
||||
return timer.Id;
|
||||
}
|
||||
|
||||
public long RepeatedTimer<T>(long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
void RepeatedTimerVoid()
|
||||
{
|
||||
EventSystem.Instance.Publish(timerHandlerType);
|
||||
}
|
||||
|
||||
return RepeatedTimer(time, RepeatedTimerVoid);
|
||||
}
|
||||
|
||||
public long OnceTimer(long time, Action action)
|
||||
{
|
||||
return OnceTillTimer(_now() + time, action);
|
||||
}
|
||||
|
||||
public long OnceTimer<T>(long time, T timerHandlerType) where T : struct
|
||||
{
|
||||
void OnceTimerVoid()
|
||||
{
|
||||
EventSystem.Instance.Publish(timerHandlerType);
|
||||
}
|
||||
|
||||
return OnceTimer(time, OnceTimerVoid);
|
||||
}
|
||||
|
||||
public long OnceTillTimer(long tillTime, Action action)
|
||||
{
|
||||
if (tillTime < _now())
|
||||
{
|
||||
Log.Error($"new once time too small tillTime:{tillTime} Now:{_now()}");
|
||||
}
|
||||
|
||||
var timer = TimerAction.Create();
|
||||
timer.TimerType = TimerType.OnceTimer;
|
||||
timer.Callback = action;
|
||||
AddTimer(tillTime, timer);
|
||||
return timer.Id;
|
||||
}
|
||||
|
||||
public long OnceTillTimer<T>(long tillTime, T timerHandlerType) where T : struct
|
||||
{
|
||||
void OnceTillTimerVoid()
|
||||
{
|
||||
EventSystem.Instance.Publish(timerHandlerType);
|
||||
}
|
||||
|
||||
return OnceTillTimer(tillTime, OnceTillTimerVoid);
|
||||
}
|
||||
|
||||
public void RemoveByRef(ref long id)
|
||||
{
|
||||
Remove(id);
|
||||
id = 0;
|
||||
}
|
||||
|
||||
public void Remove(long id)
|
||||
{
|
||||
if (id == 0 || !_timers.Remove(id, out var timer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
timer?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7936ad23c011d3419f3254b935c96a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
Assets/GameScripts/DotNet/Core/TimerScheduler/TimerType.cs
Normal file
10
Assets/GameScripts/DotNet/Core/TimerScheduler/TimerType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace TEngine
|
||||
{
|
||||
public enum TimerType
|
||||
{
|
||||
None,
|
||||
OnceWaitTimer,
|
||||
OnceTimer,
|
||||
RepeatedTimer
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6919ca6608e0e8840aaedf1f8d09f53f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user