Преглед изворни кода

Refactor trace messages

release/3.x.x
Christian Kratky пре 7 година
родитељ
комит
e0235aedff
18 измењених фајлова са 111 додато и 58 уклоњено
  1. +2
    -8
      Build/MQTTnet.nuspec
  2. +4
    -0
      MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs
  3. +1
    -1
      MQTTnet.Core/Packets/MqttConnAckPacket.cs
  4. +1
    -1
      MQTTnet.Core/Packets/MqttConnectPacket.cs
  5. +4
    -0
      MQTTnet.Core/Packets/MqttDisconnectPacket.cs
  6. +1
    -1
      MQTTnet.Core/Packets/MqttPingReqPacket.cs
  7. +1
    -1
      MQTTnet.Core/Packets/MqttPingRespPacket.cs
  8. +4
    -0
      MQTTnet.Core/Packets/MqttPubAckPacket.cs
  9. +4
    -0
      MQTTnet.Core/Packets/MqttPubCompPacket.cs
  10. +4
    -0
      MQTTnet.Core/Packets/MqttPubRecPacket.cs
  11. +4
    -0
      MQTTnet.Core/Packets/MqttPubRelPacket.cs
  12. +2
    -3
      MQTTnet.Core/Packets/MqttPublishPacket.cs
  13. +1
    -1
      MQTTnet.Core/Packets/MqttSubAckPacket.cs
  14. +1
    -1
      MQTTnet.Core/Packets/MqttSubscribePacket.cs
  15. +5
    -0
      MQTTnet.Core/Packets/MqttUnsubAckPacket.cs
  16. +6
    -0
      MQTTnet.Core/Packets/MqttUnsubscribe.cs
  17. +41
    -36
      MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs
  18. +25
    -5
      Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs

+ 2
- 8
Build/MQTTnet.nuspec Прегледај датотеку

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>MQTTnet</id>
<version>2.5.2-rc1</version>
<version>2.5.2</version>
<authors>Christian Kratky</authors>
<owners>Christian Kratky</owners>
<licenseUrl>https://github.com/chkr1011/MQTTnet/blob/master/LICENSE</licenseUrl>
@@ -10,13 +10,7 @@
<iconUrl>https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description>
<releaseNotes>* [Core] Fixed library reference issues for .NET 4.6 and netstandard 2.0 (Thanks to @JanEggers).
* [Core] Several COM exceptions are now wrapped properly resulting in less warnings in the trace.
* [Core] Removed application message payload from trace to reduce trace size and increase performance.
* [Client] Fixed WebSocket sub protocol negotiation for ASP.NET Core 2 servers (Thanks to @JanEggers).
* [Client] Fixed broken connection after 30 seconds then using WebSocket protocol (Thanks to @ChristianRiedl).
* [Server] Client connections are now closed when the server is stopped (Thanks to @zhudanfei).
* [Server] Published messages from the server are now retained (if set) (Thanks to @ChristianRiedl). BREAKING CHANGE!
<releaseNotes>* [Core] Refactored trace messages.
</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2017</copyright>
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>


+ 4
- 0
MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs Прегледај датотеку

@@ -32,11 +32,15 @@ namespace MQTTnet.Core.Adapter

public async Task ConnectAsync(TimeSpan timeout)
{
_logger.LogInformation("Connecting [Timeout={0}]", timeout);

await ExecuteAndWrapExceptionAsync(() => _channel.ConnectAsync().TimeoutAfter(timeout));
}

public async Task DisconnectAsync(TimeSpan timeout)
{
_logger.LogInformation("Disconnecting [Timeout={0}]", timeout);

await ExecuteAndWrapExceptionAsync(() => _channel.DisconnectAsync().TimeoutAfter(timeout));
}



+ 1
- 1
MQTTnet.Core/Packets/MqttConnAckPacket.cs Прегледај датотеку

@@ -10,7 +10,7 @@ namespace MQTTnet.Core.Packets

public override string ToString()
{
return nameof(MqttConnAckPacket) + ": [ConnectReturnCode=" + ConnectReturnCode + "] [IsSessionPresent=" + IsSessionPresent + "]";
return "ConnAck: [ConnectReturnCode=" + ConnectReturnCode + "] [IsSessionPresent=" + IsSessionPresent + "]";
}
}
}

+ 1
- 1
MQTTnet.Core/Packets/MqttConnectPacket.cs Прегледај датотеку

@@ -20,7 +20,7 @@ namespace MQTTnet.Core.Packets

public override string ToString()
{
return nameof(MqttConnectPacket) + ": [ClientId=" + ClientId + "] [Username=" + Username + "] [Password=" + Password + "] [KeepAlivePeriod=" + KeepAlivePeriod + "] [CleanSession=" + CleanSession + "]";
return "Connect: [ClientId=" + ClientId + "] [Username=" + Username + "] [Password=" + Password + "] [KeepAlivePeriod=" + KeepAlivePeriod + "] [CleanSession=" + CleanSession + "]";
}
}
}

