Пример #1
0
 /// <summary>
 /// 初始化
 /// </summary>
 private void Init()
 {
     this.apiActionList      = new ApiActionList(Common.GetServiceApiActions(this.GetType()));
     this.packetIdProvider   = new PacketIdProvider();
     this.taskSetActionTable = new TaskSetActionTable();
     this.Serializer         = new DefaultSerializer();
 }
Пример #2
0
 /// <summary>
 /// 快速构建Tcp服务端
 /// </summary>
 public FastTcpClient()
 {
     this.apiActionList      = new ApiActionList(FastTcpCommon.GetServiceApiActions(this.GetType()));
     this.packetIdProvider   = new PacketIdProvider();
     this.taskSetActionTable = new TaskSetActionTable();
     this.Serializer         = new DefaultSerializer();
 }
Пример #3
0
        /// <summary>
        /// 调用远程端的Api
        /// 并返回结果数据任务
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="session">会话对象</param>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="serializer">序列化工具</param>
        /// <param name="packet">封包</param>
        /// <param name="timeout">超时时间</param>
        /// <exception cref="SocketException"></exception>
        /// <returns></returns>
        public static Task <T> InvokeApi <T>(ISession session, TaskSetActionTable taskSetActionTable, ISerializer serializer, FastPacket packet, TimeSpan timeout)
        {
            var task = taskSetActionTable.Create <T>(packet.Id, timeout);

            session.SendAsync(packet.ToByteRange());
            return(task);
        }
Пример #4
0
 /// <summary>
 /// 服务器的客户端对象
 /// </summary>
 /// <param name="serializer">序列化工具</param>
 /// <param name="packetIdProvider">数据包id提供者</param>
 /// <param name="taskSetActionTable">任务行为表</param>
 /// <param name="filterAttributeProvider">特性过滤器提供者</param>
 internal FastSession(PacketIdProvider packetIdProvider, TaskSetActionTable taskSetActionTable, ISerializer serializer, IFilterAttributeProvider filterAttributeProvider)
 {
     this.packetIdProvider        = packetIdProvider;
     this.taskSetActionTable      = taskSetActionTable;
     this.Serializer              = serializer;
     this.FilterAttributeProvider = filterAttributeProvider;
 }
Пример #5
0
        /// <summary>
        /// 快速构建Tcp服务端
        /// </summary>
        public FastTcpServer()
        {
            this.apiActionList      = new ApiActionList();
            this.packetIdProvider   = new PacketIdProvider();
            this.taskSetActionTable = new TaskSetActionTable();

            this.Serializer = new DefaultSerializer();
            this.FilterAttributeProvider = new FilterAttributeProvider();
        }
Пример #6
0
        /// <summary>
        /// 设置Api行为返回的任务结果
        /// </summary>
        /// <param name="requestContext">上下文</param>
        /// <param name="taskSetActionTable">任务行为表</param>
        public static void SetApiActionTaskResult(RequestContext requestContext, TaskSetActionTable taskSetActionTable)
        {
            var taskSetAction = taskSetActionTable.Take(requestContext.Packet.Id);

            if (taskSetAction != null)
            {
                var returnBytes = requestContext.Packet.Body;
                taskSetAction.SetAction(SetTypes.SetReturnReult, returnBytes);
            }
        }
Пример #7
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public override void Dispose()
        {
            base.Dispose();

            this.apiActionList = null;
            this.taskSetActionTable.Clear();
            this.taskSetActionTable = null;
            this.packetIdProvider   = null;
            this.Serializer         = null;
        }
Пример #8
0
        /// <summary>
        /// 调用远程端的Api
        /// 并返回结果数据任务
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="session">会话对象</param>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="serializer">序列化工具</param>
        /// <param name="packet">封包</param>
        /// <exception cref="SocketException"></exception>
        public static Task <T> InvokeApi <T>(ISession session, TaskSetActionTable taskSetActionTable, ISerializer serializer, FastPacket packet)
        {
            var taskSource    = new TaskCompletionSource <T>();
            var taskSetAction = new TaskSetAction <T>(serializer, taskSource);

            taskSetActionTable.Add(packet.Id, taskSetAction);

            session.Send(packet.ToByteRange());
            return(taskSource.Task);
        }
