Esempio n. 1
0
        /// <summary>
        /// 调用服务端实现的Api
        /// </summary>
        /// <param name="api">Api行为的api</param>
        /// <param name="parameters">参数列表</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="SerializerException"></exception>
        public void InvokeApi(string api, params object[] parameters)
        {
            var packet = new FastPacket(api, this.packetIdProvider.NewId(), true);

            packet.SetBodyParameters(this.Serializer, parameters);
            this.Send(packet.ToArraySegment());
        }
Esempio n. 2
0
        /// <summary>
        /// 收到fast请求
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private Task OnFastRequestAsync(IContenxt context)
        {
            var fastPacket = default(FastPacket);

            if (FastPacket.Parse(context.StreamReader, out fastPacket) == false)
            {
                return(this.Next.Invoke(context));
            }

            // 数据未完整
            if (fastPacket == null)
            {
                var wrapper = new FastSession(context.Session, this);
                context.Session.SetProtocolWrapper(Protocol.Fast, wrapper);
                return(TaskExtend.CompletedTask);
            }

            if (context.Session.Protocol == Protocol.None)
            {
                var wrapper = new FastSession(context.Session, this);
                context.Session.SetProtocolWrapper(Protocol.Fast, wrapper);
            }

            var fastSession = (FastSession)context.Session.Wrapper;
            var fastPackets = this.GenerateFastPackets(context, fastPacket);

            foreach (var packet in fastPackets)
            {
                var requestContext = new RequestContext(fastSession, packet, context.AllSessions);
                this.OnRecvFastPacketAsync(requestContext);
            }
            return(TaskExtend.CompletedTask);
        }
Esempio n. 3
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 ApiResult <T> InvokeApi <T>(string api, params object[] parameters)
        {
            var id     = this.packetIdProvider.NewId();
            var packet = new FastPacket(api, id, true);

            packet.SetBodyParameters(this.Serializer, parameters);
            return(Common.InvokeApi <T>(this.UnWrap(), this.taskSetterTable, this.Serializer, packet, this.TimeOut));
        }
Esempio n. 4
0
        /// <summary>
        /// 调用远程端实现的Api
        /// </summary>
        /// <param name="api">数据包Api名</param>
        /// <param name="parameters">参数列表</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="SerializerException"></exception>
        public void InvokeApi(string api, params object[] parameters)
        {
            var id     = this.middleware.PacketIdProvider.NewId();
            var packet = new FastPacket(api, id, false);

            packet.SetBodyParameters(this.middleware.Serializer, parameters);
            this.session.Send(packet.ToArraySegment());
        }
Esempio n. 5
0
        /// <summary>
        /// 调用远程端实现的Api
        /// 并返回结果数据任务
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="api">数据包Api名</param>
        /// <param name="parameters">参数</param>
        /// <exception cref="SocketException"></exception>
        /// <exception cref="SerializerException"></exception>
        /// <returns>远程数据任务</returns>
        public ApiResult <T> InvokeApi <T>(string api, params object[] parameters)
        {
            var id     = this.middleware.PacketIdProvider.NewId();
            var packet = new FastPacket(api, id, false);

            packet.SetBodyParameters(this.middleware.Serializer, parameters);
            return(Common.InvokeApi <T>(this.session, this.middleware.TaskSetterTable, this.middleware.Serializer, packet, this.middleware.TimeOut));
        }
