/// <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 #2
0
        /// <summary>
        /// 下载AB包
        /// </summary>
        public static ETTask <bool> DownloadBundle()
        {
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();

            Log.Debug("初始化成功, 下载最新AB包资源");
            //注册网络检测事件
            NetworkMonitor.Instance.onReachabilityChanged += (reachability) =>
            {
                if (reachability == NetworkReachability.NotReachable)
                {
                    Log.Error("网络错误");
                    tcs.SetResult(false);
                }
            };

            Log.Debug("网络正常");

            Assets.DownloadVersions((DownloadError) =>
            {
                if (!string.IsNullOrEmpty(DownloadError))
                {
                    Log.Error("获取服务器版本失败:" + DownloadError);
                    tcs.SetResult(false);
                }
                else
                {
                    Downloader handler;
                    // 按分包下载版本更新,返回true的时候表示需要下载,false的时候,表示不需要下载
                    if (Assets.DownloadAll(out handler))
                    {
                        long totalSize = handler.size;
                        Debug.Log("发现内容更新,总计需要下载 " + Downloader.GetDisplaySize(totalSize) + " 内容");
                        handler.onUpdate += new Action <long, long, float>((progress, size, speed) =>
                        {
                            //刷新进度
                            Log.Debug("下载中..." +
                                      Downloader.GetDisplaySize(progress) + "/" + Downloader.GetDisplaySize(size) +
                                      ", 速度:" + Downloader.GetDisplaySpeed(speed));
                        });
                        handler.onFinished += new Action(() =>
                        {
                            Log.Debug("下载完成");
                            tcs.SetResult(true);
                        });

                        //开始下载
                        handler.Start();
                    }
                    else
                    {
                        Log.Debug("版本最新无需下载");
                        tcs.SetResult(true);
                    }
                }
            });

            return(tcs.Task);
        }
        /// <summary>
        /// 异步加载资源
        /// </summary>
        public ETTask <T> LoadAssetAsync <T>(AssetRequest assetRequest) where T : UnityEngine.Object
        {
            ETTaskCompletionSource <T> tcs = new ETTaskCompletionSource <T>();

            //如果已经加载完成则直接返回结果(适用于编辑器模式下的异步写法和重复加载)
            if (assetRequest.isDone)
            {
                tcs.SetResult((T)assetRequest.asset);
                return(tcs.Task);
            }

            //+=委托链,否则会导致前面完成委托被覆盖
            assetRequest.completed += (arq) => { tcs.SetResult((T)arq.asset); };
            return(tcs.Task);
        }
Beispiel #4
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 #5
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 #6
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 #7
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 #8
0
        public void Run()
        {
            ETTaskCompletionSource tcs = this.Callback;

            this.GetParent <TimerComponent>().Remove(this.Id);
            tcs.SetResult();
        }
Beispiel #9
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);
        }
        /// <summary>
        /// 异步加载场景
        /// </summary>
        public ETTask <SceneAssetRequest> LoadSceneAsync(SceneAssetRequest sceneAssetRequest)
        {
            ETTaskCompletionSource <SceneAssetRequest> tcs = new ETTaskCompletionSource <SceneAssetRequest>();

            sceneAssetRequest.completed = (arq) => { tcs.SetResult(sceneAssetRequest); };
            return(tcs.Task);
        }
Beispiel #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);
        }
Beispiel #12
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);
        }
        /// <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 #14
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 #15
0
        public ETTask LoadSceneActive(string sceneName)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            var asset = Addressables.LoadSceneAsync(sceneName);

            asset.Completed += op => { tcs.SetResult(); };
            return(tcs.Task);
        }
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource <AssetBundle> t = tcs;

            t.SetResult(this.request.assetBundle);
        }
Beispiel #17
0
        public ETTask <Session> Create(IPEndPoint ipEndPoint)
        {
            ETTaskCompletionSource <Session> t = new ETTaskCompletionSource <Session>();
            Session session = null;

            session = this.Create(ipEndPoint, (b) =>
            {
                t.SetResult(session);
            });
            return(t.Task);
        }
        public ETTask <long> GetDownLoadSize()
        {
            ETTaskCompletionSource <long> tcs    = new ETTaskCompletionSource <long>();
            AsyncOperationHandle <long>   update = Addressables.GetDownloadSizeAsync("A");

            update.Completed += download =>
            {
                tcs.SetResult(download.Result);
            };
            return(tcs.Task);
        }
        public ETTask Init()
        {
            ETTaskCompletionSource tcs       = new ETTaskCompletionSource();
            AsyncOperationHandle   initasync = Addressables.InitializeAsync();

            initasync.Completed += op =>
            {
                tcs.SetResult();
            };
            return(tcs.Task);
        }
