diff --git a/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs b/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs index 429f130ac..23491cfbb 100644 --- a/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs +++ b/Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs @@ -1,11 +1,15 @@ - + +using System; using System.Collections.Generic; using MQTTnet.Packets; namespace MQTTnet.Client.Publishing { - public class MqttClientPublishResult + public sealed class MqttClientPublishResult : IDisposable { + [ThreadStatic] + private static MqttClientPublishResult t_cache; + public ushort? PacketIdentifier { get; set; } /// @@ -28,5 +32,20 @@ public class MqttClientPublishResult /// Hint: MQTT 5 feature only. /// public List UserProperties { get; set; } + + void IDisposable.Dispose() + { + PacketIdentifier = null; + ReasonCode = default; + ReasonString = null; + UserProperties = null; + + t_cache = this; + } + + internal static MqttClientPublishResult GetInstance() + { + return t_cache ?? new MqttClientPublishResult(); + } } } diff --git a/Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs b/Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs index 775dc00cd..216fdcfc2 100644 --- a/Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs +++ b/Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs @@ -1,4 +1,4 @@ -using MQTTnet.Client.Connecting; +using MQTTnet.Client.Connecting; using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Options; using MQTTnet.Client.Publishing; @@ -251,28 +251,28 @@ public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions o public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAckPacket) { - return new MqttClientPublishResult - { - PacketIdentifier = pubAckPacket?.PacketIdentifier, - ReasonCode = MqttClientPublishReasonCode.Success - }; + var mqttClientPublishResult = MqttClientPublishResult.GetInstance(); + mqttClientPublishResult.PacketIdentifier = pubAckPacket?.PacketIdentifier; + mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.Success; + + return mqttClientPublishResult; } public MqttClientPublishResult CreateClientPublishResult(MqttPubRecPacket pubRecPacket, MqttPubCompPacket pubCompPacket) { + var mqttClientPublishResult = MqttClientPublishResult.GetInstance(); + if (pubRecPacket == null || pubCompPacket == null) { - return new MqttClientPublishResult - { - ReasonCode = MqttClientPublishReasonCode.UnspecifiedError - }; + mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError; } - - return new MqttClientPublishResult + else { - PacketIdentifier = pubCompPacket.PacketIdentifier, - ReasonCode = MqttClientPublishReasonCode.Success - }; + mqttClientPublishResult.PacketIdentifier = pubCompPacket.PacketIdentifier; + mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.Success; + } + + return mqttClientPublishResult; } } } diff --git a/Source/MQTTnet/Formatter/V5/MqttV500DataConverter.cs b/Source/MQTTnet/Formatter/V5/MqttV500DataConverter.cs index 5f034c0be..caa5b3284 100644 --- a/Source/MQTTnet/Formatter/V5/MqttV500DataConverter.cs +++ b/Source/MQTTnet/Formatter/V5/MqttV500DataConverter.cs @@ -1,4 +1,4 @@ -using MQTTnet.Client.Connecting; +using MQTTnet.Client.Connecting; using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Options; using MQTTnet.Client.Publishing; @@ -308,12 +308,10 @@ public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions o public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAckPacket) { - var result = new MqttClientPublishResult - { - ReasonCode = MqttClientPublishReasonCode.Success, - ReasonString = pubAckPacket?.Properties?.ReasonString, - UserProperties = pubAckPacket?.Properties?.UserProperties - }; + var result = MqttClientPublishResult.GetInstance(); + result.ReasonCode = MqttClientPublishReasonCode.Success; + result.ReasonString = pubAckPacket?.Properties?.ReasonString; + result.UserProperties = pubAckPacket?.Properties?.UserProperties; if (pubAckPacket != null) { @@ -329,33 +327,29 @@ public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAck public MqttClientPublishResult CreateClientPublishResult(MqttPubRecPacket pubRecPacket, MqttPubCompPacket pubCompPacket) { + var result = MqttClientPublishResult.GetInstance(); + if (pubRecPacket == null || pubCompPacket == null) { - return new MqttClientPublishResult - { - ReasonCode = MqttClientPublishReasonCode.UnspecifiedError - }; + result.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError; + return result; } // The PUBCOMP is the last packet in QoS 2. So we use the results from that instead of PUBREC. if (pubCompPacket.ReasonCode == MqttPubCompReasonCode.PacketIdentifierNotFound) { - return new MqttClientPublishResult - { - PacketIdentifier = pubCompPacket.PacketIdentifier, - ReasonCode = MqttClientPublishReasonCode.UnspecifiedError, - ReasonString = pubCompPacket.Properties?.ReasonString, - UserProperties = pubCompPacket.Properties?.UserProperties - }; + result.PacketIdentifier = pubCompPacket.PacketIdentifier; + result.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError; + result.ReasonString = pubCompPacket.Properties?.ReasonString; + result.UserProperties = pubCompPacket.Properties?.UserProperties; + + return result; } - var result = new MqttClientPublishResult - { - PacketIdentifier = pubCompPacket.PacketIdentifier, - ReasonCode = MqttClientPublishReasonCode.Success, - ReasonString = pubCompPacket.Properties?.ReasonString, - UserProperties = pubCompPacket.Properties?.UserProperties - }; + result.PacketIdentifier = pubCompPacket.PacketIdentifier; + result.ReasonCode = MqttClientPublishReasonCode.Success; + result.ReasonString = pubCompPacket.Properties?.ReasonString; + result.UserProperties = pubCompPacket.Properties?.UserProperties; if (pubRecPacket.ReasonCode.HasValue) { diff --git a/Source/MQTTnet/Server/MqttServer.cs b/Source/MQTTnet/Server/MqttServer.cs index 54827e27e..9b4c8a885 100644 --- a/Source/MQTTnet/Server/MqttServer.cs +++ b/Source/MQTTnet/Server/MqttServer.cs @@ -1,4 +1,4 @@ -using MQTTnet.Adapter; +using MQTTnet.Adapter; using MQTTnet.Client.Publishing; using MQTTnet.Client.Receiving; using MQTTnet.Diagnostics; @@ -19,6 +19,8 @@ namespace MQTTnet.Server { public class MqttServer : Disposable, IMqttServer { + static readonly Task s_completedClientPublishResult = Task.FromResult(new MqttClientPublishResult()); + readonly MqttServerEventDispatcher _eventDispatcher; readonly ICollection _adapters; readonly IMqttNetLogger _rootLogger; @@ -150,7 +152,7 @@ public Task PublishAsync(MqttApplicationMessage applica _clientSessionsManager.DispatchApplicationMessage(applicationMessage, null); - return Task.FromResult(new MqttClientPublishResult()); + return s_completedClientPublishResult; } public async Task StartAsync(IMqttServerOptions options)