internal void Server_ApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) { InboundCounter.Increment(); IncomingMessages.Add(e); if (string.IsNullOrEmpty(e.ClientId)) { _logger.LogInformation($"Message: Topic=[{e.ApplicationMessage.Topic }]"); } else { _logger.LogInformation($"Server received {e.ClientId}'s message: Topic=[{e.ApplicationMessage.Topic }],Retain=[{e.ApplicationMessage.Retain}],QualityOfServiceLevel=[{e.ApplicationMessage.QualityOfServiceLevel}]"); if (!lstTopics.ContainsKey(e.ApplicationMessage.Topic)) { lstTopics.Add(e.ApplicationMessage.Topic, 1); Task.Run(() => _serverEx.PublishAsync("$SYS/broker/subscriptions/count", lstTopics.Count.ToString())); } else { lstTopics[e.ApplicationMessage.Topic]++; } if (e.ApplicationMessage.Payload != null) { received += e.ApplicationMessage.Payload.Length; } string topic = e.ApplicationMessage.Topic; var tpary = topic.Split('/', StringSplitOptions.RemoveEmptyEntries); if (tpary.Length >= 3 && tpary[0] == "devices" && Devices.ContainsKey(e.ClientId)) { Device device = JudgeOrCreateNewDevice(tpary, Devices[e.ClientId]); if (device != null) { Dictionary <string, object> keyValues = new Dictionary <string, object>(); if (tpary.Length >= 4) { string keyname = tpary.Length >= 5 ? tpary[4] : tpary[3]; if (tpary[3].ToLower() == "xml") { try { var xml = new System.Xml.XmlDocument(); xml.LoadXml(e.ApplicationMessage.ConvertPayloadToString()); keyValues.Add(keyname, xml); } catch (Exception ex) { _logger.LogWarning(ex, $"xml data error {topic},{ex.Message}"); } } else if (tpary[3].ToLower() == "binary") { keyValues.Add(keyname, e.ApplicationMessage.Payload); } } else { try { keyValues = e.ApplicationMessage.ConvertPayloadToDictionary(); } catch (Exception ex) { _logger.LogWarning(ex, $"ConvertPayloadToDictionary Error {topic},{ex.Message}"); } } if (tpary[2] == "telemetry") { Task.Run(async() => { try { var result = await _dbContext.SaveAsync <TelemetryLatest, TelemetryData>(keyValues, device, DataSide.ClientSide); } catch (Exception ex) { _logger.LogError(ex, $"Can't upload telemetry to device {device.Name}({device.Id}).the payload is {e.ApplicationMessage.ConvertPayloadToString()}"); } }); } else if (tpary[2] == "attributes") { if (tpary.Length > 3 && tpary[3] == "request") { Task.Run(async() => { await RequestAttributes(tpary, e.ApplicationMessage.ConvertPayloadToDictionary(), device); }); } else { Task.Run(async() => { try { var result = await _dbContext.SaveAsync <AttributeLatest, AttributeData>(keyValues, device, DataSide.ClientSide); } catch (Exception ex) { _logger.LogError(ex, $"Can't upload attributes to device {device.Name}({device.Id}).the payload is \"{e.ApplicationMessage.ConvertPayloadToString()}\""); } }); } } } } } }
public void SetUp(BenchmarkContext context) { TaskScheduler.UnobservedTaskException += (sender, args) => Console.WriteLine(args.Exception); this.message = new byte[MessageSize]; for (int i = 0; i < this.message.Length; i++) { this.message[i] = (byte)(i % 2); } this.datagramChannelReads = context.GetCounter(SocketDatagramChannelReads); this.inboundCounter = new InboundCounter(MessageCount, this.datagramChannelReads); this.serverBufferAllocator = new PooledByteBufferAllocator(); this.serverGroup = new MultithreadEventLoopGroup(1); this.serverBootstrap = new Bootstrap(); this.serverBootstrap .Group(this.serverGroup) .Channel <SocketDatagramChannel>() .Option(ChannelOption.Allocator, this.serverBufferAllocator) .Option(ChannelOption.SoBroadcast, true) .Option(ChannelOption.IpMulticastLoopDisabled, false) .Handler(new ActionChannelInitializer <IChannel>(channel => { channel.Pipeline.AddLast(this.inboundCounter); })); Task <IChannel> task = this.serverBootstrap.BindAsync(Localhost, IPEndPoint.MinPort); if (!task.Wait(TimeSpan.FromMilliseconds(DefaultTimeOutInMilliseconds * 5))) { Console.WriteLine("Server start TIMED OUT"); } this.serverChannel = (SocketDatagramChannel)task.Result; var endPoint = (IPEndPoint)this.serverChannel.LocalAddress; this.serverEndPoint = new IPEndPoint(Localhost, endPoint.Port); this.datagramChannelWrites = context.GetCounter(SocketDatagramChannelWrites); this.outboundCounter = new OutboundCounter(this.datagramChannelWrites); this.clientGroup = new MultithreadEventLoopGroup(1); this.clientBufferAllocator = new PooledByteBufferAllocator(); this.clientBootstrap = new Bootstrap(); this.clientBootstrap .Group(this.clientGroup) .Channel <SocketDatagramChannel>() .Option(ChannelOption.Allocator, this.clientBufferAllocator) .Option(ChannelOption.SoBroadcast, true) .Option(ChannelOption.IpMulticastLoopDisabled, false) .Handler(new ActionChannelInitializer <IChannel>(channel => { channel.Pipeline.AddLast(this.outboundCounter); })); this.clientBootstrap.RemoteAddress(this.serverEndPoint); task = (Task <IChannel>) this.clientBootstrap.RegisterAsync(); if (!task.Wait(TimeSpan.FromMilliseconds(DefaultTimeOutInMilliseconds * 5))) { Console.WriteLine("Register client channel TIMED OUT"); } this.clientChannel = task.Result; }