From c10145f743694474489fc254a70ef8a0b73eb2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 26 Mar 2020 12:32:09 +0800 Subject: [PATCH 1/2] Use IConnectionEndPointFeature to get IMqttChannelAdapter.Endpoint in aspnetcore31 --- Source/MQTTnet.AspnetCore/MqttConnectionContext.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs index 2aa9842..8f3f1be 100644 --- a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs +++ b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs @@ -25,7 +25,7 @@ namespace MQTTnet.AspNetCore _input = Connection.Transport.Input; _output = Connection.Transport.Output; } - + _reader = new SpanBasedMqttPacketBodyReader(); } @@ -37,6 +37,13 @@ namespace MQTTnet.AspNetCore { get { +#if NETCOREAPP3_1 + var endpointFeature = Connection.Features.Get(); + if (endpointFeature != null) + { + return endpointFeature.RemoteEndPoint.ToString(); + } +#endif var connection = Http?.HttpContext?.Connection; if (connection == null) { From d49a4384c0e852560f663a5632f6060c2ef1c621 Mon Sep 17 00:00:00 2001 From: JanEggers Date: Tue, 14 Apr 2020 19:16:32 +0200 Subject: [PATCH 2/2] use the RemoteEndpoint property from the ConnectionContext and added test --- .../MqttConnectionContext.cs | 5 +- .../MQTTnet.AspNetCore.Tests.csproj | 8 ++- .../Mockups/ConnectionHandlerMockup.cs | 50 +++++++++++++++++ .../MqttConnectionContextTest.cs | 53 +++++++++++++++++-- 4 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs diff --git a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs index 8f3f1be..bbc1795 100644 --- a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs +++ b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs @@ -38,10 +38,9 @@ namespace MQTTnet.AspNetCore get { #if NETCOREAPP3_1 - var endpointFeature = Connection.Features.Get(); - if (endpointFeature != null) + if (Connection?.RemoteEndPoint != null) { - return endpointFeature.RemoteEndPoint.ToString(); + return Connection.RemoteEndPoint.ToString(); } #endif var connection = Http?.HttpContext?.Connection; diff --git a/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj b/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj index 06b5976..b03bfbd 100644 --- a/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj +++ b/Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + netcoreapp3.1;net461 false @@ -11,6 +11,12 @@ + + + + + + diff --git a/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs b/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs new file mode 100644 index 0000000..9adcebd --- /dev/null +++ b/Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Connections; +using MQTTnet.Adapter; +using MQTTnet.Formatter; +using MQTTnet.Server; +using System; +using System.Threading.Tasks; + +namespace MQTTnet.AspNetCore.Tests.Mockups +{ + public class ConnectionHandlerMockup: IMqttServerAdapter + { + public TaskCompletionSource Context { get; } = new TaskCompletionSource(); + public Func ClientHandler { get; set; } + + public ConnectionHandlerMockup() + { + } + + public async Task OnConnectedAsync(ConnectionContext connection) + { + try + { + var writer = new SpanBasedMqttPacketWriter(); + var formatter = new MqttPacketFormatterAdapter(writer); + var context = new MqttConnectionContext(formatter, connection); + Context.TrySetResult(context); + + await ClientHandler(context); + } + catch (Exception ex) + { + Context.TrySetException(ex); + } + } + + public Task StartAsync(IMqttServerOptions options) + { + return Task.CompletedTask; + } + + public Task StopAsync() + { + return Task.CompletedTask; + } + + public void Dispose() + { + } + } +} diff --git a/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs b/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs index f916779..e76d627 100644 --- a/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs +++ b/Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs @@ -1,13 +1,20 @@ -using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.AspNetCore.Tests.Mockups; -using MQTTnet.Exceptions; -using MQTTnet.Packets; + using System; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Adapter; +using MQTTnet.AspNetCore.Tests.Mockups; +using MQTTnet.Client.Options; +using MQTTnet.Exceptions; using MQTTnet.Formatter; +using MQTTnet.Packets; +using System.Net; namespace MQTTnet.AspNetCore.Tests { @@ -63,5 +70,43 @@ namespace MQTTnet.AspNetCore.Tests var readResult = await pipe.Send.Reader.ReadAsync(); Assert.IsTrue(readResult.Buffer.Length > 20000); } + + private class Startup + { + public void Configure(IApplicationBuilder app) + { + } + } + + [TestMethod] + public async Task TestEndpoint() + { + var mockup = new ConnectionHandlerMockup(); + + + using (var host = new WebHostBuilder() + .UseKestrel(kestrel => kestrel.ListenLocalhost(1883, listener => listener.Use((ctx, next) => mockup.OnConnectedAsync(ctx)))) + .UseStartup() + .ConfigureServices((hostContext, services) => + { + services.AddHostedMqttServer(o => o.WithoutDefaultEndpoint()); + services.AddSingleton(mockup); + }) + .Build()) + using (var client = new MqttFactory().CreateMqttClient()) + { + host.Start(); + await client.ConnectAsync(new MqttClientOptionsBuilder() + .WithTcpServer("localhost") + .Build(), CancellationToken.None); + + var ctx = await mockup.Context.Task; +#if NETCOREAPP3_1 + var ep = IPEndPoint.Parse(ctx.Endpoint); + Assert.IsNotNull(ep); +#endif + Assert.IsNotNull(ctx); + } + } } }