瀏覽代碼

Add an event to receive all application messages at server level.

release/3.x.x
Christian Kratky 7 年之前
父節點
當前提交
98d7bc980c
共有 10 個檔案被更改,包括 110 行新增14 行删除
  1. +1
    -1
      MQTTnet.Core/Client/MqttApplicationMessageReceivedEventArgs.cs
  2. +1
    -7
      MQTTnet.Core/Client/MqttClient.cs
  3. +10
    -0
      MQTTnet.Core/Internal/MqttApplicationMessageExtensions.cs
  4. +17
    -0
      MQTTnet.Core/Server/MqttApplicationMessageReceivedEventArgs.cs
  5. +5
    -2
      MQTTnet.Core/Server/MqttClientSession.cs
  6. +7
    -1
      MQTTnet.Core/Server/MqttClientSessionsManager.cs
  7. +3
    -0
      MQTTnet.Core/Server/MqttServer.cs
  8. +63
    -0
      MQTTnet.nuspec
  9. +2
    -2
      MQTTnet.sln
  10. +1
    -1
      build.ps1

MQTTnet.Core/MqttApplicationMessageReceivedEventArgs.cs → MQTTnet.Core/Client/MqttApplicationMessageReceivedEventArgs.cs 查看文件

@@ -1,6 +1,6 @@
using System;

namespace MQTTnet.Core
namespace MQTTnet.Core.Client
{
public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs
{

+ 1
- 7
MQTTnet.Core/Client/MqttClient.cs 查看文件

@@ -234,13 +234,7 @@ namespace MQTTnet.Core.Client
_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));
}



+ 10
- 0
MQTTnet.Core/Internal/MqttApplicationMessageExtensions.cs 查看文件

@@ -4,6 +4,16 @@ namespace MQTTnet.Core.Internal
{
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)
{
if (applicationMessage == null)


+ 17
- 0
MQTTnet.Core/Server/MqttApplicationMessageReceivedEventArgs.cs 查看文件

@@ -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; }
}
}

+ 5
- 2
MQTTnet.Core/Server/MqttClientSession.cs 查看文件

@@ -19,20 +19,23 @@ namespace MQTTnet.Core.Server
private readonly MqttClientMessageQueue _messageQueue;
private readonly Action<MqttClientSession, MqttPublishPacket> _publishPacketReceivedCallback;
private readonly MqttServerOptions _options;
private CancellationTokenSource _cancellationTokenSource;
private IMqttCommunicationAdapter _adapter;
private string _identifier;
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));
_publishPacketReceivedCallback = publishPacketReceivedCallback ?? throw new ArgumentNullException(nameof(publishPacketReceivedCallback));

_messageQueue = new MqttClientMessageQueue(options);
}

public string ClientId { get; }

public bool IsConnected => _adapter != null;

public async Task RunAsync(string identifier, MqttApplicationMessage willApplicationMessage, IMqttCommunicationAdapter adapter)


+ 7
- 1
MQTTnet.Core/Server/MqttClientSessionsManager.cs 查看文件

@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using MQTTnet.Core.Adapter;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Exceptions;
using MQTTnet.Core.Internal;
using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol;

@@ -21,6 +22,8 @@ namespace MQTTnet.Core.Server
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;

public async Task RunClientSessionAsync(MqttClientConnectedEventArgs eventArgs)
{
try
@@ -114,7 +117,7 @@ namespace MQTTnet.Core.Server
{
isExistingSession = false;

clientSession = new MqttClientSession(_options, DispatchPublishPacket);
clientSession = new MqttClientSession(connectPacket.ClientId, _options, DispatchPublishPacket);
_clientSessions[connectPacket.ClientId] = clientSession;

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)
{
var eventArgs = new MqttApplicationMessageReceivedEventArgs(senderClientSession.ClientId, publishPacket.ToApplicationMessage());
ApplicationMessageReceived?.Invoke(this, eventArgs);

foreach (var clientSession in _clientSessions.Values.ToList())
{
clientSession.EnqueuePublishPacket(senderClientSession, publishPacket);


+ 3
- 0
MQTTnet.Core/Server/MqttServer.cs 查看文件

@@ -21,6 +21,7 @@ namespace MQTTnet.Core.Server
_adapters = adapters ?? throw new ArgumentNullException(nameof(adapters));
_clientSessionsManager = new MqttClientSessionsManager(options);
_clientSessionsManager.ApplicationMessageReceived += (s, e) => ApplicationMessageReceived?.Invoke(s, e);
}

public IList<string> GetConnectedClients()
@@ -30,6 +31,8 @@ namespace MQTTnet.Core.Server

public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;

public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;

public void InjectClient(string identifier, IMqttCommunicationAdapter adapter)
{
if (adapter == null) throw new ArgumentNullException(nameof(adapter));


+ 63
- 0
MQTTnet.nuspec 查看文件

@@ -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>

+ 2
- 2
MQTTnet.sln 查看文件

@@ -17,8 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.Core", "MQTTnet.Cor
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{002203AF-2565-4C0D-95ED-027FDEFE0C35}"
ProjectSection(SolutionItems) = preProject
Build\build.ps1 = Build\build.ps1
Build\MQTTnet.nuspec = Build\MQTTnet.nuspec
build.ps1 = build.ps1
MQTTnet.nuspec = MQTTnet.nuspec
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp.NetFramework", "Tests\MQTTnet.TestApp.NetFramework\MQTTnet.TestApp.NetFramework.csproj", "{D9D74F33-6943-49B2-B765-7BD589082098}"


Build/build.ps1 → build.ps1 查看文件

@@ -11,4 +11,4 @@ $msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBui

Remove-Item .\NuGet -Force -Recurse
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

Loading…
取消
儲存