Beispiel #1
0
            static CanceledETTaskCache()
            {
                var promise = new ETTaskCompletionSource <T>();

                promise.TrySetCanceled();
                Task = new ETTask <T>(promise);
            }
Beispiel #2
0
        public void Run()
        {
            ETTaskCompletionSource tcs = this.Callback;

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

            promise.TrySetException(ex);
            return(new ETTask <T>(promise));
        }
Beispiel #4
0
        public ETTask <List <ComponentWithId> > GetBatch(string collectionName, List <long> idList)
        {
            List <ComponentWithId> components = new List <ComponentWithId>();
            bool isAllInCache = true;

            foreach (long id in idList)
            {
                ComponentWithId component = this.GetFromCache(collectionName, id);
                if (component == null)
                {
                    isAllInCache = false;
                    break;
                }
                components.Add(component);
            }

            if (isAllInCache)
            {
                return(ETTask.FromResult(components));
            }

            ETTaskCompletionSource <List <ComponentWithId> > tcs = new ETTaskCompletionSource <List <ComponentWithId> >();
            DBQueryBatchTask dbQueryBatchTask = ComponentFactory.Create <DBQueryBatchTask, List <long>, string, ETTaskCompletionSource <List <ComponentWithId> > >(idList, collectionName, tcs);

            this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask);

            return(tcs.Task);
        }
Beispiel #5
0
        public static ETTask FromException(Exception ex)
        {
            var promise = new ETTaskCompletionSource <AsyncUnit>();

            promise.TrySetException(ex);
            return(new ETTask(promise));
        }
Beispiel #6
0
        public ETTask <bool> MoveToAsync(Vector3 dest, float speedValue, CancellationToken cancellationToken)
        {
            if ((dest - this.GetParent <Unit>().Position).magnitude < 0.1f)
            {
                this.IsArrived = true;
                return(ETTask.FromResult(false));
            }

            if ((dest - this.Dest).magnitude < 0.1f)
            {
                return(ETTask.FromResult(false));
            }

            this.moveTcs   = new ETTaskCompletionSource <bool>();
            this.IsArrived = false;
            Vector3 spd = dest - this.GetParent <Unit>().Position;

            spd        = spd.normalized * speedValue;
            this.Speed = spd;
            this.Dest  = dest;

            cancellationToken.Register(() => this.moveTcs = null);

            return(this.moveTcs.Task);
        }
 public ETTask ChangeSceneAsync(string sceneName)
 {
     this.tcs = new ETTaskCompletionSource();
     // 加载map
     this.loadMapOperation = SceneManager.LoadSceneAsync(sceneName);
     return(this.tcs.Task);
 }
Beispiel #8
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);
        }
        /// <summary>
        /// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
        /// </summary>
        public ETTask <Session> Get(IPEndPoint ipEndPoint, float timeOut = 5)
        {
            ETTaskCompletionSource <Session> t = new ETTaskCompletionSource <Session>();

            if (timeOut != 0)
            {
                t.SetTimeOut(timeOut, null);
            }

            Session session;

            if (this.adressSessions.TryGetValue(ipEndPoint, out session))
            {
                t.SetResult(session);
                return(t.Task);
            }

            session = this.Create(ipEndPoint, (b) =>
            {
                t.SetResult(session);
            });

            this.adressSessions.Add(ipEndPoint, session);
            return(t.Task);
        }
Beispiel #10
0
        public ETTask <IResponse> Query(Session session, IQuery request, CancellationToken cancellationToken)
        {
            var tcs = new ETTaskCompletionSource <IResponse>();

            if (request.RpcId == 0)
            {
                request.RpcId = GetRpcID();
            }
            int RpcId = request.RpcId;

            this.requestCallback[RpcId] = (response) =>
            {
                try
                {
                    tcs.SetResult(response);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception($"Rpc Error: {RpcId}", e));
                }
            };
            cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });

            session.Send(0x1, request);
            return(tcs.Task);
        }
Beispiel #11
0
        public ETTask <IResponse> Query(Session session, IQuery request, float timeOut)
        {
            var tcs = new ETTaskCompletionSource <IResponse>();

            if (request.RpcId == 0)
            {
                request.RpcId = GetRpcID();
            }
            int RpcId = request.RpcId;

            if (timeOut != 0)
            {
                tcs.SetTimeOut(timeOut, null);
            }

            this.requestCallback[RpcId] = (response) =>
            {
                try
                {
                    tcs.SetResult(response);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception($"Rpc Error: {RpcId}", e));
                }
            };
            session.Send(0x1, request);
            return(tcs.Task);
        }
