Exemplo n.º 1
0
        /// <summary>检查缓存</summary>
        protected virtual void CheckCache()
        {
            var ms = Stream;

            if (ms == null)
            {
                Stream = ms = new MemoryStream();
            }

            // 超过该时间后按废弃数据处理
            var now = DateTime.Now;

            if (ms.Length > ms.Position && Last.AddMilliseconds(Expire) < now && (MaxCache <= 0 || MaxCache <= ms.Length))
            {
                var retain = ms.Length - ms.Position;
                var buf    = ms.ReadBytes(500);
                using var span = Tracer?.NewSpan("net:PacketCodec:DropCache", $"[{retain}]{buf.ToHex()}");
                span?.SetError(new Exception($"数据包编码器放弃数据 retain={retain} MaxCache={MaxCache}"), null);

                if (XTrace.Debug)
                {
                    XTrace.Log.Debug("数据包编码器放弃数据 {0:n0},Last={1},MaxCache={2:n0}", ms.Length, Last, MaxCache);
                }

                ms.SetLength(0);
                ms.Position = 0;
            }
            Last = now;
        }
Exemplo n.º 2
0
        /// <summary>检查缓存</summary>
        protected virtual void CheckCache()
        {
            var ms = Stream;

            if (ms == null)
            {
                Stream = ms = new MemoryStream();
            }

            // 超过该时间后按废弃数据处理
            var now = TimerX.Now;

            if (ms.Length > ms.Position && Last.AddMilliseconds(Expire) < now && (MaxCache <= 0 || MaxCache <= ms.Length))
            {
                if (XTrace.Debug)
                {
                    XTrace.Log.Debug("数据包编码器放弃数据 {0:n0},Last={1},MaxCache={2:n0}", ms.Length, Last, MaxCache);
                }

                ms.SetLength(0);
                ms.Position = 0;
            }
            Last = now;
        }