/// <summary> /// Logs a message to the message log /// </summary> /// <param name="msg">The message to log.</param> /// <param name="inbound">Set to true if the message is inbound to the client.</param> private void LogMessage(MqttMessage msg, bool inbound) { if (!disposed) { Log.Info(m => m(String.Format("{0} {1} ]>----<[ {2} ]>----|", inbound ? "<<<<" : ">>>>", DateTime.Now, msg.Header.MessageType))); } }
public void CanIndex() { dynamic msg = new MqttMessage(); msg["anyvalue"] = "something"; Assert.Equal("something", msg["anyvalue"]); }
public void CanAccessDirectly() { dynamic msg = new MqttMessage(); msg.anyvalue = "something"; Assert.Equal("something", msg.anyvalue); }
public void WillAcceptNonStrings() { dynamic msg = new MqttMessage(); msg.anyvalue = 0; Assert.Equal("0", msg.anyvalue); }
public void InvalidNamesThrow() { dynamic msg = new MqttMessage(); Assert.Throws<MissingMemberException>(() => { var test = msg.anyvalue; }); }
/// <summary> /// Logs details of received messages. /// </summary> /// <param name="msg">The message to log.</param> /// <returns>true, always.</returns> private bool MessageLoggerCallback(MqttMessage msg) { LogMessage(msg, true); return true; }
//Dictionary<short, MqttMessage> messages = new Dictionary<short, MqttMessage>(); #region IStorage Members public void StoreMessage(MqttMessage message) { }
private void DisplayMessage(MqttMessage message) { string messageformat = string.Format("{0}\t{1}\t{2}\t{3}\r\n", message.Date, message.Topic, (int)message.QosLevel, message.GetStringMessage()); fulltext = messageformat + ( (fulltext != null && fulltext.Length > 10000) ? fulltext.Substring(0, 1000) : fulltext); this.textBox_messagereceived.Text = fulltext; }
public MqttUnsubAckHandler(MqttSession session, MqttMessage message) : base(session, message) { }
public MqttClientOptionsBuilder WithWillMessage(MqttMessage value) { _options.WillMessage = value; return(this); }
public static object ConvertPayloadToObject(this MqttMessage message) { var str = message.ConvertPayloadToString(); return(JsonConvert.DeserializeObject(str)); }
private async Task RunInternalAsync(MqttConnectPacket connectPacket, IMqttChannelAdapter adapter) { if (connectPacket == null) { throw new ArgumentNullException(nameof(connectPacket)); } if (adapter == null) { throw new ArgumentNullException(nameof(adapter)); } try { if (_cancellationTokenSource != null) { Stop(MqttClientDisconnectType.Clean, true); } adapter.ReadingPacketStarted += OnAdapterReadingPacketStarted; adapter.ReadingPacketCompleted += OnAdapterReadingPacketCompleted; _cancellationTokenSource = new CancellationTokenSource(); //workaround for https://github.com/dotnet/corefx/issues/24430 #pragma warning disable 4014 _cleanupHandle = _cancellationTokenSource.Token.Register(async() => { await TryDisconnectAdapterAsync(adapter).ConfigureAwait(false); TryDisposeAdapter(adapter); }); #pragma warning restore 4014 //end workaround _wasCleanDisconnect = false; _willMessage = connectPacket.WillMessage; _pendingPacketsQueue.Start(adapter, _cancellationTokenSource.Token); _keepAliveMonitor.Start(connectPacket.KeepAlivePeriod, _cancellationTokenSource.Token); _adapterEndpoint = adapter.Endpoint; _adapterProtocolVersion = adapter.PacketSerializer.ProtocolVersion; while (!_cancellationTokenSource.IsCancellationRequested) { var packet = await adapter.ReceivePacketAsync(_options.DefaultCommunicationTimeout, _cancellationTokenSource.Token).ConfigureAwait(false); if (packet != null) { _keepAliveMonitor.PacketReceived(packet); ProcessReceivedPacket(adapter, packet, _cancellationTokenSource.Token); } } } catch (OperationCanceledException) { } catch (Exception exception) { if (exception is MqttCommunicationException) { if (exception is MqttCommunicationClosedGracefullyException) { _logger.Verbose("Client '{0}': Connection closed gracefully.", ClientId); } else { _logger.Warning(exception, "Client '{0}': Communication exception while receiving client packets.", ClientId); } } else { _logger.Error(exception, "Client '{0}': Unhandled exception while receiving client packets.", ClientId); } Stop(MqttClientDisconnectType.NotClean, true); } finally { _adapterEndpoint = null; _adapterProtocolVersion = null; // Uncomment as soon as the workaround above is no longer needed. //await TryDisconnectAdapterAsync(adapter).ConfigureAwait(false); //TryDisposeAdapter(adapter); _cleanupHandle?.Dispose(); _cleanupHandle = null; _cancellationTokenSource?.Cancel(false); _cancellationTokenSource?.Dispose(); _cancellationTokenSource = null; } }
public MqttManagedMessageBuilder WithApplicationMessage(MqttMessage applicationMessage) { _applicationMessage = applicationMessage; return(this); }
public MqttPubCompHandler(MqttSession session, MqttMessage message) : base(session, message) { }
/// <summary> /// Logs a message to the message log /// </summary> /// <param name="msg"></param> public void LogMessage(MqttMessage msg, bool inbound) { if (!disposed) { if (logFileWriter != null) { logFileWriter.WriteLine(String.Format("{0} {1} ]>----<[ {2} ]>----|", inbound ? "<<<<" : ">>>>", DateTime.Now, msg.Header.MessageType)); logFileWriter.WriteLine(msg.ToString()); logFileWriter.Flush(); } } }
public void StoreMessage(MqttMessage message) { }
/// <summary> /// Called whenever a message is sent from the client to the broker. /// </summary> /// <param name="msg">The message that was sent.</param> /// <returns>true; always.</returns> private bool MessageSentCallback(MqttMessage msg) { LogMessage(msg, false); return true; }
private void sendCommandToolStripMenuItem_Click(object sender, EventArgs e) { SendCommand scf = new SendCommand(); if (scf.ShowDialog(this) == DialogResult.OK) { SanSensNetProtocol a = new SanSensNetProtocol(); byte[] buff = a.Encode_SendCommand(scf.CommandId, scf.V1, scf.V2); string publishtopic = ConfigurationManager.AppSettings["publishCommands_topic"]; Console.WriteLine("send commande"); MqttMessage mess = new MqttMessage(publishtopic, buff, 1, false); mqtt.publish(mess); } }
public void OnMessageReceived(string senderClientId, MqttMessage applicationMessage) { ApplicationMessageReceived?.Invoke(this, new MqttMessageReceivedEventArgs(senderClientId, applicationMessage)); }