示例#1
0
        public async Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();

            try
            {
                if (timeout == TimeSpan.Zero)
                {
                    await _channel.ConnectAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await MqttTaskTimeout.WaitAsync(t => _channel.ConnectAsync(t), timeout, cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception exception)
            {
                if (IsWrappedException(exception))
                {
                    throw;
                }

                WrapException(exception);
            }
        }
        public void Setup()
        {
            var factory   = new MqttFactory();
            var tcpServer = new MqttTcpServerAdapter(new MqttNetEventLogger());

            tcpServer.ClientHandler += args =>
            {
                _serverChannel =
                    (IMqttChannel)args.GetType().GetField("_channel",
                                                          System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                    .GetValue(args);

                return(Task.CompletedTask);
            };

            _mqttServer = factory.CreateMqttServer(new[] { tcpServer }, new MqttNetEventLogger());

            var serverOptions = new MqttServerOptionsBuilder().Build();

            _mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult();

            var clientOptions = new MqttClientOptionsBuilder()
                                .WithTcpServer("localhost").Build();

            var tcpOptions = (MqttClientTcpOptions)clientOptions.ChannelOptions;

            _clientChannel = new MqttTcpChannel(new MqttClientOptions {
                ChannelOptions = tcpOptions
            });

            _clientChannel.ConnectAsync(CancellationToken.None).GetAwaiter().GetResult();
        }
示例#3
0
        public Task ConnectAsync(TimeSpan timeout)
        {
            ThrowIfDisposed();
            _logger.Verbose <MqttChannelAdapter>("Connecting [Timeout={0}]", timeout);

            return(ExecuteAndWrapExceptionAsync(() => _channel.ConnectAsync().TimeoutAfter(timeout)));
        }
示例#4
0
        public async Task ConnectAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            try
            {
                await _channel.ConnectAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                if (!WrapAndThrowException(exception))
                {
                    throw;
                }
            }
        }
示例#5
0
        public async Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();

            try
            {
                _logger.Verbose("Connecting [Timeout={0}]", timeout);

                await Common.TaskExtensions
                .TimeoutAfterAsync(ct => _channel.ConnectAsync(), timeout, cancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                if (IsWrappedException(exception))
                {
                    throw;
                }

                WrapException(exception);
            }
        }
示例#6
0
        public Task ConnectAsync(TimeSpan timeout)
        {
            _logger.Trace <MqttChannelAdapter>("Connecting [Timeout={0}]", timeout);

            return(ExecuteAndWrapExceptionAsync(() => _channel.ConnectAsync().TimeoutAfter(timeout)));
        }
示例#7
0
        public async Task ConnectAsync(TimeSpan timeout)
        {
            _logger.Info <MqttChannelAdapter>("Connecting [Timeout={0}]", timeout);

            await ExecuteAndWrapExceptionAsync(() => _channel.ConnectAsync().TimeoutAfter(timeout));
        }