public static async ETTask <IActorResponse> Call(
            this ActorMessageSenderComponent self,
            long actorId,
            int rpcId,
            MemoryStream memoryStream,
            bool needException = true
            )
        {
            if (actorId == 0)
            {
                throw new Exception($"actor id is 0: {memoryStream.ToActorMessage()}");
            }

            var tcs = new ETTaskCompletionSource <IActorResponse>();

            self.requestCallback.Add(rpcId, new ActorMessageSender(actorId, memoryStream, tcs, needException));

            self.Send(actorId, memoryStream);

            long           beginTime = TimeHelper.ServerFrameTime();
            IActorResponse response  = await tcs.Task;
            long           endTime   = TimeHelper.ServerFrameTime();

            long costTime = endTime - beginTime;

            if (costTime > 200)
            {
                Log.Warning("actor rpc time > 200: {0} {1}", costTime, memoryStream.ToActorMessage());
            }

            return(response);
        }
示例#2
0
        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;
            }
            }
        }
示例#3
0
        public static ETTask FromException(Exception ex)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
示例#4
0
            static CanceledETTaskCache()
            {
                ETTaskCompletionSource tcs = new ETTaskCompletionSource();

                tcs.TrySetCanceled();
                Task = tcs.Task;
            }
示例#5
0
        public static ETTask <T> FromCanceled <T>(CancellationToken token)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(new OperationCanceledException(token));
            return(tcs.Task);
        }
示例#6
0
        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);
        }
示例#7
0
            public async ETTask WaitAsync()
            {
                --this.count;
                if (this.count < 0)
                {
                    return;
                }

                if (this.count == 0)
                {
                    List <ETTaskCompletionSource> t = this.tcss;
                    this.tcss = null;
                    foreach (ETTaskCompletionSource ttcs in t)
                    {
                        ttcs.SetResult();
                    }

                    return;
                }

                ETTaskCompletionSource tcs = new ETTaskCompletionSource();

                tcss.Add(tcs);
                await tcs.Task;
            }
示例#8
0
        public void Run(bool isTimeout)
        {
            ETTaskCompletionSource <bool> tcs = this.Callback;

            this.GetParent <TimerComponent>().Remove(this.Id);
            tcs.SetResult(isTimeout);
        }
示例#9
0
        public static ETTask <T> FromException <T>(Exception ex)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
示例#10
0
        public async ETTask <bool> WaitAsync(long time, ETCancellationToken cancellationToken)
        {
            long tillTime = TimeHelper.Now() + time;

            if (TimeHelper.Now() > tillTime)
            {
                return(true);
            }

            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            OnceWaitTimer timer = EntityFactory.CreateWithParent <OnceWaitTimer, ETTaskCompletionSource <bool> >(this, tcs);

            this.timers[timer.Id] = timer;
            AddToTimeId(tillTime, timer.Id);
            long instanceId = timer.InstanceId;

            cancellationToken.Register(() =>
            {
                if (instanceId != timer.InstanceId)
                {
                    return;
                }

                timer.Run(false);

                this.Remove(timer.Id);
            });
            return(await tcs.Task);
        }
示例#11
0
        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);
        }
示例#13
0
        public ETTask MoveToAsync(Vector3 target, float speedValue, CancellationToken cancellationToken)
        {
            Unit unit = this.GetParent <Unit>();

            if ((target - this.Target).magnitude < 0.1f)
            {
                return(ETTask.CompletedTask);
            }

            this.Target = target;


            this.StartPos  = unit.Position;
            this.StartTime = TimeHelper.Now();
            float distance = (this.Target - this.StartPos).magnitude;

            if (Math.Abs(distance) < 0.1f)
            {
                return(ETTask.CompletedTask);
            }

            this.needTime = (long)(distance / speedValue * 1000);

            this.moveTcs = new ETTaskCompletionSource();

            cancellationToken.Register(() =>
            {
                this.moveTcs = null;
            });
            return(this.moveTcs.Task);
        }
示例#14
0
        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);
        }
示例#15
0
        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);
        }
