示例#1
0
        public async Task <IMqttConnectedClient> CreateClientAsync(MqttConfiguration configuration)
        {
            try
            {
                //Adding this to not break backwards compatibility related to the method signature
                //Yielding at this point will cause the method to return immediately after it's called,
                //running the rest of the logic acynchronously
                await Task.Yield();

                var binding             = new PrivateBinding(privateStreamListener, EndpointIdentifier.Client);
                var topicEvaluator      = new MqttTopicEvaluator(configuration);
                var innerChannelFactory = binding.GetChannelFactory(IPAddress.Loopback.ToString(), configuration);
                var channelFactory      = new PacketChannelFactory(innerChannelFactory, topicEvaluator, configuration);
                var packetIdProvider    = new PacketIdProvider();
                var repositoryProvider  = new InMemoryRepositoryProvider();
                var flowProvider        = new ClientProtocolFlowProvider(topicEvaluator, repositoryProvider, configuration);

                return(new MqttConnectedClient(channelFactory, flowProvider, repositoryProvider, packetIdProvider, configuration));
            }
            catch (Exception ex)
            {
                tracer.Error(ex, Properties.Resources.Client_InitializeError);

                throw new MqttClientException(Properties.Resources.Client_InitializeError, ex);
            }
        }
示例#2
0
        /// <summary>
        /// Creates an MQTT Client
        /// </summary>
        /// <param name="configuration">
        /// The configuration used for creating the Client
        /// See <see cref="MqttConfiguration" /> for more details about the supported values
        /// </param>
        /// <returns>A new MQTT Client</returns>
        /// <exception cref="MqttClientException">MqttClientException</exception>
        public async Task <IMqttClient> CreateClientAsync(MqttConfiguration configuration)
        {
            try {
                var topicEvaluator      = new MqttTopicEvaluator(configuration);
                var innerChannelFactory = binding.GetChannelFactory(hostAddress, configuration);
                var channelFactory      = new PacketChannelFactory(innerChannelFactory, topicEvaluator, configuration);
                var packetIdProvider    = new PacketIdProvider();
                var repositoryProvider  = new InMemoryRepositoryProvider();
                var flowProvider        = new ClientProtocolFlowProvider(topicEvaluator, repositoryProvider, configuration);
                var packetChannel       = await channelFactory
                                          .CreateAsync()
                                          .ConfigureAwait(continueOnCapturedContext: false);

                return(new MqttClientImpl(packetChannel, flowProvider, repositoryProvider, packetIdProvider, configuration));
            } catch (Exception ex) {
                tracer.Error(ex, Properties.Resources.Client_InitializeError);

                throw new MqttClientException(Properties.Resources.Client_InitializeError, ex);
            }
        }
示例#3
0
        /// <summary>
        /// Creates an MQTT Server
        /// </summary>
        /// <param name="configuration">
        /// The configuration used for creating the Server
        /// See <see cref="MqttConfiguration" /> for more details about the supported values
        /// </param>
        /// <returns>A new MQTT Server</returns>
        /// <exception cref="MqttServerException">MqttServerException</exception>
        public IMqttServer CreateServer(MqttConfiguration configuration)
        {
            try {
                var topicEvaluator              = new MqttTopicEvaluator(configuration);
                var channelProvider             = binding.GetChannelListener(configuration);
                var channelFactory              = new PacketChannelFactory(topicEvaluator, configuration);
                var repositoryProvider          = new InMemoryRepositoryProvider();
                var connectionProvider          = new ConnectionProvider();
                var packetIdProvider            = new PacketIdProvider();
                var undeliveredMessagesListener = new Subject <MqttUndeliveredMessage> ();
                var flowProvider = new ServerProtocolFlowProvider(authenticationProvider, connectionProvider, topicEvaluator,
                                                                  repositoryProvider, packetIdProvider, undeliveredMessagesListener, configuration);

                return(new MqttServerImpl(channelProvider, channelFactory,
                                          flowProvider, connectionProvider, undeliveredMessagesListener, configuration));
            } catch (Exception ex) {
                tracer.Error(ex, ServerProperties.Resources.Server_InitializeError);

                throw new MqttServerException(ServerProperties.Resources.Server_InitializeError, ex);
            }
        }