Ejemplo n.º 1
0
        public MsgFrame[] ToFrame(int headerLength = 0)
        {
            var msg      = this;
            var dataBuf  = msg.ToString();
            var dataLen  = dataBuf.Length;
            var frameLen = MsgFrame.MAX_DATA_LENGTH - headerLength;

            var frameCount = (int)Math.Ceiling(dataLen * 1.0 / frameLen);
            var frames     = new MsgFrame[frameCount];

            var id = Counter++;

            for (var i = 0; i < frameCount; i++)
            {
                var takeLength = dataLen >= frameLen ? frameLen : dataLen;

                frames[i] = new MsgFrame
                {
                    Id     = id,
                    Target = msg.TargetUser,
                    Total  = frameCount,
                    Offset = i,
                    Data   = dataBuf.Substring(i * frameLen, takeLength)
                };
                dataLen -= takeLength;
            }

            return(frames);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 不需要缓存时返回false,需要时返回true
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool Check(MsgFrame frame)
        {
            List <MsgFrame> list;

            if (!Cache.ContainsKey(frame.Id))
            {
                list = new List <MsgFrame>(frame.Total);
                Cache.Add(frame.Id, list);
            }

            list = Cache[frame.Id];
            list.Add(frame);

            return(list.Count != frame.Total);
        }