Exemple #1
0
        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());
            }
        }
Exemple #2
0
        private async void UpdateAsync()
        {
            while (true)
            {
                ActorTask actorTask = await this.GetAsync();

                if (this.Id == 0)
                {
                    return;
                }
                if (actorTask == null)
                {
                    return;
                }
                try
                {
                    this.RunTask(actorTask);
                }
                catch (Exception e)
                {
                    Log.Error(e.ToString());
                    return;
                }
            }
        }
Exemple #3
0
        private async void UpdateAsync()
        {
            if (this.Address == null)
            {
                int appId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(this.Id);

                this.Address = Game.Scene.GetComponent <StartConfigComponent>().Get(appId).GetComponent <InnerConfig>().Address;
            }
            while (true)
            {
                if (this.Id == 0)
                {
                    return;
                }
                ActorTask actorTask = await this.GetAsync();

                if (actorTask == null)
                {
                    return;
                }
                try
                {
                    this.RunTask(actorTask);
                }
                catch (Exception e)
                {
                    Log.Error(e.ToString());
                    return;
                }
            }
        }
Exemple #4
0
        public void Send(IMessage message)
        {
            ActorTask task = new ActorTask();

            task.message = (MessageObject)message;
            task.proxy   = this;
            this.Add(task);
        }
Exemple #5
0
        public Task <IResponse> Call(IRequest request)
        {
            ActorTask task = new ActorTask();

            task.message = (MessageObject)request;
            task.proxy   = this;
            task.Tcs     = new TaskCompletionSource <IResponse>();
            this.Add(task);
            return(task.Tcs.Task);
        }
Exemple #6
0
        private Task <ActorTask> GetAsync()
        {
            if (this.WaitingTasks.Count > 0)
            {
                ActorTask task = this.WaitingTasks.Dequeue();
                this.RunningTasks.Enqueue(task);
                return(Task.FromResult(task));
            }

            this.tcs = new TaskCompletionSource <ActorTask>();
            return(this.tcs.Task);
        }
Exemple #7
0
 private void Add(ActorTask task)
 {
     if (this.Id == 0)
     {
         throw new Exception("ActorProxy Disposed! dont hold actorproxy");
     }
     this.WaitingTasks.Enqueue(task);
     // failtimes > 0表示正在重试,这时候不能加到正在发送队列
     if (this.failTimes == 0)
     {
         this.AllowGet();
     }
 }
Exemple #8
0
        private async void UpdateAsync()
        {
            if (this.Address == null)
            {
                int appId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(this.Id);

                this.Address = Game.Scene.GetComponent <StartConfigComponent>().Get(appId).GetComponent <InnerConfig>().Address;
            }
            while (true)
            {
                ActorTask actorTask = await this.GetAsync();

                this.RunTask(actorTask);
            }
        }
Exemple #9
0
        private void AllowGet()
        {
            if (this.tcs == null || this.WaitingTasks.Count <= 0 || this.RunningTasks.Count >= this.WindowSize)
            {
                return;
            }

            ActorTask task = this.WaitingTasks.Dequeue();

            this.RunningTasks.Enqueue(task);

            var t = this.tcs;

            this.tcs = null;
            t.SetResult(task);
        }
Exemple #10
0
 private void Add(ActorTask task)
 {
     this.WaitingTasks.Enqueue(task);
     this.AllowGet();
 }
Exemple #11
0
        private void Remove()
        {
            ActorTask task = this.RunningTasks.Dequeue();

            this.AllowGet();
        }