Пример #1
0
        /// <summary>
        /// 处理正常的数据请求
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        private void ProcessRequest(RequestContext requestContext)
        {
            if (requestContext.Packet.IsFromClient == false)
            {
                FastTcpCommon.SetApiActionTaskResult(requestContext, this.TaskSetActionTable);
                return;
            }

            var action = this.GetApiAction(requestContext);

            if (action != null)
            {
                var actionContext  = new ActionContext(requestContext, action);
                var fastApiService = this.GetFastApiService(actionContext);
                if (fastApiService != null)
                {
                    // 执行Api行为
                    fastApiService.Execute(actionContext);
                    // 释放资源
                    this.DependencyResolver.TerminateService(fastApiService);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 获取Api行为
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        /// <returns></returns>
        private ApiAction GetApiAction(RequestContext requestContext)
        {
            var action = this.apiActionList.TryGet(requestContext.Packet.ApiName);

            if (action != null)
            {
                return(action);
            }

            var exception        = new ApiNotExistException(requestContext.Packet.ApiName);
            var exceptionContext = new ExceptionContext(requestContext, exception);

            FastTcpCommon.SetRemoteException(this, exceptionContext);

            var exceptionHandled = false;

            this.OnException(requestContext.Packet, exception, out exceptionHandled);
            if (exceptionHandled == false)
            {
                throw exception;
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// 获取FastApiService实例
        /// </summary>
        /// <param name="actionContext">Api行为上下文</param>
        /// <returns></returns>
        private IFastApiService GetFastApiService(ActionContext actionContext)
        {
            IFastApiService fastApiService = null;
            Exception       innerException = null;

            try
            {
                fastApiService = (IFastApiService)this.DependencyResolver.GetService(actionContext.Action.DeclaringService);
            }
            catch (Exception ex)
            {
                innerException = ex;
            }

            if (fastApiService == null)
            {
                var exception        = new ResolveException(actionContext.Action.DeclaringService, innerException);
                var exceptionContext = new ExceptionContext(actionContext, exception);

                FastTcpCommon.SetRemoteException(actionContext.Session, exceptionContext);
                this.ExecGlobalExceptionFilters(exceptionContext);
            }
            return(fastApiService);
        }
Пример #4
0
 /// <summary>
 /// 调用服务端实现的Api
 /// 并返回结果数据任务
 /// </summary>
 /// <typeparam name="T">返回值类型</typeparam>
 /// <param name="api">Api行为的api</param>
 /// <param name="parameters">参数</param>
 /// <exception cref="SocketException"></exception>
 /// <exception cref="SerializerException"></exception>
 /// <returns>远程数据任务</returns>
 public Task <T> InvokeApi <T>(string api, params object[] parameters)
 {
     return(FastTcpCommon.InvokeApi <T>(this, this.taskSetActionTable, this.Serializer, api, this.packetIdProvider.GetId(), true, parameters));
 }