Exemple #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());
        }
Exemple #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);
        }
Exemple #3
0
        public ApiResult <object> InvokeApi(Type returnType, 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(returnType, this.UnWrap(), this.taskSetterTable, this.Serializer, packet, this.TimeOut));
        }
Exemple #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());
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #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);
     }
 }
Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #11
0
 /// <summary>
 ///  当操作中遇到处理异常时,将触发此方法
 /// </summary>
 /// <param name="packet">数据包对象</param>
 /// <param name="exception">异常对象</param>
 protected virtual void OnException(FastPacket packet, Exception exception)
 {
 }