Exemplo n.º 1
0
 private async Task ProcessRequestAsync(ICoapConnectionInformation connection, byte[] payload)
 {
     try
     {
         await _coapHandler.ProcessRequestAsync(connection, payload);
     }
     catch (Exception ex)
     {
         _logger?.LogError(CoapUdpLoggingEvents.TransportRequestsLoop, ex, "Unexpected exception was thrown");
         throw;
     }
 }
Exemplo n.º 2
0
        private async Task HandleSession(CoapDtlsServerClientEndPoint session)
        {
            var state = new Dictionary <string, object>
            {
                { "RemoteEndPoint", session?.EndPoint }
            };

            using (_logger.BeginScope(state))
            {
                try
                {
                    _logger.LogDebug("Trying to accept TLS connection from {EndPoint}", session.EndPoint);

                    var server = _tlsServerFactory.Create();

                    session.Accept(_serverProtocol, server);

                    if (session.ConnectionInfo != null)
                    {
                        _logger.LogInformation("New TLS connection from {EndPoint}, Server Info: {ServerInfo}", session.EndPoint, session.ConnectionInfo);
                    }
                    else
                    {
                        _logger.LogInformation("New TLS connection from {EndPoint}", session.EndPoint);
                    }

                    var connectionInfo = new CoapDtlsConnectionInformation
                    {
                        LocalEndpoint  = _endPoint,
                        RemoteEndpoint = session,
                        TlsServer      = server
                    };

                    while (!session.IsClosed && !_cts.IsCancellationRequested)
                    {
                        var packet = await session.ReceiveAsync(_cts.Token);

                        _logger.LogDebug("Handling CoAP Packet from {EndPoint}", session.EndPoint);
                        await _coapHandler.ProcessRequestAsync(connectionInfo, packet.Payload);

                        _logger.LogDebug("CoAP request from {EndPoint} handled!", session.EndPoint);
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (DtlsConnectionClosedException)
                {
                }
                catch (TlsFatalAlert tlsAlert)
                {
                    if (!(tlsAlert.InnerException is DtlsConnectionClosedException) && tlsAlert.AlertDescription != AlertDescription.user_canceled)
                    {
                        _logger.LogWarning(tlsAlert, "TLS Error");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error while handling session");
                }
                finally
                {
                    _logger.LogInformation("Connection from {EndPoint} closed", session.EndPoint);
                    _sessions.TryRemove(session.EndPoint, out _);
                }
            }
        }