ServerBootstrap SetupBootstrap()
        {
            // pull/customize configuration
            int      maxInboundMessageSize = this.settingsProvider.GetIntegerSetting("MaxInboundMessageSize", 256 * 1024);
            int      connectionPoolSize    = this.settingsProvider.GetIntegerSetting("IotHubClient.ConnectionPoolSize", DefaultConnectionPoolSize);
            TimeSpan connectionIdleTimeout = this.settingsProvider.GetTimeSpanSetting("IotHubClient.ConnectionIdleTimeout", DefaultConnectionIdleTimeout);
            string   connectionString      = this.iotHubClientSettings.IotHubConnectionString;

            // setup message processing logic
            var telemetryProcessing = TopicHandling.CompileParserFromUriTemplates(new[] { "devices/{deviceId}/messages/events" });
            var commandProcessing   = TopicHandling.CompileFormatterFromUriTemplate("devices/{deviceId}/messages/devicebound");
            MessagingBridgeFactoryFunc bridgeFactory = IotHubBridge.PrepareFactory(connectionString, connectionPoolSize,
                                                                                   connectionIdleTimeout, this.iotHubClientSettings, bridge =>
            {
                bridge.RegisterRoute(topic => true, new TelemetrySender(bridge, telemetryProcessing));                                                                    // handle all incoming messages with TelemetrySender
                bridge.RegisterSource(new CommandReceiver(bridge, PooledByteBufferAllocator.Default, commandProcessing));                                                 // handle device command queue
                bridge.RegisterSource(new MethodHandler("SendMessageToDevice", bridge, (request, dispatcher) => DispatchCommands(bridge.DeviceId, request, dispatcher))); // register
            });

            var acceptLimiter = new AcceptLimiter(MaxConcurrentAccepts);

            return(new ServerBootstrap()
                   .Group(this.parentEventLoopGroup, this.eventLoopGroup)
                   .Option(ChannelOption.SoBacklog, ListenBacklogSize)
                   .Option(ChannelOption.AutoRead, false)
                   .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default)
                   .ChildOption(ChannelOption.AutoRead, false)
                   .Channel <TcpServerSocketChannel>()
                   .Handler(acceptLimiter)
                   .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                channel.Pipeline.AddLast(
                    TlsHandler.Server(this.tlsCertificate),
                    new AcceptLimiterTlsReleaseHandler(acceptLimiter),
                    MqttEncoder.Instance,
                    new MqttDecoder(true, maxInboundMessageSize),
                    new MqttAdapter(
                        this.settings,
                        this.sessionStateManager,
                        this.authProvider,
                        this.qos2StateProvider,
                        bridgeFactory));
            })));
        }
 public AcceptLimiterTlsReleaseHandler(AcceptLimiter limiter)
 {
     this.limiter = limiter;
 }