public Task <IResponse> Call(IRequest request) { TaskCompletionSource <IResponse> tcs = new TaskCompletionSource <IResponse>(); ActorTask task = new ActorTask(request, tcs); this.Add(task); return(task.tcs.Task); }
private void Add(ActorTask task) { if (IsDisposed) { throw new Exception("GateSessionActorId Disposed! dont hold actorproxy"); } WaitingMessages.Enqueue(task); AllowGet(); }
private void AllowGet() { if (this.Tcs == null || this.WaitingMessages.Count <= 0) { return; } ActorTask task = WaitingMessages.Peek(); var t = this.Tcs; this.Tcs = null; t?.SetResult(task); }
public async void UpdateAsync() { try { while (true) { ActorTask task = await GetAsync(); if (this.IsDisposed) { return; } await RunTask(task); } } catch (Exception ex) { Log.Error(ex); } }
private async Task RunTask(ActorTask task) { Session session = Game.Scene.GetComponent <NetInnerComponent>().Get(this.Address); task.message.ActorId = this.ActorId; IResponse response = await session.Call(task.message); switch (response.Error) { case ErrorCode.ERR_Success: { this.WaitingMessages.Dequeue(); task.tcs?.SetResult(response); } break; default: { this.Error = response.Error; Game.Scene.GetComponent <ActorMessageSenderComponent>().Remove(ActorId); } break; } }
public void Send(IRequest request) { ActorTask task = new ActorTask(request); this.Add(task); }