Exemplo n.º 1
0
        public async Task ConnectAsync(CoapClientConnectOptions options, CancellationToken cancellationToken)
        {
            _connectOptions = options ?? throw new ArgumentNullException(nameof(options));

            var transportLayer = options.TransportLayerFactory?.Invoke();

            if (transportLayer == null)
            {
                throw new InvalidOperationException("No CoAP transport layer is set.");
            }

            cancellationToken.ThrowIfCancellationRequested();

            _transportLayerAdapter = new CoapTransportLayerAdapter(transportLayer, _logger);
            try
            {
                var transportLayerConnectOptions = new CoapTransportLayerConnectOptions
                {
                    EndPoint = await ResolveIPEndPoint(options).ConfigureAwait(false)
                };

                await _transportLayerAdapter.ConnectAsync(transportLayerConnectOptions, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception)
            {
                _transportLayerAdapter.Dispose();
                throw;
            }
        }
Exemplo n.º 2
0
        public Task ConnectAsync(CoapTransportLayerConnectOptions connectOptions, CancellationToken cancellationToken)
        {
            _udpTransport = new UdpTransport(connectOptions);

            var clientProtocol = new DtlsClientProtocol(new SecureRandom());
            var client         = new DtlsClient(ConvertProtocolVersion(DtlsVersion), (PreSharedKey)Credentials);

            _dtlsTransport = clientProtocol.Connect(client, _udpTransport);

            return(Task.FromResult(0));
        }
Exemplo n.º 3
0
        public async Task ConnectAsync(CoapClientConnectOptions options, CancellationToken cancellationToken)
        {
            _connectOptions        = options ?? throw new ArgumentNullException(nameof(options));
            _transportLayerAdapter = new CoapTransportLayerAdapter(options.TransportLayer, _logger);

            var transportLayerConnectOptions = new CoapTransportLayerConnectOptions
            {
                EndPoint = await ResolveIPEndPoint(options).ConfigureAwait(false)
            };

            await _transportLayerAdapter.ConnectAsync(transportLayerConnectOptions, cancellationToken).ConfigureAwait(false);
        }
Exemplo n.º 4
0
        public async Task ConnectAsync(CoapTransportLayerConnectOptions connectOptions, CancellationToken cancellationToken)
        {
            if (connectOptions == null)
            {
                throw new ArgumentNullException(nameof(connectOptions));
            }

            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                _udpTransport = new UdpTransport(connectOptions);
                var clientProtocol = new DtlsClientProtocol(_secureRandom);
                _dtlsClient = new DtlsClient(ConvertProtocolVersion(DtlsVersion), (PreSharedKey)Credentials);

                using (cancellationToken.Register(() =>
                {
                    _udpTransport.Close();
                }))
                {
                    _dtlsTransport = await Task.Run(() => clientProtocol.Connect(_dtlsClient, _udpTransport), cancellationToken).ConfigureAwait(false);
                }
            }
            catch
            {
                _udpTransport?.Dispose();

                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }

                if (_dtlsClient.ReceivedAlert != 0)
                {
                    throw new DtlsException($"Received alert {AlertDescription.GetText(_dtlsClient.ReceivedAlert)}.", null)
                          {
                              ReceivedAlert = _dtlsClient.ReceivedAlert
                          };
                }

                throw;
            }
        }
Exemplo n.º 5
0
        public UdpTransport(CoapTransportLayerConnectOptions connectOptions)
        {
            _connectOptions = connectOptions ?? throw new ArgumentNullException(nameof(connectOptions));

            _socket = new Socket(connectOptions.EndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
        }