Ejemplo n.º 1
0
        public void Run()
        {
            if (_disposed)
            {
                throw new InvalidOperationException("Cannot run disposed server");
            }
            _server = new TcpListener(_host, _port);

            // Start listening for client requests.
            _server.Start();
            _running = true;
            Task.Run(async() =>
            {
                while (_running)
                {
                    var tcpClient         = await _server.AcceptTcpClientAsync();
                    var cancelTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)); // todo configuration
                    var task = Task.Run(async() =>
                    {
                        var source          = tcpClient.GetStream();
                        var lines           = await new LineParser().Parse(source, cancelTokenSource.Token);
                        var httpRequest     = HttpRequest.Parse(lines);
                        var responseChannel = new HttpResponseChannel(tcpClient);
                        var httpContext     = new HttpContext(httpRequest, responseChannel, cancelTokenSource.Token);
                        _handler(httpContext);
                    }, cancelTokenSource.Token);
                }
            });
        }
Ejemplo n.º 2
0
        public void Run()
        {
            if (_disposed)
            {
                throw new InvalidOperationException("Cannot run disposed server");
            }
            _server = new TcpListener(_host, _port);

            // Start listening for client requests.
            _server.Start();
            _running = true;
            Task.Run(async () =>
            {
                while (_running)
                {
                    var tcpClient = await _server.AcceptTcpClientAsync();
                    var cancelTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)); // todo configuration
                    var task = Task.Run(async () =>
                    {
                        var source = tcpClient.GetStream();
                        var lines = await new LineParser().Parse(source, cancelTokenSource.Token);
                        var httpRequest = HttpRequest.Parse(lines);
                        var responseChannel = new HttpResponseChannel(tcpClient);
                        var httpContext = new HttpContext(httpRequest, responseChannel, cancelTokenSource.Token);
                        _handler(httpContext);
                    }, cancelTokenSource.Token);
                }
            });
        }
Ejemplo n.º 3
0
 public HttpContext(HttpRequest httpRequest, HttpResponseChannel responseChannel, CancellationToken token)
 {
     _httpRequest = httpRequest;
     _responseChannel = responseChannel;
     _token = token;
 }
Ejemplo n.º 4
0
 public HttpContext(HttpRequest httpRequest, HttpResponseChannel responseChannel, CancellationToken token)
 {
     _httpRequest     = httpRequest;
     _responseChannel = responseChannel;
     _token           = token;
 }