Пример #9
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否也释放托管资源</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                this.apiActionList = null;

                this.taskSetActionTable.Clear();
                this.taskSetActionTable = null;

                this.packetIdProvider = null;
                this.Serializer       = null;
            }
        }
Пример #10
0
        /// <summary>
        /// 设置Api行为返回的任务异常
        /// 设置失败则返远程异常对象
        /// </summary>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="requestContext">请求上下文</param>
        /// <returns></returns>
        public static RemoteException SetApiActionTaskException(TaskSetActionTable taskSetActionTable, RequestContext requestContext)
        {
            var exceptionBytes = requestContext.Packet.Body;
            var taskSetAction  = taskSetActionTable.Take(requestContext.Packet.Id);

            if (taskSetAction != null)
            {
                taskSetAction.SetAction(SetTypes.SetReturnException, exceptionBytes);
                return(null);
            }

            var message = Encoding.UTF8.GetString(exceptionBytes);

            return(new RemoteException(message));
        }
Пример #11
0
        /// <summary>
        /// 设置Api行为返回的任务异常
        /// </summary>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="requestContext">请求上下文</param>
        /// <returns></returns>
        public static bool SetApiActionTaskException(TaskSetActionTable taskSetActionTable, RequestContext requestContext)
        {
            var taskSetAction = taskSetActionTable.Take(requestContext.Packet.Id);

            if (taskSetAction == null)
            {
                return(true);
            }

            var exceptionBytes = requestContext.Packet.Body;
            var message        = exceptionBytes == null ? string.Empty : Encoding.UTF8.GetString(exceptionBytes);
            var exception      = new RemoteException(message);

            return(taskSetAction.SetException(exception));
        }
Пример #12
0
        /// <summary>
        /// 调用远程端的Api
        /// 并返回结果数据任务
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="session">会话对象</param>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="serializer">序列化工具</param>
        /// <param name="api">api</param>
        /// <param name="id">标识符</param>
        /// <param name="fromClient">是否为客户端封包</param>
        /// <param name="parameters">参数</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="SerializerException"></exception>
        /// <returns></returns>
        public static Task <T> InvokeApi <T>(ISession session, TaskSetActionTable taskSetActionTable, ISerializer serializer, string api, long id, bool fromClient, params object[] parameters)
        {
            var taskSource = new TaskCompletionSource <T>();
            var packet     = new FastPacket(api, id, fromClient);

            packet.SetBodyParameters(serializer, parameters);

            // 登记TaskSetAction
            var setAction     = FastTcpCommon.NewSetAction <T>(taskSource, serializer);
            var taskSetAction = new TaskSetAction(setAction);

            taskSetActionTable.Add(packet.Id, taskSetAction);

            session.Send(packet.ToByteRange());
            return(taskSource.Task);
        }
Пример #13
0
        /// <summary>
        /// 设置Api行为返回的任务结果
        /// </summary>
        /// <param name="requestContext">上下文</param>
        /// <param name="taskSetActionTable">任务行为表</param>
        /// <param name="serializer">序列化工具</param>
        /// <returns></returns>
        public static bool SetApiActionTaskResult(RequestContext requestContext, TaskSetActionTable taskSetActionTable, ISerializer serializer)
        {
            var taskSetAction = taskSetActionTable.Take(requestContext.Packet.Id);

            if (taskSetAction == null)
            {
                return(true);
            }

            try
            {
                var bytes = requestContext.Packet.Body;
                var value = serializer.Deserialize(bytes, taskSetAction.ValueType);
                return(taskSetAction.SetResult(value));
            }
            catch (SerializerException ex)
            {
                return(taskSetAction.SetException(ex));
            }
            catch (Exception ex)
            {
                return(taskSetAction.SetException(new SerializerException(ex)));
            }
        }