Esempio n. 6
0
        /// <summary>
        /// 解析一个数据包
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="streamReader">数据读取器</param>
        /// <param name="packet">数据包</param>
        /// <returns></returns>
        public static bool Parse(ISessionStreamReader streamReader, out FastPacket packet)
        {
            packet = null;
            const int packetMinSize = 16;

            if (streamReader.Length < packetMinSize || streamReader[0] != FastPacket.Mark)
            {
                return(false);
            }

            streamReader.Position = 1;
            var totalBytes = streamReader.ReadInt32();

            if (totalBytes < packetMinSize)
            {
                return(false);
            }

            // 数据包未接收完整
            if (streamReader.Length < totalBytes)
            {
                return(true);
            }

            // api名称数据长度
            var apiNameLength = streamReader.ReadByte();

            if (totalBytes < apiNameLength + packetMinSize)
            {
                return(false);
            }

            // api名称数据
            var apiNameBytes = streamReader.ReadArray(apiNameLength);
            // 标识符
            var id = streamReader.ReadInt64();
            // 是否为客户端封包
            var isFromClient = streamReader.ReadBoolean();
            // 是否异常
            var isException = streamReader.ReadBoolean();
            // 实体数据
            var body = streamReader.ReadArray(totalBytes - streamReader.Position);

            // 清空本条数据
            streamReader.Clear(totalBytes);

            var apiName = Encoding.UTF8.GetString(apiNameBytes);

            packet = new FastPacket(apiName, id, isFromClient)
            {
                TotalBytes    = totalBytes,
                ApiNameLength = apiNameLength,
                IsException   = isException,
                Body          = body
            };
            return(true);
        }
Esempio n. 7
0
 /// <summary>
 /// 发送数据包
 /// </summary>
 /// <param name="package">数据包</param>
 /// <returns></returns>
 private bool TrySendPackage(FastPacket package)
 {
     try
     {
         return(this.Send(package.ToArraySegment()) > 0);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Esempio n. 8
0
        /// <summary>
        /// 处理接收到服务发来的数据包
        /// </summary>
        /// <param name="packet">数据包</param>
        private async void ProcessPacketAsync(FastPacket packet)
        {
            var requestContext = new RequestContext(null, packet, null);

            if (packet.IsException == true)
            {
                Common.SetApiActionTaskException(this.taskSetterTable, requestContext);
            }
            else if (packet.IsFromClient == true)
            {
                Common.SetApiActionTaskResult(requestContext, this.taskSetterTable, this.Serializer);
            }
            else
            {
                await TryProcessRequestPackageAsync(requestContext);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 生成数据包
        /// </summary>
        /// <param name="streamReader">数据流</param>
        /// <returns></returns>
        private IList <FastPacket> GenerateFastPackets(ISessionStreamReader streamReader)
        {
            var list = new List <FastPacket>();

            while (true)
            {
                var packet = default(FastPacket);
                if (FastPacket.Parse(streamReader, out packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// 生成数据包
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="fastPacket">数据包</param>
        /// <returns></returns>
        private IList <FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket)
        {
            var list = new List <FastPacket> {
                fastPacket
            };

            while (true)
            {
                var packet = default(FastPacket);
                if (FastPacket.Parse(context.StreamReader, out packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }
Esempio n. 11
0
 /// <summary>
 ///  当操作中遇到处理异常时,将触发此方法
 /// </summary>
 /// <param name="packet">数据包对象</param>
 /// <param name="exception">异常对象</param>
 protected virtual void OnException(FastPacket packet, Exception exception)
 {
 }
Esempio n. 12
0
 /// <summary>
 /// 请求上下文
 /// </summary>
 /// <param name="session">当前会话对象</param>
 /// <param name="packet">数据包对象</param>
 /// <param name="allSessions">所有会话对象</param>
 internal RequestContext(FastSession session, FastPacket packet, ISessionManager allSessions)
 {
     this.Session     = session;
     this.Packet      = packet;
     this.AllSessions = allSessions;
 }
Esempio n. 13
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 ApiResult <T> InvokeApi <T>(ISession session, TaskSetterTable <long> taskSetActionTable, ISerializer serializer, FastPacket packet, TimeSpan timeout)
        {
            var taskSetter = taskSetActionTable.Create <T>(packet.Id, timeout);

            session.Send(packet.ToArraySegment());
            return(new ApiResult <T>(taskSetter));
        }