Example #1
0
        public static void RunSingleSegmentParser(int numberOfRequests, int concurrentConnections, byte[] requestPayload, Action <HttpRequestSingleSegment, WritableBuffer> writeResponse)
        {
            var factory  = new PipeFactory();
            var listener = new FakeListener(factory, concurrentConnections);

            listener.OnConnection(async connection =>
            {
                while (true)
                {
                    // Wait for data
                    var result           = await connection.Input.ReadAsync();
                    ReadableBuffer input = result.Buffer;

                    try
                    {
                        if (input.IsEmpty && result.IsCompleted)
                        {
                            // No more data
                            break;
                        }

                        var requestBytes = input.First.Span;

                        if (requestBytes.Length != 492)
                        {
                            continue;
                        }
                        // Parse the input http request
                        HttpRequestSingleSegment parsedRequest = HttpRequestSingleSegment.Parse(requestBytes);

                        // Writing directly to pooled buffers
                        var output = connection.Output.Alloc();
                        writeResponse(parsedRequest, output);
                        await output.FlushAsync();
                    }
                    catch (Exception e)
                    {
                        var istr = new Utf8String(input.First.Span).ToString();
                        Debug.WriteLine(e.Message);
                    }
                    finally
                    {
                        // Consume the input
                        connection.Input.Advance(input.End, input.End);
                    }
                }
            });

            var tasks = new Task[numberOfRequests];

            for (int i = 0; i < numberOfRequests; i++)
            {
                tasks[i] = listener.ExecuteRequestAsync(requestPayload);
            }

            Task.WaitAll(tasks);

            listener.Dispose();
            factory.Dispose();
        }
        public void Dispose()
        {
            _transport.Input.Complete();
            _transport.Output.Complete();

            _application.Input.Complete();
            _application.Output.Complete();

            _pipelineFactory.Dispose();
        }
        public void Dispose()
        {
            _output.Reader.CancelPendingRead();
            _input.Reader.CancelPendingRead();
            _output.Writer.Complete();
            _input.Writer.Complete();

            if (_ownsFactory)
            {
                _factory?.Dispose();
            }
            _factory = null;
        }
Example #4
0
 /// <summary>
 /// Releases all resources owned by the listener
 /// </summary>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         GC.SuppressFinalize(this);
         _socket?.Dispose();
         _socket = null;
         if (_ownsFactory)
         {
             _factory?.Dispose();
         }
         _factory = null;
     }
 }
Example #5
0
        public async Task DisposeAsync()
        {
            if (!_disposed)
            {
                _disposed = true;
                _stopping = true;
                _output.Reader.CancelPendingRead();

                await Task.WhenAll(_sendTask, _receiveTask);

                _output.Writer.Complete();
                _input.Reader.Complete();

                _socket?.Dispose();
                _socket = null;
                if (_ownsFactory)
                {
                    _factory?.Dispose();
                }
                _factory = null;
            }
        }
Example #6
0
        /// <summary>
        /// Releases all resources owned by the connection
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _stopping = true;
                _output.Reader.CancelPendingRead();

                Task.WaitAll(_sendTask, _receiveTask);

                _output.Writer.Complete();
                _input.Reader.Complete();

                GC.SuppressFinalize(this);

                _socket?.Dispose();
                _socket = null;
                if (_ownsFactory)
                {
                    _factory?.Dispose();
                }
                _factory = null;
            }
        }
 protected override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     pipeFactory.Dispose();
 }
 public void Dispose()
 {
     _pipeFactory.Dispose();
 }
Example #9
0
 public void Dispose()
 {
     _pipe.Writer.Complete();
     _pipe.Reader.Complete();
     _pipeFactory?.Dispose();
 }
 public void Dispose()
 {
     _pipelineFactory.Dispose();
     _memoryPool.Dispose();
 }
Example #11
0
        private void ThreadStart(object parameter)
        {
            lock (_startSync)
            {
                var tcs = (TaskCompletionSource <int>)parameter;
                try
                {
                    _loop.Init(_transport.Libuv);
                    _post.Init(_loop, OnPost, EnqueueCloseHandle);
                    _initCompleted = true;
                    tcs.SetResult(0);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                    return;
                }
            }

            try
            {
                _loop.Run();
                if (_stopImmediate)
                {
                    // thread-abort form of exit, resources will be leaked
                    return;
                }

                // run the loop one more time to delete the open handles
                _post.Reference();
                _post.Dispose();

                // We need this walk because we call ReadStop on on accepted connections when there's back pressure
                // Calling ReadStop makes the handle as in-active which means the loop can
                // end while there's still valid handles around. This makes loop.Dispose throw
                // with an EBUSY. To avoid that, we walk all of the handles and dispose them.
                Walk(ptr =>
                {
                    var handle = UvMemory.FromIntPtr <UvHandle>(ptr);
                    // handle can be null because UvMemory.FromIntPtr looks up a weak reference
                    handle?.Dispose();
                });

                // Ensure the Dispose operations complete in the event loop.
                _loop.Run();

                _loop.Dispose();
            }
            catch (Exception ex)
            {
                _closeError = ExceptionDispatchInfo.Capture(ex);
                // Request shutdown so we can rethrow this exception
                // in Stop which should be observable.
                _appLifetime.StopApplication();
            }
            finally
            {
                PipeFactory.Dispose();
                WriteReqPool.Dispose();
                _threadTcs.SetResult(null);

#if DEBUG
                // Check for handle leaks after disposing everything
                CheckUvReqLeaks();
#endif
            }
        }
 public void Dispose()
 {
     _libuvThread.StopAsync(TimeSpan.FromSeconds(1)).Wait();
     _pipeFactory.Dispose();
 }
Example #13
0
        public void Dispose()
        {
            Stop();

            PipeFactory.Dispose();
        }
 public Task StopAsync()
 {
     _pipeFactory.Dispose();
     return(Task.CompletedTask);
 }
Example #15
0
 public void Dispose()
 {
     _input.Reader.Complete();
     _input.Writer.Complete();
     _pipelineFactory.Dispose();
 }
 public void Dispose()
 {
     EPoll?.Dispose();
     PipeEnds.Dispose();
     PipeFactory?.Dispose();
 }