async Task EnsureServerInitializedAsync() { if (this.ServerAddress != null) { return; } int threadCount = Environment.ProcessorCount; var executorGroup = new MultithreadEventLoopGroup(threadCount); BlobSessionStatePersistenceProvider sessionStateProvider = await BlobSessionStatePersistenceProvider.CreateAsync( this.settingsProvider.GetSetting("BlobSessionStatePersistenceProvider.StorageConnectionString"), this.settingsProvider.GetSetting("BlobSessionStatePersistenceProvider.StorageContainerName")); TableQos2StatePersistenceProvider qos2StateProvider = await TableQos2StatePersistenceProvider.CreateAsync( this.settingsProvider.GetSetting("TableQos2StatePersistenceProvider.StorageConnectionString"), this.settingsProvider.GetSetting("TableQos2StatePersistenceProvider.StorageTableName")); var settings = new Settings(this.settingsProvider); var iotHubClientSettings = new IotHubClientSettings(this.settingsProvider); var authProvider = new SasTokenDeviceIdentityProvider(); var topicNameRouter = new ConfigurableMessageAddressConverter(); var iotHubClientFactory = IotHubClient.PreparePoolFactory(iotHubClientSettings.IotHubConnectionString, 400, TimeSpan.FromMinutes(5), iotHubClientSettings, PooledByteBufferAllocator.Default, topicNameRouter); MessagingBridgeFactoryFunc bridgeFactory = async identity => new SingleClientMessagingBridge(identity, await iotHubClientFactory(identity)); ServerBootstrap server = new ServerBootstrap() .Group(executorGroup) .Channel <TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .ChildOption(ChannelOption.AutoRead, false) .ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(TlsHandler.Server(this.tlsCertificate)); ch.Pipeline.AddLast( MqttEncoder.Instance, new MqttDecoder(true, 256 * 1024), new LoggingHandler("SERVER"), new MqttAdapter( settings, sessionStateProvider, authProvider, qos2StateProvider, bridgeFactory), new XUnitLoggingHandler(this.output)); })); IChannel serverChannel = await server.BindAsync(IPAddress.Any, this.ProtocolGatewayPort); this.ScheduleCleanup(async() => { await serverChannel.CloseAsync(); await executorGroup.ShutdownGracefullyAsync(); }); this.ServerAddress = IPAddress.Loopback; }
ServerBootstrap SetupBootstrap() { return(new ServerBootstrap() .Group(this.parentEventLoopGroup, this.eventLoopGroup) .Option(ChannelOption.SoBacklog, ListenBacklogSize) .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .ChildOption(ChannelOption.AutoRead, true) .Channel <TcpServerSocketChannel>() .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { var messagingAddressConverter = new ConfigurableMessageAddressConverter(); channel.Pipeline.AddLast(DataBasedTenancyHandler.HandlerName, new DataBasedTenancyHandler(settingsProvider)); channel.Pipeline.AddLast(DeviceTopicDecoder.HandlerName, new DeviceTopicDecoder(this.credentialProvider, messagingAddressConverter.TopicTemplates)); channel.Pipeline.AddLast(SocketIoTHubAdapter.HandlerName, new SocketIoTHubAdapter(this.settings, credentialProvider, this.authProvider)); }))); }
ServerBootstrap SetupBootstrap() { return(new ServerBootstrap() .Group(this.parentEventLoopGroup, this.eventLoopGroup) .Option(ChannelOption.SoBacklog, ListenBacklogSize) .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .ChildOption(ChannelOption.AutoRead, true) .Channel <TcpServerSocketChannel>() .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { if (this.tlsCertificate != null) { var tlsSettings = new ServerTlsSettings(this.tlsCertificate, true, true, System.Security.Authentication.SslProtocols.Tls12); var handler = new Tenancy.X509TlsTenancyHandler(tlsSettings, this.settingsProvider); channel.Pipeline.AddLast("certTls", handler); } var messagingAddressConverter = new ConfigurableMessageAddressConverter(); channel.Pipeline.AddLast(DeviceTopicDecoder.HandlerName, new DeviceTopicDecoder(this.credentialProvider, messagingAddressConverter.TopicTemplates)); channel.Pipeline.AddLast(SocketIoTHubAdapter.HandlerName, new SocketIoTHubAdapter(this.settings, credentialProvider, this.authProvider)); }))); }