Example #1
0
        public static ETTask <T> FromCanceled <T>(CancellationToken token)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(new OperationCanceledException(token));
            return(tcs.Task);
        }
Example #2
0
            static CanceledETTaskCache()
            {
                ETTaskCompletionSource tcs = new ETTaskCompletionSource();

                tcs.TrySetCanceled();
                Task = tcs.Task;
            }
Example #3
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);
        }
Example #4
0
        public static ETTask <T> FromException <T>(Exception ex)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
Example #5
0
        public void Run()
        {
            ETTaskCompletionSource tcs = this.Callback;

            this.GetParent <TimerComponent>().Remove(this.Id);
            tcs.SetResult();
        }
Example #6
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);
        }
Example #7
0
        public static ETTask FromException(Exception ex)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
Example #8
0
 public ETTask ChangeSceneAsync(string sceneName)
 {
     this.tcs = new ETTaskCompletionSource();
     // 加载map
     this.loadMapOperation = SceneManager.LoadSceneAsync(sceneName);
     //this.loadMapOperation.allowSceneActivation = false;
     return(this.tcs.Task);
 }
Example #9
0
        public ETTask WaitAsync(long time)
        {
            long tillTime = TimeHelper.Now() + time;
            ETTaskCompletionSource tcs   = new ETTaskCompletionSource();
            OnceWaitTimer          timer = EntityFactory.CreateWithParent <OnceWaitTimer, ETTaskCompletionSource>(this, tcs);

            this.timers[timer.Id] = timer;
            AddToTimeId(tillTime, timer.Id);
            return(tcs.Task);
        }
Example #10
0
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource <AssetBundle> t = tcs;

            t.SetResult(this.request.assetBundle);
        }
Example #11
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);
        }
Example #12
0
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource t = tcs;

            t.SetResult();
        }
Example #13
0
        public ETTask WaitTillAsync(long tillTime, ETCancellationToken cancellationToken)
        {
            if (TimeHelper.Now() > tillTime)
            {
                return(ETTask.CompletedTask);
            }
            ETTaskCompletionSource tcs   = new ETTaskCompletionSource();
            OnceWaitTimer          timer = EntityFactory.CreateWithParent <OnceWaitTimer, ETTaskCompletionSource>(this, tcs);

            this.timers[timer.Id] = timer;
            AddToTimeId(tillTime, timer.Id);
            cancellationToken.Register(() => { this.Remove(timer.Id); });
            return(tcs.Task);
        }
Example #14
0
        public void SetResult()
        {
            if (moveNext == null)
            {
            }
            else
            {
                if (this.tcs == null)
                {
                    this.tcs = new ETTaskCompletionSource();
                }

                this.tcs.TrySetResult();
            }
        }
Example #15
0
        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);
            }
        }
Example #16
0
        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);
            }
        }
Example #17
0
        public 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(ETTask.FromResult(EntityFactory.CreateWithParent <CoroutineLock, CoroutineLockType, long>(this, coroutineLockType, key)));
            }

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

            queue.Enqueue(tcs);
            return(tcs.Task);
        }
Example #18
0
        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);
        }
Example #19
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));
        }
Example #20
0
        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);
        }
Example #21
0
 public void Enqueue(ETTaskCompletionSource <CoroutineLock> tcs)
 {
     this.queue.Enqueue(tcs);
 }
Example #22
0
 private ETTask InnerLoadAllAssetsAsync()
 {
     this.tcs     = new ETTaskCompletionSource();
     this.request = assetBundle.LoadAllAssetsAsync();
     return(this.tcs.Task);
 }
Example #23
0
 public ETTask <AssetBundle> LoadAsync(string path)
 {
     this.tcs     = new ETTaskCompletionSource <AssetBundle>();
     this.request = AssetBundle.LoadFromFileAsync(path);
     return(this.tcs.Task);
 }