Beispiel #12
0
        /// <summary>
        /// 对外的发送请求接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ETTask <IResponse> Call(IRequest request)
        {
            //RpcId逻辑自增 保证唯一性
            int rpcId = ++RpcId;
            //这里使用了ETTask 暂时只需要知道先声明ETTaskCompletionSource对象tcs
            //然后当tcs这个对象调用了SetResult,就会返回上一层await等待的地方,继续执行后面的语句
            var tcs = new ETTaskCompletionSource <IResponse>();

            //思路是先以key和value存储到字典里,当收到响应的时候,因为请求和响应的RPCID是一致的,所以可以通过RPCID取到Value
            //因为Value存储的是委托,所以收到响应的时候,调用一下即可执行这内部的tcs.SetResult(response);
            this.requestCallback[rpcId] = (response) =>
            {
                try
                {
                    if (ErrorCode.IsRpcNeedThrowException(response.Error))
                    {
                        throw new RpcException(response.Error, response.Message);
                    }
                    //设置结果 即可返回await等待的地方
                    tcs.SetResult(response);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName}", e));
                }
            };
            //设置RpcId 然后发送调用请求的方法
            request.RpcId = rpcId;
            this.Send(request);
            return(tcs.Task);
        }
Beispiel #13
0
            static CanceledETTaskCache()
            {
                ETTaskCompletionSource tcs = new ETTaskCompletionSource();

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

            promise.TrySetException(new OperationCanceledException(token));
            return(new ETTask <T>(promise));
        }
Beispiel #15
0
        public static ETTask <T> FromCanceled <T>(CancellationToken token)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(new OperationCanceledException(token));
            return(tcs.Task);
        }
Beispiel #16
0
        public static ETTask <T> FromException <T>(Exception ex)
        {
            var tcs = new ETTaskCompletionSource <T>();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
        /// <summary>
        /// 异步加载场景
        /// </summary>
        public ETTask <SceneAssetRequest> LoadSceneAsync(SceneAssetRequest sceneAssetRequest)
        {
            ETTaskCompletionSource <SceneAssetRequest> tcs = new ETTaskCompletionSource <SceneAssetRequest>();

            sceneAssetRequest.completed = (arq) => { tcs.SetResult(sceneAssetRequest); };
            return(tcs.Task);
        }
Beispiel #18
0
        public static ETTask CompletedTask => new ETTask(); //public static ETTask CompletedTask { get { return new ETTask(); } }

        public static ETTask FromException(Exception ex)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();

            tcs.TrySetException(ex);
            return(tcs.Task);
        }
Beispiel #19
0
        public ETTask <IResponse> Call(IRequest request, CancellationToken cancellationToken)
        {
            int rpcId = ++RpcId;
            var tcs   = new ETTaskCompletionSource <IResponse>();

            this.requestCallback[rpcId] = (response) =>
            {
                try
                {
                    if (ErrorCode.IsRpcNeedThrowException(response.Error))
                    {
                        throw new RpcException(response.Error, response.Message);
                    }

                    tcs.SetResult(response);
                }
                catch (Exception e)
                {
                    tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName}", e));
                }
            };

            cancellationToken.Register(() => this.requestCallback.Remove(rpcId));

            request.RpcId = rpcId;
            this.Send(request);
            return(tcs.Task);
        }
Beispiel #20
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);
        }
Beispiel #21
0
        void MoveToAsync()
        {
            if (this.moveTcs == null)
            {
                this.GetParent <Unit>().GetComponent <AnimatorComponent>().AnimSet(MotionType.None);
                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;
            }

            this.GetParent <Unit>().GetComponent <AnimatorComponent>().AnimSet(MotionType.Move);

            float amount = (timeNow - this.StartTime) * 1f / this.needTime;

            unit.Position = Vector3.Lerp(this.StartPos, this.Target, amount);
        }
