Пример #1
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
            var ss = Client;

            if (ss == null)
            {
                return(null);
            }

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException(action + "超时取消", ex);
            }
        }
Пример #2
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
            // 让上层异步到这直接返回,后续代码在另一个线程执行
#if !NET4
            await Task.Yield();
#endif

            Open();

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    await Cluster.InvokeAsync(client => OnLoginAsync(client, true));

                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException($"[{act}]超时[{Timeout:n0}ms]取消");
            }
        }
Пример #3
0
        private IMessage OnProcess(IApiSession session, IMessage msg)
        {
            var enc = Encoder;

            var    action = "";
            Object result = null;
            var    code   = 0;

            try
            {
                if (!ApiHostHelper.Decode(msg, out action, out _, out var args))
                {
                    return(null);
                }

                result = Handler.Execute(session, action, args);
            }
            catch (Exception ex)
            {
                ex = ex.GetTrue();

                // 支持自定义错误
                if (ex is ApiException aex)
                {
                    code   = aex.Code;
                    result = ex?.Message;
                }
                else
                {
                    code   = 500;
                    result = ex?.Message;
                }
            }

            // 单向请求无需响应
            if (msg is DefaultMessage dm && dm.OneWay)
            {
                return(null);
            }

            // 编码响应数据包,二进制优先
            var pk = result as Packet;

            if (pk == null)
            {
                pk = enc.Encode(action, code, result);
            }
            pk = ApiHostHelper.Encode(action, code, pk);

            // 构造响应消息
            var rs = msg.CreateReply();

            rs.Payload = pk;
            if (code > 0 && rs is DefaultMessage dm2)
            {
                dm2.Error = true;
            }

            return(rs);
        }
Пример #4
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
#if !NET4
            // 让上层异步到这直接返回,后续代码在另一个线程执行
            await Task.Yield();
#endif

            Open();

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException($"[{action}]超时取消", ex);
            }
        }
Пример #5
0
        /// <summary>同步调用,不等待返回</summary>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual Boolean Invoke(String action, Object args = null, Byte flag = 0)
        {
            if (!Open())
            {
                return(false);
            }

            var act = action;

            return(ApiHostHelper.Invoke(this, this, act, args, flag));
        }
Пример #6
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public async Task <TResult> InvokeAsync <TResult>(String action, object args = null)
        {
            var ss = Client;

            if (ss == null)
            {
                return(default(TResult));
            }

            return(await ApiHostHelper.InvokeAsync <TResult>(this, this, action, args).ConfigureAwait(false));
        }
Пример #7
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <param name="cookie">附加参数,位于顶级</param>
        /// <returns></returns>
        public virtual async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, IDictionary <String, Object> cookie = null)
        {
            var ss = Client;

            if (ss == null)
            {
                return(default(TResult));
            }

            // 未登录且设置了用户名,并且当前不是登录,则异步登录
            if (!Logined && !UserName.IsNullOrEmpty() && action != "Login")
            {
                await LoginAsync();
            }

            var act = action;

            if (!ActionPrefix.IsNullOrEmpty() && !act.Contains("/"))
            {
                act = ActionPrefix.EnsureEnd("/") + act;
            }

            LastInvoke = DateTime.Now;
            try
            {
                return(await ApiHostHelper.InvokeAsync <TResult>(this, this, act, args, cookie ?? Cookie));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    Logined = false;
                    // 如果当前不是登录,且设置了用户名,尝试自动登录
                    if (action != "Login" && !UserName.IsNullOrEmpty())
                    {
                        await LoginAsync();

                        return(await ApiHostHelper.InvokeAsync <TResult>(this, this, act, args, cookie ?? Cookie));
                    }
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException(action + "超时取消");
            }
        }
Пример #8
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <param name="cookie">附加参数,位于顶级</param>
        /// <returns></returns>
        public virtual async Task<TResult> InvokeAsync<TResult>(String action, Object args = null, IDictionary<String, Object> cookie = null)
        {
            var ss = Client;
            if (ss == null) return default(TResult);

            //if (!Active || !ss) Logined = false;

            // 未登录且设置了用户名,并且当前不是登录,则异步登录
            if (!Logined && !UserName.IsNullOrEmpty() && action != LoginAction) await LoginAsync();

            LastInvoke = DateTime.Now;
            try
            {
                return await ApiHostHelper.InvokeAsync<TResult>(this, this, action, args, cookie ?? Cookie).ConfigureAwait(false);
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    Logined = false;
                    // 如果当前不是登录,且设置了用户名,尝试自动登录
                    if (action != LoginAction && !UserName.IsNullOrEmpty())
                    {
                        await LoginAsync();
                        return await ApiHostHelper.InvokeAsync<TResult>(this, this, action, args, cookie ?? Cookie).ConfigureAwait(false);
                    }
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException(action + "超时取消");
            }
        }
Пример #9
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action"></param>
 /// <param name="args"></param>
 /// <param name="cookie">附加参数,位于顶级</param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, IDictionary <String, Object> cookie = null)
 {
     return(await ApiHostHelper.InvokeAsync <TResult>(_Host, this, action, args, cookie).ConfigureAwait(false));
 }
Пример #10
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action">服务操作</param>
 /// <param name="args">参数</param>
 /// <param name="flag">标识</param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, Byte flag = 0) => (TResult)await ApiHostHelper.InvokeAsync(_Host, this, typeof(TResult), action, args, flag);
Пример #11
0
        /// <summary>指定客户端的异步调用,等待返回结果</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="client">客户端</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        protected virtual async Task <TResult> InvokeWithClientAsync <TResult>(ISocketClient client, String action, Object args = null, Byte flag = 0)
        {
            var act = action;

            return((TResult)await ApiHostHelper.InvokeAsync(this, client, typeof(TResult), act, args, flag));
        }
Пример #12
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(string action, object args = null)
 {
     return(await ApiHostHelper.InvokeAsync <TResult>(_ApiHost, this, action, args).ConfigureAwait(false));
 }