Beispiel #1
0
        /// <summary>单向发送。同步调用,不等待返回</summary>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual Boolean InvokeOneWay(String action, Object args = null, Byte flag = 0)
        {
            if (!Open())
            {
                return(false);
            }

            var act = action;

            return(ApiHostHelper.Invoke(this, this, act, args, flag));
        }
Beispiel #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)
        {
            // 让上层异步到这直接返回,后续代码在另一个线程执行
            await Task.Yield();

            Open();

            var act = action;

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

                    await waiter;

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

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException($"[{action}]超时[{Timeout:n0}ms]取消", ex);
            }
        }
Beispiel #3
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);
Beispiel #4
0
        /// <summary>指定客户端的异步调用,等待返回结果</summary>
        /// <remarks>常用于在OnLoginAsync中实现连接后登录功能</remarks>
        /// <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));
        }