Beispiel #22
0
        void TurnToAsync()
        {
            if (this.turnTcs == null)
            {
                return;
            }

            Unit unit = this.GetParent <Unit>();

            this.time += Time.deltaTime;

            if (this.time >= this.needTime)
            {
                unit.GameObject.transform.eulerAngles = this.TargetEulerAngles;

                ETTaskCompletionSource tcs = this.turnTcs;
                this.turnTcs = null;
                tcs.SetResult();
                return;
            }

            float amount = this.time * 1f / this.needTime;

            Quaternion from = PositionHelper.GetAngleToQuaternion(this.StartEul.y);
            Quaternion to   = PositionHelper.GetAngleToQuaternion(this.TargetEulerAngles.y);

            Quaternion v = Quaternion.Slerp(from, to, amount);

            this.GetParent <Unit>().Rotation = v;

            //unit.GameObject.transform.eulerAngles = Vector3.Lerp(this.StartEul, this.TargetEulerAngles, amount);

            Debug.Log(" TurnEulerAnglesComponent-68-amount: " + this.time + " / " + this.needTime + " / " + amount);
            Debug.Log(" TurnEulerAnglesComponent-69-eulerAngles: " + "(" + 0 + ", " + unit.GameObject.transform.eulerAngles.y + ", " + 0 + " )");
        }
Beispiel #23
0
        /// <summary>
        /// 异步加载资源,path需要是全路径
        /// </summary>
        /// <param name="path"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public ETTask <T> LoadAssetAsync <T>(string path) where T : UnityEngine.Object
        {
            ETTaskCompletionSource <T> tcs = new ETTaskCompletionSource <T>();
            AssetRequest assetRequest      = Assets.LoadAssetAsync(path, typeof(T));

            assetRequest.completed = (arq) => { tcs.SetResult((T)arq.asset); };
            return(tcs.Task);
        }
Beispiel #24
0
        public ETTask AddBatch(List <ComponentWithId> components, string collectionName)
        {
            ETTaskCompletionSource tcs  = new ETTaskCompletionSource();
            DBSaveBatchTask        task = ComponentFactory.Create <DBSaveBatchTask, List <ComponentWithId>, string, ETTaskCompletionSource>(components, collectionName, tcs);

            this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
            return(tcs.Task);
        }
        public ETTask DeleteJson(string collectionName, string json)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            DBDeleteJsonTask       dbDeleteJsonTask = ComponentFactory.Create <DBDeleteJsonTask, string, string, ETTaskCompletionSource>(collectionName, json, tcs);

            this.tasks[(int)((ulong)dbDeleteJsonTask.Id % taskCount)].Add(dbDeleteJsonTask);
            return(tcs.Task);
        }
Beispiel #26
0
 public ETTask <bool> DownloadAsync(string url, CancellationToken cancellationToken)
 {
     url      = url.Replace(" ", "%20");
     this.www = new WWW(url);
     this.tcs = new ETTaskCompletionSource <bool>();
     cancellationToken.Register(() => { this.isCancel = true; });
     return(this.tcs.Task);
 }
Beispiel #27
0
 public ETTask <bool> LoadFromCacheOrDownload(string url, Hash128 hash, CancellationToken cancellationToken)
 {
     url      = url.Replace(" ", "%20");
     this.www = WWW.LoadFromCacheOrDownload(url, hash, 0);
     this.tcs = new ETTaskCompletionSource <bool>();
     cancellationToken.Register(() => { this.isCancel = true; });
     return(this.tcs.Task);
 }
        public ETTask <long> GetCountByJson(string collectionName, string json)
        {
            ETTaskCompletionSource <long> tcs = new ETTaskCompletionSource <long>();
            DBQueryCountTask dbQueryCountTask = ComponentFactory.Create <DBQueryCountTask, string, string, ETTaskCompletionSource <long> >(collectionName, json, tcs);

            this.tasks[(int)((ulong)dbQueryCountTask.Id % taskCount)].Add(dbQueryCountTask);
            return(tcs.Task);
        }
        /// <summary>
        /// 加载场景,path需要是全路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public ETTask <SceneAssetRequest> LoadSceneAsync(string path)
        {
            ETTaskCompletionSource <SceneAssetRequest> tcs = new ETTaskCompletionSource <SceneAssetRequest>();
            SceneAssetRequest sceneAssetRequest            = Assets.LoadSceneAsync(path, false);

            sceneAssetRequest.completed = (arq) => { tcs.SetResult(arq as SceneAssetRequest); };
            return(tcs.Task);
        }
Beispiel #30
0
        public ETTask ChangeSceneAsync(string sceneName)
        {
            this.tcs = new ETTaskCompletionSource();
            // 加载scene
            this.op = SceneManager.LoadSceneAsync(sceneName);

            return(this.tcs.Task);
        }