Beispiel #1
0
        private async Task BindToNextendpoint(ICoapEndpoint endpoint, ICoapHandler handler)
        {
            _logger?.LogDebug(CoapLoggingEvents.ServerBindTo, "Binding to", endpoint);
            var transport = _transportFactory.Create(endpoint, handler);

            await transport.BindAsync();

            _transports.Add(transport);
        }
        public CoapDtlsServerTransport(CoapDtlsServerEndPoint endPoint, ICoapHandler coapHandler, IDtlsServerFactory tlsServerFactory, ILogger <CoapDtlsServerTransport> logger)
        {
            _endPoint         = endPoint ?? throw new ArgumentNullException(nameof(endPoint));
            _coapHandler      = coapHandler ?? throw new ArgumentNullException(nameof(coapHandler));
            _tlsServerFactory = tlsServerFactory ?? throw new ArgumentNullException(nameof(tlsServerFactory));
            _logger           = logger ?? throw new ArgumentNullException(nameof(logger));

            SecureRandom random = new SecureRandom();

            _serverProtocol = new DtlsServerProtocol(random);

            _sessions = new ConcurrentDictionary <IPEndPoint, CoapDtlsServerClientEndPoint>();
        }
        public ICoapTransport Create(ICoapEndpoint endPoint, ICoapHandler handler)
        {
            var serverEndpoint = endPoint as CoapDtlsServerEndPoint;

            if (serverEndpoint == null)
            {
                throw new ArgumentException($"Endpoint has to be {nameof(CoapDtlsServerEndPoint)}");
            }
            if (_transport != null)
            {
                throw new InvalidOperationException("CoAP transport may only be created once!");
            }

            _transport = new CoapDtlsServerTransport(serverEndpoint, handler, _tlsServerFactory, _loggerFactory.CreateLogger <CoapDtlsServerTransport>());
            return(_transport);
        }
Beispiel #4
0
        public async Task StartAsync(ICoapHandler handler, CancellationToken token)
        {
            if (Interlocked.CompareExchange(ref _serverState, (int)ServerState.Started, (int)ServerState.None) != (int)ServerState.None)
            {
                throw new InvalidOperationException($"{nameof(CoapServer)} has already started");
            }

            _logger?.LogDebug(CoapLoggingEvents.ServerStart, "Starting");

            var bindToQueue = Interlocked.Exchange(ref _bindToQueue, null);

            while (bindToQueue.Count > 0)
            {
                await BindToNextendpoint(bindToQueue.Dequeue(), handler);
            }

            // TODO: Implement MaxRequests
            _logger?.LogInformation(CoapLoggingEvents.ServerStart, "Started");
        }
Beispiel #5
0
 public CoapUdpTransport(CoapUdpEndPoint endPoint, ICoapHandler coapHandler, ILogger <CoapUdpTransport> logger = null)
 {
     _endPoint    = endPoint;
     _coapHandler = coapHandler;
     _logger      = logger;
 }
Beispiel #6
0
 public ICoapTransport Create(ICoapEndpoint endPoint, ICoapHandler handler)
 {
     return(new CoapUdpTransport(endPoint as CoapUdpEndPoint ?? throw new InvalidOperationException(), handler, _loggerFactory?.CreateLogger <CoapUdpTransport>()));
 }