+ 4
- 0
MQTTnet.Core/Packets/MqttDisconnectPacket.cs Прегледај датотеку

@@ -2,5 +2,9 @@
{
public sealed class MqttDisconnectPacket : MqttBasePacket
{
public override string ToString()
{
return "Disconnect";
}
}
}

+ 1
- 1
MQTTnet.Core/Packets/MqttPingReqPacket.cs Прегледај датотеку

@@ -4,7 +4,7 @@
{
public override string ToString()
{
return nameof(MqttPingReqPacket);
return "PingReq";
}
}
}

+ 1
- 1
MQTTnet.Core/Packets/MqttPingRespPacket.cs Прегледај датотеку

@@ -4,7 +4,7 @@
{
public override string ToString()
{
return nameof(MqttPingRespPacket);
return "PingResp";
}
}
}

+ 4
- 0
MQTTnet.Core/Packets/MqttPubAckPacket.cs Прегледај датотеку

@@ -2,5 +2,9 @@
{
public sealed class MqttPubAckPacket : MqttBasePublishPacket
{
public override string ToString()
{
return "PubAck";
}
}
}

+ 4
- 0
MQTTnet.Core/Packets/MqttPubCompPacket.cs Прегледај датотеку

@@ -2,5 +2,9 @@
{
public sealed class MqttPubCompPacket : MqttBasePublishPacket
{
public override string ToString()
{
return "PubComp";
}
}
}

+ 4
- 0
MQTTnet.Core/Packets/MqttPubRecPacket.cs Прегледај датотеку

@@ -2,5 +2,9 @@
{
public sealed class MqttPubRecPacket : MqttBasePublishPacket
{
public override string ToString()
{
return "PubRec";
}
}
}

+ 4
- 0
MQTTnet.Core/Packets/MqttPubRelPacket.cs Прегледај датотеку

@@ -2,5 +2,9 @@
{
public sealed class MqttPubRelPacket : MqttBasePublishPacket
{
public override string ToString()
{
return "PubRel";
}
}
}

+ 2
- 3
MQTTnet.Core/Packets/MqttPublishPacket.cs Прегледај датотеку

@@ -16,9 +16,8 @@ namespace MQTTnet.Core.Packets

public override string ToString()
{
return nameof(MqttPublishPacket) +
": [Topic=" + Topic + "]" +
" [PayloadLength=" + Payload?.Length + "]" +
return "Publish: [Topic=" + Topic + "]" +
" [Payload.Length=" + Payload?.Length + "]" +
" [QoSLevel=" + QualityOfServiceLevel + "]" +
" [Dup=" + Dup + "]" +
" [Retain=" + Retain + "]" +


+ 1
- 1
MQTTnet.Core/Packets/MqttSubAckPacket.cs Прегледај датотеку

@@ -13,7 +13,7 @@ namespace MQTTnet.Core.Packets
public override string ToString()
{
var subscribeReturnCodesText = string.Join(",", SubscribeReturnCodes.Select(f => f.ToString()));
return nameof(MqttSubAckPacket) + ": [PacketIdentifier=" + PacketIdentifier + "] [SubscribeReturnCodes=" + subscribeReturnCodesText + "]";
return "SubAck: [PacketIdentifier=" + PacketIdentifier + "] [SubscribeReturnCodes=" + subscribeReturnCodesText + "]";
}
}
}

+ 1
- 1
MQTTnet.Core/Packets/MqttSubscribePacket.cs Прегледај датотеку

@@ -12,7 +12,7 @@ namespace MQTTnet.Core.Packets
public override string ToString()
{
var topicFiltersText = string.Join(",", TopicFilters.Select(f => f.Topic + "@" + f.QualityOfServiceLevel));
return nameof(MqttSubscribePacket) + ": [PacketIdentifier=" + PacketIdentifier + "] [TopicFilters=" + topicFiltersText + "]";
return "Subscribe: [PacketIdentifier=" + PacketIdentifier + "] [TopicFilters=" + topicFiltersText + "]";
}
}
}

+ 5
- 0
MQTTnet.Core/Packets/MqttUnsubAckPacket.cs Прегледај датотеку

@@ -3,5 +3,10 @@
public sealed class MqttUnsubAckPacket : MqttBasePacket, IMqttPacketWithIdentifier
{
public ushort PacketIdentifier { get; set; }

public override string ToString()
{
return "UnsubAck: [PacketIdentifier=" + PacketIdentifier + "]";
}
}
}

+ 6
- 0
MQTTnet.Core/Packets/MqttUnsubscribe.cs Прегледај датотеку

@@ -7,5 +7,11 @@ namespace MQTTnet.Core.Packets
public ushort PacketIdentifier { get; set; }

public IList<string> TopicFilters { get; set; } = new List<string>();

public override string ToString()
{
var topicFiltersText = string.Join(",", TopicFilters);
return "Subscribe: [PacketIdentifier=" + PacketIdentifier + "] [TopicFilters=" + topicFiltersText + "]";
}
}
}

