@@ -1,6 +1,6 @@ | |||||
using System; | using System; | ||||
namespace MQTTnet.Core | |||||
namespace MQTTnet.Core.Client | |||||
{ | { | ||||
public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs | public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs | ||||
{ | { |
@@ -234,13 +234,7 @@ namespace MQTTnet.Core.Client | |||||
_processedPublishPackets.Add(publishPacket.PacketIdentifier); | _processedPublishPackets.Add(publishPacket.PacketIdentifier); | ||||
} | } | ||||
var applicationMessage = new MqttApplicationMessage( | |||||
publishPacket.Topic, | |||||
publishPacket.Payload, | |||||
publishPacket.QualityOfServiceLevel, | |||||
publishPacket.Retain | |||||
); | |||||
var applicationMessage = publishPacket.ToApplicationMessage(); | |||||
ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(applicationMessage)); | ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(applicationMessage)); | ||||
} | } | ||||
@@ -4,6 +4,16 @@ namespace MQTTnet.Core.Internal | |||||
{ | { | ||||
internal static class MqttApplicationMessageExtensions | internal static class MqttApplicationMessageExtensions | ||||
{ | { | ||||
public static MqttApplicationMessage ToApplicationMessage(this MqttPublishPacket publishPacket) | |||||
{ | |||||
return new MqttApplicationMessage( | |||||
publishPacket.Topic, | |||||
publishPacket.Payload, | |||||
publishPacket.QualityOfServiceLevel, | |||||
publishPacket.Retain | |||||
); | |||||
} | |||||
public static MqttPublishPacket ToPublishPacket(this MqttApplicationMessage applicationMessage) | public static MqttPublishPacket ToPublishPacket(this MqttApplicationMessage applicationMessage) | ||||
{ | { | ||||
if (applicationMessage == null) | if (applicationMessage == null) | ||||
@@ -0,0 +1,17 @@ | |||||
using System; | |||||
namespace MQTTnet.Core.Server | |||||
{ | |||||
public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs | |||||
{ | |||||
public MqttApplicationMessageReceivedEventArgs(string clientId, MqttApplicationMessage applicationMessage) | |||||
{ | |||||
ClientId = clientId; | |||||
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage)); | |||||
} | |||||
public string ClientId { get; } | |||||
public MqttApplicationMessage ApplicationMessage { get; } | |||||
} | |||||
} |
@@ -19,20 +19,23 @@ namespace MQTTnet.Core.Server | |||||
private readonly MqttClientMessageQueue _messageQueue; | private readonly MqttClientMessageQueue _messageQueue; | ||||
private readonly Action<MqttClientSession, MqttPublishPacket> _publishPacketReceivedCallback; | private readonly Action<MqttClientSession, MqttPublishPacket> _publishPacketReceivedCallback; | ||||
private readonly MqttServerOptions _options; | private readonly MqttServerOptions _options; | ||||
private CancellationTokenSource _cancellationTokenSource; | private CancellationTokenSource _cancellationTokenSource; | ||||
private IMqttCommunicationAdapter _adapter; | private IMqttCommunicationAdapter _adapter; | ||||
private string _identifier; | private string _identifier; | ||||
private MqttApplicationMessage _willApplicationMessage; | private MqttApplicationMessage _willApplicationMessage; | ||||
public MqttClientSession(MqttServerOptions options, Action<MqttClientSession, MqttPublishPacket> publishPacketReceivedCallback) | |||||
public MqttClientSession(string clientId, MqttServerOptions options, Action<MqttClientSession, MqttPublishPacket> publishPacketReceivedCallback) | |||||
{ | { | ||||
ClientId = clientId; | |||||
_options = options ?? throw new ArgumentNullException(nameof(options)); | _options = options ?? throw new ArgumentNullException(nameof(options)); | ||||
_publishPacketReceivedCallback = publishPacketReceivedCallback ?? throw new ArgumentNullException(nameof(publishPacketReceivedCallback)); | _publishPacketReceivedCallback = publishPacketReceivedCallback ?? throw new ArgumentNullException(nameof(publishPacketReceivedCallback)); | ||||
_messageQueue = new MqttClientMessageQueue(options); | _messageQueue = new MqttClientMessageQueue(options); | ||||
} | } | ||||
public string ClientId { get; } | |||||
public bool IsConnected => _adapter != null; | public bool IsConnected => _adapter != null; | ||||
public async Task RunAsync(string identifier, MqttApplicationMessage willApplicationMessage, IMqttCommunicationAdapter adapter) | public async Task RunAsync(string identifier, MqttApplicationMessage willApplicationMessage, IMqttCommunicationAdapter adapter) | ||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks; | |||||
using MQTTnet.Core.Adapter; | using MQTTnet.Core.Adapter; | ||||
using MQTTnet.Core.Diagnostics; | using MQTTnet.Core.Diagnostics; | ||||
using MQTTnet.Core.Exceptions; | using MQTTnet.Core.Exceptions; | ||||
using MQTTnet.Core.Internal; | |||||
using MQTTnet.Core.Packets; | using MQTTnet.Core.Packets; | ||||
using MQTTnet.Core.Protocol; | using MQTTnet.Core.Protocol; | ||||
@@ -21,6 +22,8 @@ namespace MQTTnet.Core.Server | |||||
_options = options ?? throw new ArgumentNullException(nameof(options)); | _options = options ?? throw new ArgumentNullException(nameof(options)); | ||||
} | } | ||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived; | |||||
public async Task RunClientSessionAsync(MqttClientConnectedEventArgs eventArgs) | public async Task RunClientSessionAsync(MqttClientConnectedEventArgs eventArgs) | ||||
{ | { | ||||
try | try | ||||
@@ -114,7 +117,7 @@ namespace MQTTnet.Core.Server | |||||
{ | { | ||||
isExistingSession = false; | isExistingSession = false; | ||||
clientSession = new MqttClientSession(_options, DispatchPublishPacket); | |||||
clientSession = new MqttClientSession(connectPacket.ClientId, _options, DispatchPublishPacket); | |||||
_clientSessions[connectPacket.ClientId] = clientSession; | _clientSessions[connectPacket.ClientId] = clientSession; | ||||
MqttTrace.Verbose(nameof(MqttClientSessionsManager), $"Created a new session for client '{connectPacket.ClientId}'."); | MqttTrace.Verbose(nameof(MqttClientSessionsManager), $"Created a new session for client '{connectPacket.ClientId}'."); | ||||
@@ -126,6 +129,9 @@ namespace MQTTnet.Core.Server | |||||
private void DispatchPublishPacket(MqttClientSession senderClientSession, MqttPublishPacket publishPacket) | private void DispatchPublishPacket(MqttClientSession senderClientSession, MqttPublishPacket publishPacket) | ||||
{ | { | ||||
var eventArgs = new MqttApplicationMessageReceivedEventArgs(senderClientSession.ClientId, publishPacket.ToApplicationMessage()); | |||||
ApplicationMessageReceived?.Invoke(this, eventArgs); | |||||
foreach (var clientSession in _clientSessions.Values.ToList()) | foreach (var clientSession in _clientSessions.Values.ToList()) | ||||
{ | { | ||||
clientSession.EnqueuePublishPacket(senderClientSession, publishPacket); | clientSession.EnqueuePublishPacket(senderClientSession, publishPacket); | ||||
@@ -21,6 +21,7 @@ namespace MQTTnet.Core.Server | |||||
_adapters = adapters ?? throw new ArgumentNullException(nameof(adapters)); | _adapters = adapters ?? throw new ArgumentNullException(nameof(adapters)); | ||||
_clientSessionsManager = new MqttClientSessionsManager(options); | _clientSessionsManager = new MqttClientSessionsManager(options); | ||||
_clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e); | |||||
} | } | ||||
public IList<string> GetConnectedClients() | public IList<string> GetConnectedClients() | ||||
@@ -30,6 +31,8 @@ namespace MQTTnet.Core.Server | |||||
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected; | public event EventHandler<MqttClientConnectedEventArgs> ClientConnected; | ||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived; | |||||
public void InjectClient(string identifier, IMqttCommunicationAdapter adapter) | public void InjectClient(string identifier, IMqttCommunicationAdapter adapter) | ||||
{ | { | ||||
if (adapter == null) throw new ArgumentNullException(nameof(adapter)); | if (adapter == null) throw new ArgumentNullException(nameof(adapter)); | ||||
@@ -0,0 +1,63 @@ | |||||
<?xml version="1.0"?> | |||||
<package > | |||||
<metadata> | |||||
<id>MQTTnet</id> | |||||
<version>2.1.4.0</version> | |||||
<authors>Christian Kratky</authors> | |||||
<owners>Christian Kratky</owners> | |||||
<licenseUrl>https://github.com/chkr1011/MQTTnet/blob/master/LICENSE</licenseUrl> | |||||
<projectUrl>https://github.com/chkr1011/MQTTnet</projectUrl> | |||||
<iconUrl>https://raw.githubusercontent.com/chkr1011/MQTTnet/master/Images/Logo_128x128.png</iconUrl> | |||||
<requireLicenseAcceptance>false</requireLicenseAcceptance> | |||||
<description>MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description> | |||||
<releaseNotes>* [.NET Standard] Added support for .NET Standard 1.3 (Thanks to 1iveowl) | |||||
* [Server] Added an event to receive every application message</releaseNotes> | |||||
<copyright>Copyright Christian Kratky 2016-2017</copyright> | |||||
<tags>MQTT MQTTClient MQTTServer MQTTBroker Broker</tags> | |||||
<dependencies> | |||||
<group targetFramework="netstandard1.3"> | |||||
<dependency id="System.Net.Security" version="4.3.1" /> | |||||
</group> | |||||
</dependencies> | |||||
</metadata> | |||||
<files> | |||||
<!-- Universal Windows --> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\Release\MQTTnet.Core.dll" target="lib\uap10.0"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\Release\MQTTnet.dll" target="lib\uap10.0"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\Release\MQTTnet.pri" target="lib\uap10.0"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\ARM\Release\MQTTnet.Core.dll" target="lib\uap10.0-arm"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\ARM\Release\MQTTnet.dll" target="lib\uap10.0-arm"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\ARM\Release\MQTTnet.pri" target="lib\uap10.0-arm"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x64\Release\MQTTnet.Core.dll" target="lib\uap10.0-x64"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x64\Release\MQTTnet.dll" target="lib\uap10.0-x64"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x64\Release\MQTTnet.pri" target="lib\uap10.0-x64"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x86\Release\MQTTnet.Core.dll" target="lib\uap10.0-x86"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x86\Release\MQTTnet.dll" target="lib\uap10.0-x86"/> | |||||
<file src=".\Frameworks\MQTTnet.UniversalWindows\bin\x86\Release\MQTTnet.pri" target="lib\uap10.0-x86"/> | |||||
<!-- .NET Framework --> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\Release\MQTTnet.Core.dll" target="lib\net45"/> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\Release\MQTTnet.dll" target="lib\net45"/> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\x86\Release\MQTTnet.Core.dll" target="lib\net45-x86"/> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\x86\Release\MQTTnet.dll" target="lib\net45-x86"/> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\x64\Release\MQTTnet.Core.dll" target="lib\net45-x64"/> | |||||
<file src=".\Frameworks\MQTTnet.NetFramework\bin\x64\Release\MQTTnet.dll" target="lib\net45-x64"/> | |||||
<!-- .NET Core App --> | |||||
<file src=".\Frameworks\MQTTnet.NetCoreApp\bin\Release\netcoreapp1.1\MQTTnet.Core.dll" target="lib\netcoreapp1.1"/> | |||||
<file src=".\Frameworks\MQTTnet.NetCoreApp\bin\Release\netcoreapp1.1\MQTTnet.dll" target="lib\netcoreapp1.1"/> | |||||
<!-- .NET Standard 1.3 --> | |||||
<file src=".\Frameworks\MQTTnet.Netstandard\bin\Release\netstandard1.3\MQTTnet.Core.dll" target="lib\netstandard1.3"/> | |||||
<file src=".\Frameworks\MQTTnet.Netstandard\bin\Release\netstandard1.3\MQTTnet.dll" target="lib\netstandard1.3"/> | |||||
</files> | |||||
</package> |
@@ -17,8 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Core", "MQTTnet.Cor | |||||
EndProject | EndProject | ||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{002203AF-2565-4C0D-95ED-027FDEFE0C35}" | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{002203AF-2565-4C0D-95ED-027FDEFE0C35}" | ||||
ProjectSection(SolutionItems) = preProject | ProjectSection(SolutionItems) = preProject | ||||
Build\build.ps1 = Build\build.ps1 | |||||
Build\MQTTnet.nuspec = Build\MQTTnet.nuspec | |||||
build.ps1 = build.ps1 | |||||
MQTTnet.nuspec = MQTTnet.nuspec | |||||
EndProjectSection | EndProjectSection | ||||
EndProject | EndProject | ||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp.NetFramework", "Tests\MQTTnet.TestApp.NetFramework\MQTTnet.TestApp.NetFramework.csproj", "{D9D74F33-6943-49B2-B765-7BD589082098}" | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp.NetFramework", "Tests\MQTTnet.TestApp.NetFramework\MQTTnet.TestApp.NetFramework.csproj", "{D9D74F33-6943-49B2-B765-7BD589082098}" | ||||
@@ -11,4 +11,4 @@ $msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBui | |||||
Remove-Item .\NuGet -Force -Recurse | Remove-Item .\NuGet -Force -Recurse | ||||
New-Item -ItemType Directory -Force -Path .\NuGet | New-Item -ItemType Directory -Force -Path .\NuGet | ||||
NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version | |||||
.\NuGet.exe pack MQTTnet.nuspec -Verbosity detailed -Symbols -OutputDir "NuGet" -Version $version |