Beispiel #20
0
        public ETTask StartUpdate()
        {
            Tcs = new ETTaskCompletionSource();
            Updater.ResPreparedCompleted = () =>
            {
                Tcs.SetResult();
            };
            Updater.StartUpdate();

            return(Tcs.Task);
        }
        public void Update()
        {
            if (!this.request.isDone)
            {
                return;
            }

            ETTaskCompletionSource t = tcs;

            t.SetResult();
        }
        public ETTask <T> LoadAsync <T>(string assetName)
        {
            ETTaskCompletionSource <T> tcs   = new ETTaskCompletionSource <T>();
            AsyncOperationHandle <T>   asset = Addressables.LoadAssetAsync <T>(assetName);

            asset.Completed += op =>
            {
                tcs.SetResult(asset.Result);
            };
            return(tcs.Task);
        }
Beispiel #23
0
        public ETTask CacheBundleAsync(string assetBundleName)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();

            if (cacheBundles.ContainsKey(assetBundleName))
            {
                tcs.SetResult();
            }
            else
            {
                AsyncOperationHandle <UnityEngine.Object> asset = Addressables.LoadAssetAsync <UnityEngine.Object>(assetBundleName);
                asset.Completed += op =>
                {
                    cacheBundles[assetBundleName] = op.Result;
                    tcs.SetResult();
                };
            }

            return(tcs.Task);
        }
        public void Notify(long key)
        {
            if (!this.lockQueues.TryGetValue(key, out LockQueue lockQueue))
            {
                Log.Error($"CoroutineLockComponent Notify not found queue: {key}");
                return;
            }
            if (lockQueue.Count == 0)
            {
                this.lockQueues.Remove(key);
                lockQueue.Dispose();
                return;
            }

            ETTaskCompletionSource <CoroutineLock> tcs = lockQueue.Dequeue();

            tcs.SetResult(ComponentFactory.Create <CoroutineLock, long>(key));
        }
Beispiel #25
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));
        }
Beispiel #26
0
        public ETTask <IResponse> Call(IRequest request)
        {
            int rpcId = ++RpcId;
            var tcs   = new ETTaskCompletionSource <IResponse>();

            this.requestCallback[rpcId] = (response) =>
            {
                if (ErrorCode.IsRpcNeedThrowException(response.Error))
                {
                    tcs.SetException(new Exception($"Rpc Error: {request.GetType().FullName} {response.Error}"));
                    return;
                }

                tcs.SetResult(response);
            };
            request.RpcId = rpcId;
            this.Send(request);
            return(tcs.Task);
        }
Beispiel #27
0
        public ETTask <IResponse> CallWithoutException(IRequest request)
        {
            int rpcId = ++RpcId;
            var tcs   = new ETTaskCompletionSource <IResponse>();

            this.requestCallback[rpcId] = (response) =>
            {
                if (response is ErrorResponse errorResponse)
                {
                    tcs.SetException(new Exception($"session close, errorcode: {errorResponse.Error} {errorResponse.Message}"));
                    return;
                }
                tcs.SetResult(response);
            };

            request.RpcId = rpcId;
            this.Send(request);
            return(tcs.Task);
        }
Beispiel #28
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);
        }
        public void Update()
        {
            if (this.moveTcs == null)
            {
                return;
            }

            Gamer gamer   = this.GetParent <Gamer>();
            long  timeNow = TimeHelper.Now();

            if (timeNow - this.StartTime >= this.needTime)
            {
                gamer.Position = this.Target;
                ETTaskCompletionSource tcs = this.moveTcs;
                this.moveTcs = null;
                tcs.SetResult();
                return;
            }

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

            //插值 Vector3.Lerp返回已走过的百分比的路径 amount为已走过的百分比
            gamer.Position = Vector3.Lerp(this.StartPos, this.Target, amount);
        }