예제 #1
0
        /// <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);
        }
예제 #2
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);
        }