瀏覽代碼

Refactoring

release/3.x.x
Christian Kratky 7 年之前
父節點
當前提交
418f2b2c3f
共有 10 個文件被更改,包括 40 次插入36 次删除
  1. +4
    -1
      MQTTnet.Core/Client/MqttApplicationMessageReceivedEventArgs.cs
  2. +1
    -1
      MQTTnet.Core/Client/MqttClient.cs
  3. +1
    -2
      MQTTnet.Core/Client/MqttClientExtensions.cs
  4. +2
    -4
      MQTTnet.Core/Server/IMqttServer.cs
  5. +0
    -17
      MQTTnet.Core/Server/MqttApplicationMessageReceivedEventArgs.cs
  6. +2
    -1
      MQTTnet.Core/Server/MqttClientSessionsManager.cs
  7. +15
    -4
      MQTTnet.Core/Server/MqttServer.cs
  8. +1
    -1
      MQTTnet.Core/Server/MqttServerOptions.cs
  9. +12
    -4
      Tests/MQTTnet.Core.Tests/MqttServerTests.cs
  10. +2
    -1
      Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs

+ 4
- 1
MQTTnet.Core/Client/MqttApplicationMessageReceivedEventArgs.cs 查看文件

@@ -4,11 +4,14 @@ namespace MQTTnet.Core.Client
{
public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs
{
public MqttApplicationMessageReceivedEventArgs(MqttApplicationMessage applicationMessage)
public MqttApplicationMessageReceivedEventArgs(string clientId, MqttApplicationMessage applicationMessage)
{
ClientId = clientId;
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
}

public string ClientId { get; }

public MqttApplicationMessage ApplicationMessage { get; }
}
}

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

@@ -305,7 +305,7 @@ namespace MQTTnet.Core.Client
try
{
var applicationMessage = publishPacket.ToApplicationMessage();
ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(applicationMessage));
ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(_options.ClientId, applicationMessage));
}
catch (Exception exception)
{


+ 1
- 2
MQTTnet.Core/Client/MqttClientExtensions.cs 查看文件

@@ -2,13 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MQTTnet.Core.Packets;

namespace MQTTnet.Core.Client
{
public static class MqttClientExtensions
{
public static Task PublishAsync(this IMqttClient client, params MqttApplicationMessage[] applicationMessages)
public static Task PublishAsync(this IApplicationMessagePublisher client, params MqttApplicationMessage[] applicationMessages)
{
if (client == null) throw new ArgumentNullException(nameof(client));
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));


+ 2
- 4
MQTTnet.Core/Server/IMqttServer.cs 查看文件

@@ -1,18 +1,16 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MQTTnet.Core.Client;

namespace MQTTnet.Core.Server
{
public interface IMqttServer
public interface IMqttServer : IApplicationMessageReceiver, IApplicationMessagePublisher
{
event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;

event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;

IList<ConnectedMqttClient> GetConnectedClients();
void Publish(MqttApplicationMessage applicationMessage);

Task StartAsync();
Task StopAsync();


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

@@ -1,17 +0,0 @@
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; }
}
}

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

@@ -11,6 +11,7 @@ using MQTTnet.Core.Protocol;
using MQTTnet.Core.Serializer;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MQTTnet.Core.Client;

namespace MQTTnet.Core.Server
{
@@ -28,9 +29,9 @@ namespace MQTTnet.Core.Server
_mqttClientSesssionFactory = mqttClientSesssionFactory ?? throw new ArgumentNullException(nameof(mqttClientSesssionFactory));
}

public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
public event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;

public MqttClientRetainedMessagesManager RetainedMessagesManager { get; }
public MqttServerOptions Options { get; }


+ 15
- 4
MQTTnet.Core/Server/MqttServer.cs 查看文件

@@ -6,6 +6,7 @@ using MQTTnet.Core.Adapter;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Linq;
using MQTTnet.Core.Client;

namespace MQTTnet.Core.Server
{
@@ -45,12 +46,22 @@ namespace MQTTnet.Core.Server
public event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;

public void Publish(MqttApplicationMessage applicationMessage)
public Task PublishAsync(IEnumerable<MqttApplicationMessage> applicationMessages)
{
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));

_options.ApplicationMessageInterceptor?.Invoke(applicationMessage);
_clientSessionsManager.DispatchApplicationMessage(null, applicationMessage);
if (_cancellationTokenSource == null)
{
throw new InvalidOperationException("The server is not started.");
}

foreach (var applicationMessage in applicationMessages)
{
_options.ApplicationMessageInterceptor?.Invoke(applicationMessage);
_clientSessionsManager.DispatchApplicationMessage(null, applicationMessage);
}
return Task.FromResult(0);
}

public async Task StartAsync()


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

@@ -4,7 +4,7 @@ using MQTTnet.Core.Protocol;

namespace MQTTnet.Core.Server
{
public sealed class MqttServerOptions
public class MqttServerOptions
{
public MqttServerDefaultEndpointOptions DefaultEndpointOptions { get; } = new MqttServerDefaultEndpointOptions();



+ 12
- 4
Tests/MQTTnet.Core.Tests/MqttServerTests.cs 查看文件

@@ -122,7 +122,7 @@ namespace MQTTnet.Core.Tests
var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));

s.Publish(message);
s.PublishAsync(message).Wait();
await Task.Delay(500);

await s.StopAsync();
@@ -180,7 +180,13 @@ namespace MQTTnet.Core.Tests
public async Task MqttServer_ClearRetainedMessage()
{
var serverAdapter = new TestMqttServerAdapter();
var s = new MqttFactory().CreateMqttServer();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer() // TODO: Is there maybe an easier way for the library user to set the options?
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory(services).CreateMqttServer();
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -207,11 +213,12 @@ namespace MQTTnet.Core.Tests

var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddMqttServer(options => options.Storage = storage)
.AddLogging()
.AddMqttServer(options => options.Storage = storage) // TODO: Is there maybe an easier way for the library user to set the options?
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();

var s = new MqttFactory().CreateMqttServer();
var s = new MqttFactory(services).CreateMqttServer(); // TODO: Like here?
await s.StartAsync();

var c1 = await serverAdapter.ConnectTestClient(s, "c1");
@@ -246,6 +253,7 @@ namespace MQTTnet.Core.Tests

var serverAdapter = new TestMqttServerAdapter();
var services = new ServiceCollection()
.AddLogging()
.AddMqttServer(options => options.ApplicationMessageInterceptor = Interceptor)
.AddSingleton<IMqttServerAdapter>(serverAdapter)
.BuildServiceProvider();


+ 2
- 1
Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs 查看文件

@@ -8,6 +8,7 @@ using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using MQTTnet.AspnetCore;
using MQTTnet.Core;
using MQTTnet.Core.Client;

namespace MQTTnet.TestApp.AspNetCore2
{
@@ -29,7 +30,7 @@ namespace MQTTnet.TestApp.AspNetCore2

while (true)
{
server.Publish(msg.Build());
server.PublishAsync(msg.Build()).Wait();
await Task.Delay(TimeSpan.FromSeconds(2));
msg.WithPayload("Mqtt is still awesome at " + DateTime.Now);
}


Loading…
取消
儲存