+ 41
- 36
MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs Прегледај датотеку

@@ -57,42 +57,7 @@ namespace MQTTnet.Core.Server
await _gate.WaitAsync().ConfigureAwait(false);
try
{
var saveIsRequired = false;

if (applicationMessage.Payload?.Any() == false)
{
saveIsRequired = _retainedMessages.Remove(applicationMessage.Topic);
_logger.LogInformation("Client '{0}' cleared retained message for topic '{1}'.", clientId, applicationMessage.Topic);
}
else
{
if (!_retainedMessages.ContainsKey(applicationMessage.Topic))
{
_retainedMessages[applicationMessage.Topic] = applicationMessage;
saveIsRequired = true;
}
else
{
var existingMessage = _retainedMessages[applicationMessage.Topic];
if (existingMessage.QualityOfServiceLevel != applicationMessage.QualityOfServiceLevel || !existingMessage.Payload.SequenceEqual(applicationMessage.Payload ?? new byte[0]))
{
_retainedMessages[applicationMessage.Topic] = applicationMessage;
saveIsRequired = true;
}
}
_logger.LogInformation("Client '{0}' set retained message for topic '{1}'.", clientId, applicationMessage.Topic);
}

if (!saveIsRequired)
{
_logger.LogTrace("Skipped saving retained messages because no changes were detected.");
}

if (saveIsRequired && _options.Storage != null)
{
await _options.Storage.SaveRetainedMessagesAsync(_retainedMessages.Values.ToList());
}
await HandleMessageInternalAsync(clientId, applicationMessage);
}
catch (Exception exception)
{
@@ -137,5 +102,45 @@ namespace MQTTnet.Core.Server

return retainedMessages;
}

private async Task HandleMessageInternalAsync(string clientId, MqttApplicationMessage applicationMessage)
{
var saveIsRequired = false;

if (applicationMessage.Payload?.Any() == false)
{
saveIsRequired = _retainedMessages.Remove(applicationMessage.Topic);
_logger.LogInformation("Client '{0}' cleared retained message for topic '{1}'.", clientId, applicationMessage.Topic);
}
else
{
if (!_retainedMessages.ContainsKey(applicationMessage.Topic))
{
_retainedMessages[applicationMessage.Topic] = applicationMessage;
saveIsRequired = true;
}
else
{
var existingMessage = _retainedMessages[applicationMessage.Topic];
if (existingMessage.QualityOfServiceLevel != applicationMessage.QualityOfServiceLevel || !existingMessage.Payload.SequenceEqual(applicationMessage.Payload ?? new byte[0]))
{
_retainedMessages[applicationMessage.Topic] = applicationMessage;
saveIsRequired = true;
}
}

_logger.LogInformation("Client '{0}' set retained message for topic '{1}'.", clientId, applicationMessage.Topic);
}

if (!saveIsRequired)
{
_logger.LogTrace("Skipped saving retained messages because no changes were detected.");
}

if (saveIsRequired && _options.Storage != null)
{
await _options.Storage.SaveRetainedMessagesAsync(_retainedMessages.Values.ToList());
}
}
}
}

+ 25
- 5
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs Прегледај датотеку

@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Cryptography.Certificates;
@@ -18,6 +19,8 @@ namespace MQTTnet.TestApp.UniversalWindows
{
public sealed partial class MainPage
{
private readonly ConcurrentQueue<MqttNetTraceMessage> _traceMessages = new ConcurrentQueue<MqttNetTraceMessage>();

private IMqttClient _mqttClient;
private IMqttServer _mqttServer;

@@ -30,15 +33,32 @@ namespace MQTTnet.TestApp.UniversalWindows

private async void OnTraceMessagePublished(object sender, MqttNetTraceMessagePublishedEventArgs e)
{
var text = $"[{e.TraceMessage.Timestamp:yyyy-MM-dd HH:mm:ss.fff}] [{e.TraceMessage.Level}] [{e.TraceMessage.Source}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Message}]{Environment.NewLine}";
if (e.TraceMessage.Exception != null)
_traceMessages.Enqueue(e.TraceMessage);
while (_traceMessages.Count > 100)
{
_traceMessages.TryDequeue(out _);
}

var logText = new StringBuilder();
foreach (var traceMessage in _traceMessages)
{
text += $"{e.TraceMessage.Exception}{Environment.NewLine}";
logText.AppendFormat(
"[{0:yyyy-MM-dd HH:mm:ss.fff}] [{1}] [{2}] [{3}] [{4}]{5}", traceMessage.Timestamp,
traceMessage.Level,
traceMessage.Source,
traceMessage.ThreadId,
traceMessage.Message,
Environment.NewLine);

if (traceMessage.Exception != null)
{
logText.AppendLine(traceMessage.Exception.ToString());
}
}
await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
Trace.Text += text;
Trace.Text = logText.ToString();
});
}



Loading…
Откажи
Сачувај