public ETTask <IResponse> Call(IRequest request) { int rpcId = ++RpcId; var tcs = new ETTaskCompletionSource <IResponse>(); this.requestCallback[rpcId] = (response) => { if (response is ErrorResponse) { tcs.SetException(new Exception($"Rpc error: {MongoHelper.ToJson(response)}")); return; } if (ErrorCode.IsRpcNeedThrowException(response.Error)) { tcs.SetException(new Exception($"Rpc error: {MongoHelper.ToJson(response)}")); return; } tcs.SetResult(response); }; request.RpcId = rpcId; this.Send(request); return(tcs.Task); }
public static ETTask <IActorResponse> Call(this ActorMessageSenderComponent self, long actorId, IActorRequest message, bool exception = true) { if (actorId == 0) { throw new Exception($"actor id is 0: {MongoHelper.ToJson(message)}"); } var tcs = new ETTaskCompletionSource <IActorResponse>(); int process = IdGenerater.GetProcess(actorId); string address = StartProcessConfigCategory.Instance.Get(process).InnerAddress; Session session = NetInnerComponent.Instance.Get(address); InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId); instanceIdStruct.Process = IdGenerater.Process; message.ActorId = instanceIdStruct.ToLong(); message.RpcId = ++self.RpcId; self.requestCallback.Add(message.RpcId, new ActorMessageSender((response) => { if (exception && ErrorCode.IsRpcNeedThrowException(response.Error)) { tcs.SetException(new Exception($"Rpc error: {MongoHelper.ToJson(response)}")); return; } tcs.SetResult(response); })); session.Send(message); return(tcs.Task); }
public void Run(bool isTimeout) { ETTaskCompletionSource <bool> tcs = this.Callback; this.GetParent <TimerComponent>().Remove(this.Id); tcs.SetResult(isTimeout); }
public override void Run(bool isTimeout) { ETTaskCompletionSource <bool> tcs = this.Callback; TimerComponent.Instance.Remove(this.InstanceId); tcs.SetResult(isTimeout); }
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); }
private void Run(TimerAction timerAction) { switch (timerAction.TimerClass) { case TimerClass.OnceWaitTimer: { ETTaskCompletionSource <bool> tcs = timerAction.Callback as ETTaskCompletionSource <bool>; this.Remove(timerAction.Id); tcs.SetResult(true); break; } case TimerClass.OnceTimer: { Action action = timerAction.Callback as Action; this.Remove(timerAction.Id); action?.Invoke(); break; } case TimerClass.RepeatedTimer: { Action action = timerAction.Callback as Action; long tillTime = TimeHelper.ServerNow() + timerAction.Time; this.AddTimer(tillTime, timerAction); action?.Invoke(); break; } } }
public static async ETTask <UnityEngine.Object[]> UnityLoadAssetAsync(AssetBundle assetBundle) { var tcs = new ETTaskCompletionSource <UnityEngine.Object[]>(); AssetBundleRequest request = assetBundle.LoadAllAssetsAsync(); request.completed += operation => { tcs.SetResult(request.allAssets); }; return(await tcs); }
public static async ETTask <AssetBundle> UnityLoadBundleAsync(string path) { var tcs = new ETTaskCompletionSource <AssetBundle>(); AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path); request.completed += operation => { tcs.SetResult(request.assetBundle); }; return(await tcs); }
public void Update() { if (!this.request.isDone) { return; } ETTaskCompletionSource <AssetBundle> t = tcs; t.SetResult(this.request.assetBundle); }
public void Update() { if (!this.request.isDone) { return; } ETTaskCompletionSource t = tcs; t.SetResult(); }
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; ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>(); 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.Task; } finally { cancellationToken?.Remove(CancelAction); } if (moveRet) { Game.EventSystem.Publish(new EventType.MoveStop() { Unit = self.GetParent <Unit>() }).Coroutine(); } return(moveRet); }
public override void Update(SceneChangeComponent self) { if (!self.loadMapOperation.isDone) { return; } if (self.tcs == null) { return; } ETTaskCompletionSource tcs = self.tcs; self.tcs = null; tcs.SetResult(); }
public void Notify(CoroutineLockType coroutineLockType, long key) { CoroutineLockQueueType coroutineLockQueueType = this.list[(int)coroutineLockType]; if (!coroutineLockQueueType.TryGetValue(key, out CoroutineLockQueue queue)) { throw new Exception($"first work notify not find queue"); } if (queue.Count == 0) { coroutineLockQueueType.Remove(key); queue.Dispose(); return; } ETTaskCompletionSource <CoroutineLock> tcs = queue.Dequeue(); tcs.SetResult(EntityFactory.CreateWithParent <CoroutineLock, CoroutineLockType, long>(this, coroutineLockType, key)); }
public override void Update(MoveComponent self) { if (self.moveTcs == null) { return; } Unit unit = self.GetParent <Unit>(); long timeNow = TimeHelper.ClientNow(); if (timeNow - self.StartTime >= self.needTime) { unit.Position = self.Target; ETTaskCompletionSource tcs = self.moveTcs; self.moveTcs = null; tcs.SetResult(); return; } float amount = (timeNow - self.StartTime) * 1f / self.needTime; unit.Position = Vector3.Lerp(self.StartPos, self.Target, amount); }
public void Update() { if (this.moveTcs == null) { return; } Unit unit = this.GetParent <Unit>(); long timeNow = TimeHelper.Now(); if (timeNow - this.StartTime >= this.needTime) { unit.Position = this.Target; ETTaskCompletionSource tcs = this.moveTcs; this.moveTcs = null; tcs.SetResult(); return; } float amount = (timeNow - this.StartTime) * 1f / this.needTime; unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount); }