Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae84aa4

Browse files
authored
1846 server mqttserverstopasync doesnt indicate correct reason (#1872)
1 parentb431a5d commitae84aa4

File tree

25 files changed

+521
-147
lines changed

25 files changed

+521
-147
lines changed

‎.github/workflows/ReleaseNotes.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
*[Server] Fixed not working_UpdateRetainedMessageAsync_ public api (#1858, thanks to@kimdiego2098).
1+
*[Client] Added support for custom CA chain validation (#1851, thanks to@rido-min).
22
*[Client] Added support for custom CA chain validation (#1851, thanks to@rido-min).
33
*[Client] Fixed handling of unobserved tasks exceptions (#1871).
4-
*[Client] Fixed not specified ReasonCode when using_SendExtendedAuthenticationExchangeDataAsync_ (#1882, thanks to@rido-min).
4+
*[Client] Fixed not specified ReasonCode when using_SendExtendedAuthenticationExchangeDataAsync_ (#1882, thanks to@rido-min).
5+
*[Server] Fixed not working_UpdateRetainedMessageAsync_ public api (#1858, thanks to@kimdiego2098).
6+
*[Server] Added support for custom DISCONNECT packets when stopping the server or disconnect a client (BREAKING CHANGE!,#1846).
7+
*[Server] Added new property to stop the server from accepting new connections even if it is running (#1846).

‎Source/MQTTnet.AspnetCore/MqttHostedServer.cs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
usingSystem;
56
usingSystem.Collections.Generic;
67
usingSystem.Threading;
78
usingSystem.Threading.Tasks;
@@ -14,20 +15,27 @@ namespace MQTTnet.AspNetCore
1415
{
1516
publicsealedclassMqttHostedServer:MqttServer,IHostedService
1617
{
17-
publicMqttHostedServer(MqttServerOptionsoptions,IEnumerable<IMqttServerAdapter>adapters,IMqttNetLoggerlogger)
18-
:base(options,adapters,logger)
18+
readonlyMqttFactory_mqttFactory;
19+
20+
publicMqttHostedServer(MqttFactorymqttFactory,MqttServerOptionsoptions,IEnumerable<IMqttServerAdapter>adapters,IMqttNetLoggerlogger):base(
21+
options,
22+
adapters,
23+
logger)
1924
{
25+
_mqttFactory=mqttFactory??thrownewArgumentNullException(nameof(mqttFactory));
2026
}
2127

22-
publicTaskStartAsync(CancellationTokencancellationToken)
28+
publicasyncTaskStartAsync(CancellationTokencancellationToken)
2329
{
24-
_=StartAsync();
25-
returnTask.CompletedTask;
30+
// The yield makes sure that the hosted service is considered up and running.
31+
awaitTask.Yield();
32+
33+
_=StartAsync();
2634
}
2735

2836
publicTaskStopAsync(CancellationTokencancellationToken)
2937
{
30-
returnStopAsync();
38+
returnStopAsync(_mqttFactory.CreateMqttServerStopOptionsBuilder().Build());
3139
}
3240
}
3341
}

‎Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs‎

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
usingSystem;
66
usingMicrosoft.Extensions.DependencyInjection;
7-
usingMicrosoft.Extensions.Hosting;
7+
usingMicrosoft.Extensions.DependencyInjection.Extensions;
88
usingMQTTnet.Adapter;
99
usingMQTTnet.Diagnostics;
1010
usingMQTTnet.Implementations;
@@ -14,76 +14,102 @@ namespace MQTTnet.AspNetCore
1414
{
1515
publicstaticclassServiceCollectionExtensions
1616
{
17-
publicstaticIServiceCollectionAddMqttServer(thisIServiceCollectionserviceCollection,Action<MqttServerOptionsBuilder>configure=null)
17+
publicstaticIServiceCollectionAddHostedMqttServer(thisIServiceCollectionservices,MqttServerOptionsoptions)
1818
{
19-
if(serviceCollectionisnull)
19+
if(services==null)
2020
{
21-
thrownewArgumentNullException(nameof(serviceCollection));
21+
thrownewArgumentNullException(nameof(services));
2222
}
2323

24-
serviceCollection.AddMqttConnectionHandler();
25-
serviceCollection.AddHostedMqttServer(configure);
26-
27-
returnserviceCollection;
28-
}
29-
30-
publicstaticIServiceCollectionAddHostedMqttServer(thisIServiceCollectionservices,MqttServerOptionsoptions)
31-
{
32-
if(options==null)thrownewArgumentNullException(nameof(options));
24+
if(options==null)
25+
{
26+
thrownewArgumentNullException(nameof(options));
27+
}
3328

3429
services.AddSingleton(options);
35-
3630
services.AddHostedMqttServer();
3731

3832
returnservices;
3933
}
4034

41-
publicstaticIServiceCollectionAddHostedMqttServer(thisIServiceCollectionservices,Action<MqttServerOptionsBuilder>configure=null)
35+
publicstaticIServiceCollectionAddHostedMqttServer(thisIServiceCollectionservices,Action<MqttServerOptionsBuilder>configure)
4236
{
43-
services.AddSingleton(s=>
37+
if(services==null)
4438
{
45-
varserverOptionsBuilder=newMqttServerOptionsBuilder();
46-
configure?.Invoke(serverOptionsBuilder);
47-
returnserverOptionsBuilder.Build();
48-
});
39+
thrownewArgumentNullException(nameof(services));
40+
}
4941

50-
services.AddHostedMqttServer();
42+
if(configure==null)
43+
{
44+
thrownewArgumentNullException(nameof(configure));
45+
}
5146

52-
returnservices;
47+
varserverOptionsBuilder=newMqttServerOptionsBuilder();
48+
configure.Invoke(serverOptionsBuilder);
49+
varoptions=serverOptionsBuilder.Build();
50+
51+
returnAddHostedMqttServer(services,options);
52+
}
53+
54+
publicstaticvoidAddHostedMqttServer(thisIServiceCollectionservices)
55+
{
56+
// The user may have these services already registered.
57+
services.TryAddSingleton<IMqttNetLogger>(MqttNetNullLogger.Instance);
58+
services.TryAddSingleton(newMqttFactory());
59+
60+
services.AddSingleton<MqttHostedServer>();
61+
services.AddHostedService<MqttHostedServer>();
5362
}
5463

5564
publicstaticIServiceCollectionAddHostedMqttServerWithServices(thisIServiceCollectionservices,Action<AspNetMqttServerOptionsBuilder>configure)
5665
{
57-
services.AddSingleton(s=>
66+
if(services==null)
5867
{
59-
varbuilder=newAspNetMqttServerOptionsBuilder(s);
60-
configure(builder);
61-
returnbuilder.Build();
62-
});
68+
thrownewArgumentNullException(nameof(services));
69+
}
70+
71+
services.AddSingleton(
72+
s=>
73+
{
74+
varbuilder=newAspNetMqttServerOptionsBuilder(s);
75+
configure(builder);
76+
returnbuilder.Build();
77+
});
6378

6479
services.AddHostedMqttServer();
6580

6681
returnservices;
6782
}
6883

69-
staticIServiceCollectionAddHostedMqttServer(thisIServiceCollectionservices)
84+
publicstaticIServiceCollectionAddMqttConnectionHandler(thisIServiceCollectionservices)
7085
{
71-
varlogger=newMqttNetEventLogger();
72-
73-
services.AddSingleton<IMqttNetLogger>(logger);
74-
services.AddSingleton<MqttHostedServer>();
75-
services.AddSingleton<IHostedService>(s=>s.GetService<MqttHostedServer>());
76-
services.AddSingleton<MqttServer>(s=>s.GetService<MqttHostedServer>());
86+
services.AddSingleton<MqttConnectionHandler>();
87+
services.AddSingleton<IMqttServerAdapter>(s=>s.GetService<MqttConnectionHandler>());
7788

7889
returnservices;
7990
}
8091

81-
publicstaticIServiceCollectionAddMqttWebSocketServerAdapter(thisIServiceCollectionservices)
92+
publicstaticvoidAddMqttLogger(thisIServiceCollectionservices,IMqttNetLoggerlogger)
8293
{
83-
services.AddSingleton<MqttWebSocketServerAdapter>();
84-
services.AddSingleton<IMqttServerAdapter>(s=>s.GetService<MqttWebSocketServerAdapter>());
94+
if(services==null)
95+
{
96+
thrownewArgumentNullException(nameof(services));
97+
}
8598

86-
returnservices;
99+
services.AddSingleton(logger);
100+
}
101+
102+
publicstaticIServiceCollectionAddMqttServer(thisIServiceCollectionserviceCollection,Action<MqttServerOptionsBuilder>configure=null)
103+
{
104+
if(serviceCollectionisnull)
105+
{
106+
thrownewArgumentNullException(nameof(serviceCollection));
107+
}
108+
109+
serviceCollection.AddMqttConnectionHandler();
110+
serviceCollection.AddHostedMqttServer(configure);
111+
112+
returnserviceCollection;
87113
}
88114

89115
publicstaticIServiceCollectionAddMqttTcpServerAdapter(thisIServiceCollectionservices)
@@ -94,12 +120,12 @@ public static IServiceCollection AddMqttTcpServerAdapter(this IServiceCollection
94120
returnservices;
95121
}
96122

97-
publicstaticIServiceCollectionAddMqttConnectionHandler(thisIServiceCollectionservices)
123+
publicstaticIServiceCollectionAddMqttWebSocketServerAdapter(thisIServiceCollectionservices)
98124
{
99-
services.AddSingleton<MqttConnectionHandler>();
100-
services.AddSingleton<IMqttServerAdapter>(s=>s.GetService<MqttConnectionHandler>());
125+
services.AddSingleton<MqttWebSocketServerAdapter>();
126+
services.AddSingleton<IMqttServerAdapter>(s=>s.GetService<MqttWebSocketServerAdapter>());
101127

102128
returnservices;
103129
}
104130
}
105-
}
131+
}

‎Source/MQTTnet.Tests/Clients/LowLevelMqttClient/LowLevelMqttClient_Tests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
usingMQTTnet.LowLevelClient;
1414
usingMQTTnet.Packets;
1515
usingMQTTnet.Protocol;
16+
usingMQTTnet.Server;
1617

1718
namespaceMQTTnet.Tests.Clients.LowLevelMqttClient
1819
{

‎Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
usingMQTTnet.Internal;
1818
usingMQTTnet.Packets;
1919
usingMQTTnet.Protocol;
20+
usingMQTTnet.Server;
2021
usingMQTTnet.Tests.Mockups;
2122

2223
// ReSharper disable InconsistentNaming

‎Source/MQTTnet.Tests/MQTTv5/Server_Tests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
usingSystem.Threading.Tasks;
1111
usingMQTTnet.Internal;
1212
usingMQTTnet.Protocol;
13+
usingMQTTnet.Server;
1314

1415
namespaceMQTTnet.Tests.MQTTv5
1516
{

‎Source/MQTTnet.Tests/Server/General.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public async Task Intercept_Message()
303303
varserver=awaittestEnvironment.StartServer();
304304
server.InterceptingPublishAsync+= e=>
305305
{
306-
e.ApplicationMessage.Payload=Encoding.ASCII.GetBytes("extended");
306+
e.ApplicationMessage.PayloadSegment=newArraySegment<byte>(Encoding.ASCII.GetBytes("extended"));
307307
returnCompletedTask.Instance;
308308
};
309309

@@ -314,7 +314,7 @@ public async Task Intercept_Message()
314314
varisIntercepted=false;
315315
c2.ApplicationMessageReceivedAsync+= e=>
316316
{
317-
isIntercepted=string.Compare("extended",Encoding.UTF8.GetString(e.ApplicationMessage.Payload),StringComparison.Ordinal)==0;
317+
isIntercepted=string.Compare("extended",Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment.ToArray()),StringComparison.Ordinal)==0;
318318
returnCompletedTask.Instance;
319319
};
320320

@@ -425,7 +425,7 @@ await server.InjectApplicationMessage(
425425
newMqttApplicationMessage
426426
{
427427
Topic="/test/1",
428-
Payload=Encoding.UTF8.GetBytes("true"),
428+
PayloadSegment=newArraySegment<byte>(Encoding.UTF8.GetBytes("true")),
429429
QualityOfServiceLevel=MqttQualityOfServiceLevel.ExactlyOnce
430430
})
431431
{
@@ -780,7 +780,7 @@ public async Task Send_Long_Body()
780780
varclient1=awaittestEnvironment.ConnectClient();
781781
client1.ApplicationMessageReceivedAsync+= e=>
782782
{
783-
receivedBody=e.ApplicationMessage.Payload;
783+
receivedBody=e.ApplicationMessage.PayloadSegment.ToArray();
784784
returnCompletedTask.Instance;
785785
};
786786

‎Source/MQTTnet.Tests/Server/Publishing_Tests.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
usingMQTTnet.Formatter;
99
usingMQTTnet.Internal;
1010
usingMQTTnet.Protocol;
11+
usingMQTTnet.Server;
1112

1213
namespaceMQTTnet.Tests.Server
1314
{

‎Source/MQTTnet.Tests/Server/Session_Tests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public async Task Set_Session_Item()
283283

284284
server.InterceptingPublishAsync+= e=>
285285
{
286-
e.ApplicationMessage.Payload=Encoding.UTF8.GetBytes(e.SessionItems["default_payload"]asstring??string.Empty);
286+
e.ApplicationMessage.PayloadSegment=newArraySegment<byte>(Encoding.UTF8.GetBytes(e.SessionItems["default_payload"]asstring??string.Empty));
287287
returnCompletedTask.Instance;
288288
};
289289

‎Source/MQTTnet.Tests/Server/Tls_Tests.cs‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ await firstClient.PublishAsync(
101101
newMqttApplicationMessage
102102
{
103103
Topic="TestTopic1",
104-
Payload=newbyte[]{1,2,3,4}
104+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
105105
});
106106

107107
awaittestEnvironment.Server.InjectApplicationMessage(
108108
newInjectedMqttApplicationMessage(
109109
newMqttApplicationMessage
110110
{
111111
Topic="TestTopic1",
112-
Payload=newbyte[]{1,2,3,4}
112+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
113113
}));
114114

115115
certificateProvider.CurrentCertificate=CreateCertificate(secondOid);
@@ -137,31 +137,31 @@ await firstClient.PublishAsync(
137137
newMqttApplicationMessage
138138
{
139139
Topic="TestTopic2",
140-
Payload=newbyte[]{1,2,3,4}
140+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
141141
});
142142

143143
awaittestEnvironment.Server.InjectApplicationMessage(
144144
newInjectedMqttApplicationMessage(
145145
newMqttApplicationMessage
146146
{
147147
Topic="TestTopic2",
148-
Payload=newbyte[]{1,2,3,4}
148+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
149149
}));
150150

151151
// Ensure first client still works
152152
awaitfirstClient.PublishAsync(
153153
newMqttApplicationMessage
154154
{
155155
Topic="TestTopic1",
156-
Payload=newbyte[]{1,2,3,4}
156+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
157157
});
158158

159159
awaittestEnvironment.Server.InjectApplicationMessage(
160160
newInjectedMqttApplicationMessage(
161161
newMqttApplicationMessage
162162
{
163163
Topic="TestTopic1",
164-
Payload=newbyte[]{1,2,3,4}
164+
PayloadSegment=newArraySegment<byte>(newbyte[]{1,2,3,4})
165165
}));
166166

167167
awaitTask.Delay(1000);
@@ -178,12 +178,10 @@ static async Task<IMqttClient> ConnectClientAsync(TestEnvironment testEnvironmen
178178
varclientOptionsBuilder=testEnvironment.Factory.CreateClientOptionsBuilder();
179179
clientOptionsBuilder.WithClientId(Guid.NewGuid().ToString())
180180
.WithTcpServer("localhost",8883)
181-
.WithTls(
182-
tls=>
181+
.WithTlsOptions(
182+
o=>
183183
{
184-
tls.UseTls=true;
185-
tls.SslProtocol=SslProtocols.Tls12;
186-
tls.CertificateValidationHandler=certValidator;
184+
o.WithSslProtocols(SslProtocols.Tls12).WithCertificateValidationHandler(certValidator);
187185
});
188186

189187
varclientOptions=clientOptionsBuilder.Build();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp