public static async ETTask <bool> WaitTillAsync(this TimerComponent self, long tillTime, ETCancellationToken cancellationToken = null) { if (self.timeNow >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = self.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - self.timeNow, 0, tcs, true); self.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (self.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } ETTask <bool> tcs = ETTask <bool> .Create(true); TimerAction timer = this.AddChild <TimerAction, TimerClass, long, object>(TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public static async ETTask <T> Wait <T>(this ObjectWait self, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); Type type = typeof(T); self.tcss.Add(type, tcs); void CancelAction() { self.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <T> Wait <T>(int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { long timerId = TimerComponent.Instance.NewOnceTimer(TimeHelper.ServerNow() + timeout, () => { Notify(new T() { Error = WaitTypeError.Timeout }); }); ResultCallback <T> tcs = new ResultCallback <T>(timerId); this.tcss.Add(typeof(T), tcs); void CancelAction() { Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null) { if (TimeHelper.ServerNow() >= tillTime) { return(true); } ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); TimerAction timer = EntityFactory.CreateWithParent <TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true); this.AddTimer(tillTime, timer); long timerId = timer.Id; void CancelAction() { if (this.Remove(timerId)) { tcs.SetResult(false); } } bool ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public static async ETTask <bool> MoveToAsync(this DMoveComponent self, Vector3 target, float speedValue, ETCancellationToken cancellationToken = null) { await ETTask.CompletedTask; DUnit unit = self.GetParent <DUnit>(); if ((target - self.Target).magnitude < 0.1f) { return(true); } self.Target = target; self.StartPos = unit.Position; self.StartTime = TimeHelper.ClientNow(); float distance = (self.Target - self.StartPos).magnitude; if (Math.Abs(distance) < 0.1f) { return(true); } self.needTime = (long)(distance / speedValue * 1000); ETTask <bool> moveTcs = ETTask <bool> .Create(); self.Callback = (ret) => { moveTcs.SetResult(ret); }; void CancelAction() { if (self.Callback != null) { Action <bool> callback = self.Callback; self.Callback = null; callback.Invoke(false); } } bool moveRet; try { cancellationToken?.Add(CancelAction); moveRet = await moveTcs; } finally { cancellationToken?.Remove(CancelAction); } return(moveRet); }
public static async ETTask <bool> MoveToAsync(this MoveComponent self, List <Vector3> target, float speed, int turnTime = 100, ETCancellationToken cancellationToken = null) { self.Stop(); foreach (Vector3 v in target) { self.Targets.Add(v); } self.IsTurnHorizontal = true; self.TurnTime = turnTime; self.Speed = speed; ETTask <bool> tcs = ETTask <bool> .Create(); self.Callback = (ret) => { tcs.SetResult(ret); }; Game.EventSystem.Publish(new EventType.MoveStart() { Unit = self.GetParent <Unit>() }).Coroutine(); self.StartMove(); void CancelAction() { self.Stop(); } bool moveRet; try { cancellationToken?.Add(CancelAction); moveRet = await tcs; } finally { cancellationToken?.Remove(CancelAction); } if (moveRet) { Game.EventSystem.Publish(new EventType.MoveStop() { Unit = self.GetParent <Unit>() }).Coroutine(); } return(moveRet); }
public static async ETTask <T> Wait <T>(this ObjectWait self, int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); async ETTask WaitTimeout() { bool retV = await TimerComponent.Instance.WaitAsync(timeout, cancellationToken); if (!retV) { return; } if (tcs.IsDisposed) { return; } self.Notify(new T() { Error = WaitTypeError.Timeout }); } WaitTimeout().Coroutine(); self.tcss.Add(typeof(T), tcs); void CancelAction() { self.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { cancellationToken?.Add(CancelAction); ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <IResponse> Call(IRequest request, ETCancellationToken cancellationToken) { int rpcId = ++RpcId; RpcInfo rpcInfo = new RpcInfo(request); this.requestCallbacks[rpcId] = rpcInfo; request.RpcId = rpcId; this.Send(request); void CancelAction() { if (!this.requestCallbacks.TryGetValue(rpcId, out RpcInfo action)) { return; } this.requestCallbacks.Remove(rpcId); Type responseType = OpcodeTypeComponent.Instance.GetResponseType(action.Request.GetType()); IResponse response = (IResponse)Activator.CreateInstance(responseType); response.Error = ErrorCore.ERR_Cancel; action.Tcs.SetResult(response); } IResponse ret; try { cancellationToken?.Add(CancelAction); ret = await rpcInfo.Tcs; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }
public async ETTask <T> Wait <T>(ETCancellationToken cancellationToken = null) where T : struct, IWaitType { ResultCallback <T> tcs = new ResultCallback <T>(); Type type = typeof(T); this.tcss.Add(type, tcs); void CancelAction() { this.Notify(new T() { Error = WaitTypeError.Cancel }); } T ret; try { if (cancellationToken != null) { if (!cancellationToken.IsCancel()) { cancellationToken?.Add(CancelAction); } else { Log.Error("cancellationToken 已经取消过了"); } } ret = await tcs.Task; } finally { cancellationToken?.Remove(CancelAction); } return(ret); }