示例#1
0
        private async Task StartListenerAsync()
        {
            try
            {
                _tcpListener.Start();

                while (_isListening)
                {
                    // Await request.

                    var client = await _tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);

                    var request = new HttpListenerRequest(client);

                    // Handle request in a separate thread.

                    Task.Run(async() =>
                    {
                        // Process request.

                        var response = new HttpListenerResponse(request, client);

                        try
                        {
                            await request.ProcessAsync();

                            response.Initialize();

                            if (Request == null)
                            {
                                // No Request handlers exist. Respond with "Not Found".

                                response.NotFound();
                                response.Close();
                            }
                            else
                            {
                                // Invoke Request handlers.

                                Request(this, new HttpListenerRequestEventArgs(request, response));
                            }
                        }
                        catch (Exception)
                        {
                            response.CloseSocket();
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Diagnostics.Debug.WriteLine($"Exception in HttpListener.StartListenerAsync: {ex.Message}");
                //throw;
            }
            finally
            {
                _isListening = false;
                _cts         = null;
            }
        }
示例#2
0
        private async Task StartListenerAsync()
        {
            try
            {
                _tcpListener.Start();

                while (_isListening)
                {
                    // Await request.
                    var client = await _tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);

                    var request = new HttpListenerRequest(client);

                    // Handle request in a separate thread.
                    Task.Run(async() =>
                    {
                        // Process request.
                        var response = new HttpListenerResponse(request, client);

                        try
                        {
                            await request.ProcessAsync();

                            response.Initialize();

                            if (_handler == null)
                            {
                                // No Request handler exist. Respond with "Not Found".
                                response.NotFound();
                                response.Close();
                            }
                            else
                            {
                                // execute handler.
                                _handler.HandleContext(new HttpListenerContext(request, response));
                            }
                        }
                        catch (Exception)
                        {
                            response.CloseSocket();
                        }
                    });

                    testL++;
                }
            }
            finally
            {
                _isListening = false;
                _cts         = null;
            }
        }
示例#3
0
        private async Task StartListenerAsync()
        {
            _tcpListener.Start();

            while (!_cancellationToken.IsCancellationRequested)
            {
                // Await request.
                var client = await _tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);

                _cancellationToken.ThrowIfCancellationRequested();
                var request = new HttpListenerRequest(client, _cancellationToken);

                // Handle request in a separate thread.
                Task.Run(async() =>
                {
                    // Process request.
                    var response = new HttpListenerResponse(request, client);

                    try
                    {
                        await request.ProcessAsync();

                        response.Initialize();

                        _cancellationToken.ThrowIfCancellationRequested();

                        if (_handler == null)
                        {
                            // No Request handler exist. Respond with "Not Found".
                            response.NotFound();
                            response.Close();
                        }
                        else
                        {
                            // execute handler.
                            _handler.HandleContext(new HttpListenerContext(request, response), _cancellationToken);
                        }
                    }
                    catch (Exception)
                    {
                        response.CloseSocket();
                    }
                });
            }
        }
        private async Task listener()
        {
            try
            {
#if NETCore
                _tcpListener.Start();
#endif
#if WINDOWS_UWP
                await _tcpListener.StartAsync();
#endif
                while (_isListening)
                {
                    // Await request.

                    var client = await _tcpListener.AcceptTcpClientAsync();

                    var request = new HttpListenerRequest(client);

                    // Handle request in a separate thread.

                    Task.Run(async() =>
                    {
                        // Process request.

                        var response = new HttpListenerResponse(request, client);

                        try
                        {
                            await request.ProcessAsync();

                            response.Initialize();

                            if (Request == null)
                            {
                                // No Request handlers exist. Respond with "Not Found".

                                response.NotFound();
                                response.Close();
                            }
                            else
                            {
                                // Invoke Request handlers.

                                Request(this, new HttpListenerRequestEventArgs(request, response));
                            }
                        }
                        catch (Exception)
                        {
                            response.CloseSocket();
                        }
                    });
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                _isListening = false;
                _cts         = null;
            }
        }