Exemplo n.º 1
0
        async Task <IPEndPoint> ResolveIPEndPoint(CoapClientConnectOptions connectOptions)
        {
            if (IPAddress.TryParse(connectOptions.Host, out var ipAddress))
            {
                return(new IPEndPoint(ipAddress, connectOptions.Port));
            }
            else
            {
#if NETSTANDARD1_3
                await Task.FromResult(0);

                throw new NotSupportedException("Resolving DNS end points is not supported for NETSTANDARD1_3. Please pass IP address instead.");
#else
                var hostIPAddresses = await Dns.GetHostAddressesAsync(connectOptions.Host).ConfigureAwait(false);

                if (hostIPAddresses.Length == 0)
                {
                    throw new CoapCommunicationException("Failed to resolve DNS end point", null);
                }

                // We only use the first address for now.
                return(new IPEndPoint(hostIPAddresses[0], _connectOptions.Port));
#endif
            }
        }
Exemplo n.º 2
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;
            }
        }
        public async Task ConnectAsync(CoapClientConnectOptions options, CancellationToken cancellationToken)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _connectOptions = options;

            if (_dtlsClient != null)
            {
                _dtlsClient.OnDatagramReceived -= OnDatagramReceived;
                _dtlsClient.Dispose();
            }

            _udpClient?.Dispose();

            await ResolveIPEndpointAsync().ConfigureAwait(false);

            ConvertCredentials();

            // ! Match the local address family with the address family of the host!
            _udpClient  = new UdpClient(0, _ipEndPoint.AddressFamily);
            _dtlsClient = new Waher.Security.DTLS.DtlsOverUdp(_udpClient, Waher.Security.DTLS.DtlsMode.Client, null, null);
            _dtlsClient.OnDatagramReceived += OnDatagramReceived;
        }
Exemplo n.º 4
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);
        }