示例#16
0
 public async ETTask ChangeSceneAsync(string sceneName)
 {
     this.tcs = new ETTaskCompletionSource();
     // 加载map
     this.loadMapOperation = SceneManager.LoadSceneAsync(sceneName);
     //this.loadMapOperation.allowSceneActivation = false;
     await this.tcs.Task;
 }
示例#17
0
 public ActorMessageSender(long actorId, MemoryStream memoryStream, ETTaskCompletionSource <IActorResponse> tcs, bool needException)
 {
     this.ActorId       = actorId;
     this.MemoryStream  = memoryStream;
     this.CreateTime    = TimeHelper.ServerNow();
     this.Tcs           = tcs;
     this.NeedException = needException;
 }
示例#18
0
        public async ETTask <bool> WaitAsync(long time)
        {
            long tillTime = TimeHelper.Now() + time;
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            OnceWaitTimer timer = EntityFactory.CreateWithParent <OnceWaitTimer /*, ETTaskCompletionSource<bool>*/>(this, tcs);

            this.timers[timer.Id] = timer;
            AddToTimeId(tillTime, timer.Id);
            return(await tcs.Task);
        }
示例#19
0
        public async ETTask <bool> WaitAsync(long time)
        {
            long tillTime = TimeHelper.Now() + time;
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            OnceWaitTimer timer = TimerFactory.Create <OnceWaitTimer, ETTaskCompletionSource <bool> >(tcs);

            this.timers[timer.InstanceId] = timer;
            AddToTimeId(tillTime, timer.InstanceId);
            return(await tcs.Task);
        }
示例#20
0
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource <AssetBundle> t = tcs;

            t.SetResult(this.request.assetBundle);
        }
示例#21
0
        public ETTask DownloadAsync(string url)
        {
            this.tcs = new ETTaskCompletionSource();

            url          = url.Replace(" ", "%20");
            this.Request = UnityWebRequest.Get(url);
            this.Request.certificateHandler = certificateHandler;
            this.Request.SendWebRequest();

            return(this.tcs.Task);
        }
示例#22
0
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource t = tcs;

            t.SetResult();
        }
示例#23
0
        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 void SetResult()
        {
            if (moveNext == null)
            {
            }
            else
            {
                if (this.tcs == null)
                {
                    this.tcs = new ETTaskCompletionSource();
                }

                this.tcs.TrySetResult();
            }
        }
        public void SetException(Exception exception)
        {
            if (this.tcs == null)
            {
                this.tcs = new ETTaskCompletionSource();
            }

            if (exception is OperationCanceledException ex)
            {
                this.tcs.TrySetCanceled(ex);
            }
            else
            {
                this.tcs.TrySetException(exception);
            }
        }
        public void SetResult(T ret)
        {
            if (moveNext == null)
            {
                this.result = ret;
            }
            else
            {
                if (this.tcs == null)
                {
                    this.tcs = new ETTaskCompletionSource <T>();
                }

                this.tcs.TrySetResult(ret);
            }
        }
示例#27
0
        public async ETTask <CoroutineLock> Wait(CoroutineLockType coroutineLockType, long key)
        {
            CoroutineLockQueueType coroutineLockQueueType = this.list[(int)coroutineLockType];

            if (!coroutineLockQueueType.TryGetValue(key, out CoroutineLockQueue queue))
            {
                queue = EntityFactory.Create <CoroutineLockQueue>(this.Domain);
                coroutineLockQueueType.Add(key, queue);

                return(EntityFactory.CreateWithParent <CoroutineLock, CoroutineLockType, long>(this, coroutineLockType, key));
            }

            ETTaskCompletionSource <CoroutineLock> tcs = new ETTaskCompletionSource <CoroutineLock>();

            queue.Enqueue(tcs);
            return(await tcs.Task);
        }
示例#28
0
        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 AwaitOnCompleted <TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
            where TAwaiter : INotifyCompletion
            where TStateMachine : IAsyncStateMachine
        {
            if (moveNext == null)
            {
                if (this.tcs == null)
                {
                    this.tcs = new ETTaskCompletionSource(); // built future.
                }

                var runner = new MoveNextRunner <TStateMachine>();
                moveNext            = runner.Run;
                runner.StateMachine = stateMachine; // set after create delegate.
            }

            awaiter.OnCompleted(moveNext);
        }
示例#30
0
        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));
        }