예제 #1
0
        /// <summary>
        ///     Creates a new <see cref="IResourceLeakTracker" /> which is expected to be closed
        ///     when the
        ///     related resource is deallocated.
        /// </summary>
        /// <returns>the <see cref="IResourceLeakTracker" /> or <c>null</c></returns>
        public IResourceLeakTracker Track(object obj)
        {
            DetectionLevel level = Level;

            if (level == DetectionLevel.Disabled)
            {
                return(null);
            }

            if (level < DetectionLevel.Paranoid)
            {
                if (0u >= (uint)(PlatformDependent.GetThreadLocalRandom().Next(this.samplingInterval)))
                {
                    return(new DefaultResourceLeak(this, obj));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(new DefaultResourceLeak(this, obj));
            }
        }
예제 #2
0
        protected sealed override IByteBuffer NewBuffer(int length, int maxCapacity)
        {
            this.AssumedMaxCapacity = maxCapacity == int.MaxValue;

            int         offset = length == 0 ? 0 : PlatformDependent.GetThreadLocalRandom().Next(length);
            IByteBuffer buffer = Unpooled.Buffer(length * 2);
            IByteBuffer slice  = this.NewSlice(buffer, offset, length);

            Assert.Equal(0, slice.ReaderIndex);
            Assert.Equal(length, slice.WriterIndex);

            return(slice);
        }
예제 #3
0
            private void Record0(object hint)
            {
                // Check TARGET_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords
                if (s_targetRecords <= 0)
                {
                    return;
                }

                StackTrace stackTrace = null;

                var         thisHead = Volatile.Read(ref v_head);
                RecordEntry oldHead;
                RecordEntry prevHead;
                RecordEntry newHead;
                bool        dropped;

                do
                {
                    if ((prevHead = oldHead = thisHead) is null)
                    {
                        // already closed.
                        return;
                    }
                    int numElements = thisHead.Pos + 1;
                    if (numElements >= s_targetRecords)
                    {
                        int backOffFactor = Math.Min(numElements - s_targetRecords, 30);
                        dropped = PlatformDependent.GetThreadLocalRandom().Next(1 << backOffFactor) != 0;
                        if (dropped)
                        {
                            prevHead = thisHead.Next;
                        }
                    }
                    else
                    {
                        dropped = false;
                    }
                    stackTrace ??= new StackTrace(skipFrames: 3, fNeedFileInfo: true);
                    newHead  = hint is object?new RecordEntry(prevHead, stackTrace, hint) : new RecordEntry(prevHead, stackTrace);
                    thisHead = Interlocked.CompareExchange(ref v_head, newHead, thisHead);
                }while (thisHead != oldHead);
                if (dropped)
                {
                    _ = Interlocked.Increment(ref _droppedRecords);
                }
            }
예제 #4
0
            void Record0(object hint)
            {
                // Check TARGET_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords
                if (TargetRecords > 0)
                {
                    string stackTrace = Environment.StackTrace;

                    var         thisHead = Volatile.Read(ref this.head);
                    RecordEntry oldHead;
                    RecordEntry prevHead;
                    RecordEntry newHead;
                    bool        dropped;
                    do
                    {
                        if ((prevHead = oldHead = thisHead) is null)
                        {
                            // already closed.
                            return;
                        }
                        int numElements = thisHead.Pos + 1;
                        if (numElements >= TargetRecords)
                        {
                            int backOffFactor = Math.Min(numElements - TargetRecords, 30);
                            dropped = PlatformDependent.GetThreadLocalRandom().Next(1 << backOffFactor) != 0;
                            if (dropped)
                            {
                                prevHead = thisHead.Next;
                            }
                        }
                        else
                        {
                            dropped = false;
                        }
                        newHead  = hint is object?new RecordEntry(prevHead, stackTrace, hint) : new RecordEntry(prevHead, stackTrace);
                        thisHead = Interlocked.CompareExchange(ref this.head, newHead, thisHead);
                    }while (thisHead != oldHead);
                    if (dropped)
                    {
                        _ = Interlocked.Increment(ref this.droppedRecords);
                    }
                }
            }
 // construct a generated delimiter
 static string GetNewMultipartDelimiter() => Convert.ToString(PlatformDependent.GetThreadLocalRandom().NextLong(), 16).ToLower();
예제 #6
0
        void TestHttpResponseAndFrameInSameBuffer(bool codec)
        {
            string url = "ws://localhost:9999/ws";
            WebSocketClientHandshaker shaker = this.NewHandshaker(new Uri(url));
            var handshaker = new Handshaker(shaker);

            var data = new byte[24];

            PlatformDependent.GetThreadLocalRandom().NextBytes(data);

            // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
            // to test the actual handshaker.
            var factory = new WebSocketServerHandshakerFactory(url, null, false);
            WebSocketServerHandshaker socketServerHandshaker = factory.NewHandshaker(shaker.NewHandshakeRequest());
            var websocketChannel = new EmbeddedChannel(socketServerHandshaker.NewWebSocketEncoder(),
                                                       socketServerHandshaker.NewWebsocketDecoder());

            Assert.True(websocketChannel.WriteOutbound(new BinaryWebSocketFrame(Unpooled.WrappedBuffer(data))));

            byte[] bytes = Encoding.ASCII.GetBytes("HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n");

            CompositeByteBuffer compositeByteBuf = Unpooled.CompositeBuffer();

            compositeByteBuf.AddComponent(true, Unpooled.WrappedBuffer(bytes));
            for (;;)
            {
                var frameBytes = websocketChannel.ReadOutbound <IByteBuffer>();
                if (frameBytes == null)
                {
                    break;
                }
                compositeByteBuf.AddComponent(true, frameBytes);
            }

            var ch = new EmbeddedChannel(new HttpObjectAggregator(int.MaxValue), new Handler(handshaker));

            if (codec)
            {
                ch.Pipeline.AddFirst(new HttpClientCodec());
            }
            else
            {
                ch.Pipeline.AddFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
            }

            // We need to first write the request as HttpClientCodec will fail if we receive a response before a request
            // was written.
            shaker.HandshakeAsync(ch).Wait();
            for (;;)
            {
                // Just consume the bytes, we are not interested in these.
                var buf = ch.ReadOutbound <IByteBuffer>();
                if (buf == null)
                {
                    break;
                }
                buf.Release();
            }
            Assert.True(ch.WriteInbound(compositeByteBuf));
            Assert.True(ch.Finish());

            var         frame  = ch.ReadInbound <BinaryWebSocketFrame>();
            IByteBuffer expect = Unpooled.WrappedBuffer(data);

            try
            {
                Assert.Equal(expect, frame.Content);
                Assert.True(frame.IsFinalFragment);
                Assert.Equal(0, frame.Rsv);
            }
            finally
            {
                expect.Release();
                frame.Release();
            }
        }