コード例 #1
0
        /// <summary>
        /// 收到fast请求
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private Task OnFastRequest(IContenxt context)
        {
            var fastPacket = default(FastPacket);

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

            if (fastPacket == null)
            {
                return(new Task(() => { }));
            }

            if (context.Session.Protocol == null)
            {
                var wrapper = new FastSession(context.Session, this);
                context.Session.SetProtocolWrapper("fast", wrapper);
            }

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

            return(new Task(() =>
            {
                foreach (var packet in fastPackets)
                {
                    var requestContext = new RequestContext(fastSession, packet, context.AllSessions);
                    this.OnRecvFastPacket(requestContext);
                }
            }));
        }
コード例 #2
0
        /// <summary>
        /// 收到fast请求
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private Task OnFastRequestAsync(IContenxt context)
        {
            var fastPacket = default(FastPacket);

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

            // 数据未完整
            if (fastPacket == null)
            {
                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);
        }
コード例 #3
0
 /// <summary>
 /// 当接收到远程端的数据时,将触发此方法
 /// </summary>
 /// <param name="stream">接收到的历史数据</param>
 protected sealed override void OnReceive(INsStream stream)
 {
     while (true)
     {
         var packet = default(FastPacket);
         if (FastPacket.Parse(stream, out packet) == false)
         {
             stream.Clear();
         }
         if (packet == null)
         {
             break;
         }
         // 新线程处理业务内容
         Task.Factory.StartNew(() => this.OnReceivePacket(packet));
     }
 }
コード例 #4
0
ファイル: FastTcpClient.cs プロジェクト: zyanam/NetworkSocket
        /// <summary>
        /// 生成数据包
        /// </summary>
        /// <param name="streamReader">数据流</param>
        /// <returns></returns>
        private IList <FastPacket> GenerateFastPackets(ISessionStreamReader streamReader)
        {
            var list = new List <FastPacket>();

            while (true)
            {
                if (FastPacket.Parse(streamReader, out FastPacket packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }
コード例 #5
0
        /// <summary>
        /// 生成数据包
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="fastPacket">数据包</param>
        /// <returns></returns>
        private IEnumerable <FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket)
        {
            yield return(fastPacket);

            while (true)
            {
                var packet = default(FastPacket);
                if (FastPacket.Parse(context.Buffer, out packet) == false)
                {
                    yield break;
                }
                if (packet == null)
                {
                    yield break;
                }
                yield return(packet);
            }
        }
コード例 #6
0
        /// <summary>
        /// 生成数据包
        /// </summary>
        /// <param name="inputStream">数据流</param>
        /// <returns></returns>
        private IList <FastPacket> GenerateFastPackets(IStreamReader inputStream)
        {
            var list = new List <FastPacket>();

            while (true)
            {
                var packet = default(FastPacket);
                if (FastPacket.Parse(inputStream, out packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }
コード例 #7
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)
            {
                if (FastPacket.Parse(context.StreamReader, out FastPacket packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }
コード例 #8
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.InputStream, out packet) == false)
                {
                    return(list);
                }
                if (packet == null)
                {
                    return(list);
                }
                list.Add(packet);
            }
        }