static async Task RunServerAsync() { Console.WriteLine("Gateway Start, ThreadName=" + System.Threading.Thread.CurrentThread.ManagedThreadId); var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); try { var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler(LogLevel.INFO)) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new LengthFieldPrepender(2)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2)); pipeline.AddLast(new GatewayHandler()); })); IChannel bootstrapChannel = await bootstrap.BindAsync(GatewaySettings.Port); Console.ReadLine(); await bootstrapChannel.CloseAsync(); } finally { Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync()); } }
public NettyClient(string host, int port, string instanceId, Action<string, byte[]> dataReceivedAction, Action connectedAction) { this.host = host; this.port = port; this.instanceId = instanceId; this.dataReceivedAction = dataReceivedAction; this.connectedAction = connectedAction; this.cts = new CancellationTokenSource(); var hostEntry = Dns.GetHostEntry(host); this.firstAddress = hostEntry.AddressList.First(x => x.AddressFamily == AddressFamily.InterNetwork); this.group = new MultithreadEventLoopGroup(); this.bootstrap = new Bootstrap(); this.bootstrap .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; //TODO: Send InstanceId in the pipeline instead of part of the data buffer pipeline.AddLast("framing-enc", new LengthFieldPrepender(2)); pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(32 * 1024, 0, 2, 0, 2)); pipeline.AddLast("main", new NettyClientHandler(this)); })); }
public async Task StartAsync(EndPoint endPoint) { if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebug($"准备启动服务主机,监听地址:{endPoint}。"); var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel => { var pipeline = channel.Pipeline; pipeline.AddLast(new LengthFieldPrepender(4)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4)); pipeline.AddLast(new TransportMessageChannelHandlerAdapter(_transportMessageDecoder)); pipeline.AddLast(new ServerHandler((contenxt, message) => { var sender = new DotNettyServerMessageSender(_transportMessageEncoder, contenxt); OnReceived(sender, message); }, _logger)); })); _channel = await bootstrap.BindAsync(endPoint); if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebug($"服务主机启动成功,监听地址:{endPoint}。"); }
public NettyServer( int listenPort, Action<string, string, string, byte[]> dataReceivedAction, Action<string, string> clientConnectedAction) { this.listenPort = listenPort; this.channels = new Dictionary<string, IChannel>(); this.bossGroup = new MultithreadEventLoopGroup(1); this.workerGroup = new MultithreadEventLoopGroup(); this.bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler("SRV-LSTN")) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; //TODO: Receive InstanceId in the pipeline instead of part of the data buffer pipeline.AddLast("framing-enc", new LengthFieldPrepender(2)); pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(32 * 1024, 0, 2, 0, 2)); pipeline.AddLast("main", new NettyServerHandler(this, dataReceivedAction, clientConnectedAction)); })); }
static void Main(string[] args) { var options = new Options(); if (!Parser.Default.ParseArgumentsStrict(args, options)) { //Console.WriteLine(HelpText.AutoBuild(options).ToString()); return; } if (options.CreateDeviceCount > 0) { CreateDevices(options).Wait(); } string[] runnerConfigurationOptions = options.Runners; if (runnerConfigurationOptions.Length % 3 != 0) { Console.WriteLine("Could not parse runner configuration. Make sure runners are described in threes."); } var group = new MultithreadEventLoopGroup(Environment.ProcessorCount); var idProvider = new IdProvider(options.DeviceStartingFrom, options.DeviceNamePattern); var runnerConfigurations = new List<RunnerConfiguration>(); foreach (List<string> runnerOptions in runnerConfigurationOptions.InSetsOf(3)) { string name = runnerOptions[0]; double startFrequency = double.Parse(runnerOptions[1]); int count = int.Parse(runnerOptions[2]); RunnerConfiguration config; switch (name) { case "stable": var stableRunner = new StableTelemetryRunner(group, options.DeviceKey, options.IotHubConnectionString, new IPEndPoint(IPAddress.Parse(options.Address), options.Port), options.HostName); config = stableRunner.CreateConfiguration(count, TimeSpan.FromSeconds(count / startFrequency)); break; case "occasional": var occasionalRunner = new OccasionalTelemetryRunner(group, options.DeviceKey, options.IotHubConnectionString, new IPEndPoint(IPAddress.Parse(options.Address), options.Port), options.HostName); config = occasionalRunner.CreateConfiguration(count, TimeSpan.FromSeconds(count / startFrequency)); break; default: Console.WriteLine("Unknown runner configuration `{0}`", name); return; } runnerConfigurations.Add(config); } var cts = new CancellationTokenSource(); var host = new RunnerHost(idProvider, runnerConfigurations, TimeSpan.FromSeconds(10)); host.Run(cts.Token); Console.ReadLine(); Console.WriteLine("Closing runners"); cts.Cancel(); }
public async void EchoServerAndClient() { var testPromise = new TaskCompletionSource(); var tlsCertificate = new X509Certificate2("dotnetty.com.pfx", "password"); Func<Task> closeServerFunc = await this.StartServerAsync(true, ch => { ch.Pipeline.AddLast("server logger", new LoggingHandler("SERVER")); ch.Pipeline.AddLast("server tls", TlsHandler.Server(tlsCertificate)); ch.Pipeline.AddLast("server logger2", new LoggingHandler("SER***")); ch.Pipeline.AddLast("server prepender", new LengthFieldPrepender(2)); ch.Pipeline.AddLast("server decoder", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2)); ch.Pipeline.AddLast(new EchoChannelHandler()); }, testPromise); var group = new MultithreadEventLoopGroup(); Bootstrap b = new Bootstrap() .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(ch => { string targetHost = tlsCertificate.GetNameInfo(X509NameType.DnsName, false); var clientTlsSettings = new ClientTlsSettings(targetHost); ch.Pipeline.AddLast("client logger", new LoggingHandler("CLIENT")); ch.Pipeline.AddLast("client tls", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), clientTlsSettings)); ch.Pipeline.AddLast("client logger2", new LoggingHandler("CLI***")); ch.Pipeline.AddLast("client prepender", new LengthFieldPrepender(2)); ch.Pipeline.AddLast("client decoder", new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2)); ch.Pipeline.AddLast(new TestScenarioRunner(this.GetEchoClientScenario, testPromise)); })); this.Output.WriteLine("Configured Bootstrap: {0}", b); IChannel clientChannel = null; try { clientChannel = await b.ConnectAsync(IPAddress.Loopback, Port); this.Output.WriteLine("Connected channel: {0}", clientChannel); await Task.WhenAny(testPromise.Task, Task.Delay(TimeSpan.FromSeconds(30))); Assert.True(testPromise.Task.IsCompleted, "timed out"); testPromise.Task.Wait(); } finally { Task serverCloseTask = closeServerFunc(); clientChannel?.CloseAsync().Wait(TimeSpan.FromSeconds(5)); group.ShutdownGracefullyAsync(); if (!serverCloseTask.Wait(ShutdownTimeout)) { this.Output.WriteLine("Didn't stop in time."); } } }
public async void EchoServerAndClient() { var testPromise = new TaskCompletionSource(); var tlsCertificate = new X509Certificate2("dotnetty.com.pfx", "password"); Func<Task> closeServerFunc = await this.StartServerAsync(true, ch => { ch.Pipeline.AddLast(TlsHandler.Server(tlsCertificate)); ch.Pipeline.AddLast(new EchoChannelHandler()); }, testPromise); var group = new MultithreadEventLoopGroup(); Bootstrap b = new Bootstrap() .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(ch => { string targetHost = tlsCertificate.GetNameInfo(X509NameType.DnsName, false); ch.Pipeline.AddLast(TlsHandler.Client(targetHost, null, (sender, certificate, chain, errors) => true)); ch.Pipeline.AddLast(new TestScenarioRunner(this.GetEchoClientScenario, testPromise)); })); this.Output.WriteLine("Configured Bootstrap: {0}", b); IChannel clientChannel = null; try { clientChannel = await b.ConnectAsync(IPAddress.Loopback, Port); this.Output.WriteLine("Connected channel: {0}", clientChannel); await Task.WhenAny(testPromise.Task, Task.Delay(TimeSpan.FromMinutes(1))); Assert.True(testPromise.Task.IsCompleted); testPromise.Task.Wait(); } finally { Task serverCloseTask = closeServerFunc(); if (clientChannel != null) { clientChannel.CloseAsync().Wait(TimeSpan.FromSeconds(5)); } group.ShutdownGracefullyAsync(); if (!serverCloseTask.Wait(ShutdownTimeout)) { this.Output.WriteLine("Didn't stop in time."); } } }
static async Task RunClientAsync() { var eventListener = new ObservableEventListener(); eventListener.LogToConsole(); eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose); var group = new MultithreadEventLoopGroup(); X509Certificate2 cert = null; string targetHost = null; if (EchoClientSettings.IsSsl) { cert = new X509Certificate2("dotnetty.com.pfx", "password"); targetHost = cert.GetNameInfo(X509NameType.DnsName, false); } try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; if (cert != null) { pipeline.AddLast(new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(targetHost))); } pipeline.AddLast(new LengthFieldPrepender(2)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2)); pipeline.AddLast(new EchoClientHandler()); })); IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(EchoClientSettings.Host, EchoClientSettings.Port)); Console.ReadLine(); await bootstrapChannel.CloseAsync(); } finally { group.ShutdownGracefullyAsync().Wait(1000); eventListener.Dispose(); } }
static async Task RunServerAsync() { var eventListener = new ObservableEventListener(); eventListener.LogToConsole(); eventListener.EnableEvents(DefaultEventSource.Log, EventLevel.Verbose); var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); X509Certificate2 tlsCertificate = null; if (EchoServerSettings.IsSsl) { tlsCertificate = new X509Certificate2("dotnetty.com.pfx", "password"); } try { var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .Handler(new LoggingHandler(LogLevel.INFO)) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; if (tlsCertificate != null) { pipeline.AddLast(TlsHandler.Server(tlsCertificate)); } pipeline.AddLast(new LengthFieldPrepender(2)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(ushort.MaxValue, 0, 2, 0, 2)); pipeline.AddLast(new EchoServerHandler()); })); IChannel bootstrapChannel = await bootstrap.BindAsync(EchoServerSettings.Port); Console.ReadLine(); await bootstrapChannel.CloseAsync(); } finally { Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync()); eventListener.Dispose(); } }
static async Task RunClientAsync() { IPAddress host = IPAddress.Parse("192.168.0.10"); int port = 5882; var factory = new ClientSessionFactory(); var group = new MultithreadEventLoopGroup(); try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new LengthFieldPrepender( ByteOrder.LittleEndian, 2, 0, false)); pipeline.AddLast(new LengthFieldBasedFrameDecoder( ByteOrder.LittleEndian, ushort.MaxValue, 0, 2, 0, 2, true)); pipeline.AddLast(new ClientHandler(factory)); })); IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(host, port)); Console.ReadLine(); await bootstrapChannel.CloseAsync(); } finally { group.ShutdownGracefullyAsync().Wait(1000); } }
/// <summary> /// 启动主机。 /// </summary> /// <param name="endPoint">主机终结点。</param> /// <returns>一个任务。</returns> public async Task StartAsync(EndPoint endPoint) { if (_logger.IsEnabled(LogLevel.Debug)) _logger.Debug($"准备启动服务主机,监听地址:{endPoint}。"); var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, 100) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel => { var pipeline = channel.Pipeline; pipeline.AddLast(new LengthFieldPrepender(4)); pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4)); pipeline.AddLast(new ServerHandler(_serializer, _serviceEntryLocate, _logger)); })); _channel = await bootstrap.BindAsync(endPoint); if (_logger.IsEnabled(LogLevel.Debug)) _logger.Debug($"服务主机启动成功,监听地址:{endPoint}。"); }
/// <summary> /// Starts Echo server. /// </summary> /// <returns>function to trigger closure of the server.</returns> async Task<Func<Task>> StartServerAsync(bool tcpNoDelay, Action<IChannel> childHandlerSetupAction, TaskCompletionSource testPromise) { var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); bool started = false; //var tlsCertificate = new X509Certificate2("dotnetty.com.pfx", "password"); try { ServerBootstrap b = new ServerBootstrap() .Group(bossGroup, workerGroup) .Channel<TcpServerSocketChannel>() .Handler(new ExceptionCatchHandler(ex => testPromise.TrySetException(ex))) .ChildHandler(new ActionChannelInitializer<ISocketChannel>(childHandlerSetupAction)) .ChildOption(ChannelOption.TcpNodelay, tcpNoDelay); this.output.WriteLine("Configured ServerBootstrap: {0}", b); IChannel serverChannel = await b.BindAsync(Port); this.output.WriteLine("Bound server channel: {0}", serverChannel); started = true; return async () => { try { await serverChannel.CloseAsync(); } finally { Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync()); } }; } finally { if (!started) { Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync()); } } }
async Task EnsureServerInitializedAsync() { if (this.ServerAddress != null) { return; } int threadCount = Environment.ProcessorCount; var executorGroup = new MultithreadEventLoopGroup(threadCount); var bufAllocator = new PooledByteBufferAllocator(16 * 1024, 10 * 1024 * 1024 / threadCount); // reserve 10 MB for 64 KB buffers 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 authProvider = new SasTokenAuthenticationProvider(); var topicNameRouter = new TopicNameRouter(); DeviceClientFactoryFunc deviceClientFactoryFactory = IotHubDeviceClient.PreparePoolFactory(settings.IotHubConnectionString + ";DeviceId=stub", "a", 1); ServerBootstrap server = new ServerBootstrap() .Group(executorGroup) .Channel<TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, bufAllocator) .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 MqttIotHubAdapter( settings, deviceClientFactoryFactory, sessionStateProvider, authProvider, topicNameRouter, qos2StateProvider), 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; }
public async Task BasicFunctionalityTest() { await this.EnsureServerInitializedAsync(); int protocolGatewayPort = this.ProtocolGatewayPort; string iotHubConnectionString = ConfigurationManager.AppSettings["IoTHubConnectionString"]; IotHubConnectionStringBuilder hubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(iotHubConnectionString); bool deviceNameProvided; this.deviceId = ConfigurationManager.AppSettings["End2End.DeviceName"]; if (!string.IsNullOrEmpty(this.deviceId)) { deviceNameProvided = true; } else { deviceNameProvided = false; this.deviceId = Guid.NewGuid().ToString("N"); } RegistryManager registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString); if (!deviceNameProvided) { await registryManager.AddDeviceAsync(new Device(this.deviceId)); this.ScheduleCleanup(async () => { await registryManager.RemoveDeviceAsync(this.deviceId); await registryManager.CloseAsync(); }); } Device device = await registryManager.GetDeviceAsync(this.deviceId); this.deviceSas = new SharedAccessSignatureBuilder { Key = device.Authentication.SymmetricKey.PrimaryKey, Target = string.Format("{0}/devices/{1}", hubConnectionStringBuilder.HostName, this.deviceId), KeyName = null, TimeToLive = TimeSpan.FromMinutes(20) } .ToSignature(); EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(iotHubConnectionString, "messages/events"); EventHubConsumerGroup ehGroup = eventHubClient.GetDefaultConsumerGroup(); string[] partitionIds = (await eventHubClient.GetRuntimeInformationAsync()).PartitionIds; EventHubReceiver[] receivers = await Task.WhenAll(partitionIds.Select(pid => ehGroup.CreateReceiverAsync(pid.Trim(), DateTime.UtcNow))); ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString); Stopwatch sw = Stopwatch.StartNew(); var testPromise = new TaskCompletionSource(); var group = new MultithreadEventLoopGroup(); string targetHost = this.tlsCertificate.GetNameInfo(X509NameType.DnsName, false); Bootstrap bootstrap = new Bootstrap() .Group(group) .Channel<TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer<ISocketChannel>(ch => ch.Pipeline.AddLast( TlsHandler.Client(targetHost, null, (sender, certificate, chain, errors) => true), MqttEncoder.Instance, new MqttDecoder(false, 256 * 1024), new TestScenarioRunner(cmf => GetClientScenario(cmf, hubConnectionStringBuilder.HostName, this.deviceId, this.deviceSas), testPromise, CommunicationTimeout, CommunicationTimeout), new XUnitLoggingHandler(this.output)))); IChannel clientChannel = await bootstrap.ConnectAsync(this.ServerAddress, protocolGatewayPort); this.ScheduleCleanup(async () => { await clientChannel.CloseAsync(); await group.ShutdownGracefullyAsync(); }); Task testWorkTask = Task.Run(async () => { Tuple<EventData, string>[] ehMessages = await CollectEventHubMessagesAsync(receivers, 2); Tuple<EventData, string> qos0Event = Assert.Single(ehMessages.Where(x => TelemetryQoS0Content.Equals(x.Item2, StringComparison.Ordinal))); //Assert.Equal((string)qos0Event.Item1.Properties["level"], "verbose"); //Assert.Equal((string)qos0Event.Item1.Properties["subject"], "n/a"); Tuple<EventData, string> qos1Event = Assert.Single(ehMessages.Where(x => TelemetryQoS1Content.Equals(x.Item2, StringComparison.Ordinal))); string qosPropertyName = ConfigurationManager.AppSettings["QoSPropertyName"]; var qos0Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS0Content)); qos0Notification.Properties[qosPropertyName] = "0"; qos0Notification.Properties["subTopic"] = "tips"; await serviceClient.SendAsync(this.deviceId, qos0Notification); var qos1Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS1Content)); qos1Notification.Properties["subTopic"] = "firmware-update"; await serviceClient.SendAsync(this.deviceId, qos1Notification); var qos2Notification = new Message(Encoding.UTF8.GetBytes(NotificationQoS2Content)); qos2Notification.Properties[qosPropertyName] = "2"; qos2Notification.Properties["subTopic"] = "critical-alert"; await serviceClient.SendAsync(this.deviceId, qos2Notification); }); Task timeoutTask = Task.Run(async () => { await Task.Delay(TestTimeout); throw new TimeoutException("Test has timed out."); }); await TaskExtensions.WhenSome(new[] { testPromise.Task, testWorkTask, timeoutTask }, 2); this.output.WriteLine(string.Format("Core test completed in {0}", sw.Elapsed)); // making sure that device queue is empty. await Task.Delay(TimeSpan.FromSeconds(3)); DeviceClient deviceClient = DeviceClient.Create( hubConnectionStringBuilder.HostName, new DeviceAuthenticationWithRegistrySymmetricKey(this.deviceId, device.Authentication.SymmetricKey.PrimaryKey)); Client.Message message = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(1)); Assert.True(message == null, string.Format("Message is not null: {0}", message)); }
static async Task RunWebSocketServer() { HttpListenerContext context = await listener.GetContextAsync(); if (!context.Request.IsWebSocketRequest) { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; context.Response.Close(); } HttpListenerWebSocketContext webSocketContext = await context.AcceptWebSocketAsync(WebSocketConstants.SubProtocols.Mqtt, 8 * 1024, TimeSpan.FromMinutes(5)); serverListener = new ReadListeningHandler(); serverWebSocketChannel = new ServerWebSocketChannel(null, webSocketContext.WebSocket, context.Request.RemoteEndPoint); serverWebSocketChannel .Option(ChannelOption.Allocator, UnpooledByteBufferAllocator.Default) .Option(ChannelOption.AutoRead, true) .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator()) .Option(ChannelOption.MessageSizeEstimator, DefaultMessageSizeEstimator.Default); serverWebSocketChannel.Pipeline.AddLast("server logger", new LoggingHandler("SERVER")); serverWebSocketChannel.Pipeline.AddLast( MqttEncoder.Instance, new MqttDecoder(true, 256 * 1024), serverListener); var workerGroup = new MultithreadEventLoopGroup(); await workerGroup.GetNext().RegisterAsync(serverWebSocketChannel); while (true) { if (done) { break; } await Task.Delay(TimeSpan.FromSeconds(2)); } }
public async Task MqttWebSocketClientAndServerScenario() { var websocket = new ClientWebSocket(); websocket.Options.AddSubProtocol(WebSocketConstants.SubProtocols.Mqtt); Uri uri = new Uri("ws://" + IotHubName + ":" + Port + WebSocketConstants.UriSuffix); await websocket.ConnectAsync(uri, CancellationToken.None); var clientReadListener = new ReadListeningHandler(); var clientChannel = new ClientWebSocketChannel(null, websocket); clientChannel .Option(ChannelOption.Allocator, UnpooledByteBufferAllocator.Default) .Option(ChannelOption.AutoRead, true) .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator()) .Option(ChannelOption.MessageSizeEstimator, DefaultMessageSizeEstimator.Default); clientChannel.Pipeline.AddLast( MqttEncoder.Instance, new MqttDecoder(false, 256 * 1024), clientReadListener); var clientWorkerGroup = new MultithreadEventLoopGroup(); await clientWorkerGroup.GetNext().RegisterAsync(clientChannel); await Task.WhenAll(RunMqttClientScenarioAsync(clientChannel, clientReadListener), RunMqttServerScenarioAsync(serverWebSocketChannel, serverListener)); done = true; }