private async void RunTask(ActorTask task) { try { IResponse response = await task.Run(); // 如果没找到Actor,发送窗口减少为1,重试 if (response.Error == ErrorCode.ERR_NotFoundActor) { this.CancellationTokenSource.Cancel(); this.WindowSize = 1; ++this.failTimes; while (this.WaitingTasks.Count > 0) { ActorTask actorTask = this.WaitingTasks.Dequeue(); this.RunningTasks.Enqueue(actorTask); } ObjectHelper.Swap(ref this.RunningTasks, ref this.WaitingTasks); // 失败3次则清空actor发送队列,返回失败 if (this.failTimes > 3) { while (this.WaitingTasks.Count > 0) { ActorTask actorTask = this.WaitingTasks.Dequeue(); actorTask.RunFail(response.Error); } // 失败直接删除actorproxy Game.Scene.GetComponent <ActorProxyComponent>().Remove(this.Id); return; } // 等待一会再发送 await Game.Scene.GetComponent <TimerComponent>().WaitAsync(this.failTimes * 500); int appId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(this.Id); this.Address = Game.Scene.GetComponent <StartConfigComponent>().Get(appId).GetComponent <InnerConfig>().IPEndPoint; this.CancellationTokenSource = new CancellationTokenSource(); this.AllowGet(); return; } // 发送成功 this.LastSendTime = TimeHelper.Now(); this.failTimes = 0; if (this.WindowSize < MaxWindowSize) { ++this.WindowSize; } this.RunningTasks.Dequeue(); this.AllowGet(); } catch (Exception e) { Log.Error(e.ToString()); } }
public override void Dispose() { if (this.IsDisposed) { return; } base.Dispose(); this.LastSendTime = 0; this.Address = null; while (this.WaitingTasks.Count > 0) { ActorTask actorTask = this.WaitingTasks.Dequeue(); actorTask.RunFail(ErrorCode.ERR_NotFoundActor); } this.failTimes = 0; var t = this.tcs; this.tcs = null; t?.SetResult(new ActorTask()); }