Skip to content

Commit 85e9414

Browse files
committed
Update CloudNative.CloudEvents.Mqtt to MQTTnet version 4.3.6.1152
This involves a new major version of CloudNative.CloudEvents.Mqtt. Signed-off-by: Jon Skeet <jonskeet@google.com>
1 parent b04e929 commit 85e9414

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

RELEASING.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,26 @@ acts as encouragement to use the latest version of the core package.
4141

4242
Within this repository, this is achieved by the following mechanisms:
4343

44-
- Individual csproj files do not specify a version
44+
- Most individual csproj files do not specify a version
4545
- The [Directory.Build.props](src/Directory.Build.props) file has a `<Version>` element
4646
specifying the version of all packages which don't need a separate major
4747

48-
A single GitHub release (and tag) will be created for each beta release, to cover all packages.
48+
A single GitHub release (and tag) will be created for each beta
49+
release, to cover most packages.
4950

5051
- Example tag name: "CloudNative.CloudEvents.All-2.0.0"
5152
- Example release title: "All packages version 2.0.0"
5253

53-
### Exception: new major versions
54+
### Exception: packages with different major versions
5455

55-
Sometimes, we need a new major version of a "satellite" package, typically
56-
to adopt a new major version of a dependency. In this case, the satellite package
57-
will have its own major version, but keep the minor and patch version of everything
58-
else.
56+
For some "satellite" packages, we need a different major version
57+
number, typically to adopt a new major version of a dependency. In
58+
this case, the satellite package will have its own major version,
59+
but keep the minor and patch version of everything else.
60+
61+
For example, in order to take a new version of the `MQTTnet`
62+
dependency, `CloudNative.CloudEvents.Mqtt` 3.8.0 was released with
63+
version 2.8.0 of all other packages.
5964

6065
## New / unstable package versioning
6166

src/CloudNative.CloudEvents.Mqtt/CloudNative.CloudEvents.Mqtt.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
<Description>MQTT extensions for CloudNative.CloudEvents</Description>
66
<PackageTags>cncf;cloudnative;cloudevents;events;mqtt</PackageTags>
77
<LangVersion>8.0</LangVersion>
8+
<Version>3.$(MinorVersion).$(PatchVersion)</Version>
9+
<!-- After the first release of v3, we'll change the major here to 3. -->
10+
<PackageValidationBaselineVersion>2.$(PackageValidationMinor).0</PackageValidationBaselineVersion>
811
<Nullable>enable</Nullable>
912
</PropertyGroup>
1013

1114
<ItemGroup>
12-
<PackageReference Include="MQTTnet" Version="3.0.15" />
15+
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
1316
<ProjectReference Include="..\CloudNative.CloudEvents\CloudNative.CloudEvents.csproj" />
1417
</ItemGroup>
1518

src/CloudNative.CloudEvents.Mqtt/MqttExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Cloud Native Foundation.
1+
// Copyright (c) Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

@@ -39,10 +39,10 @@ public static CloudEvent ToCloudEvent(this MqttApplicationMessage message,
3939
Validation.CheckNotNull(message, nameof(message));
4040

4141
// TODO: Determine if there's a sensible content type we should apply.
42-
return formatter.DecodeStructuredModeMessage(message.Payload, contentType: null, extensionAttributes);
42+
return formatter.DecodeStructuredModeMessage(message.PayloadSegment, contentType: null, extensionAttributes);
4343
}
4444

45-
// TODO: Update to a newer version of MQTTNet and support both binary and structured mode?
45+
// TODO: Support both binary and structured mode.
4646
/// <summary>
4747
/// Converts a CloudEvent to <see cref="MqttApplicationMessage"/>.
4848
/// </summary>
@@ -61,11 +61,11 @@ public static MqttApplicationMessage ToMqttApplicationMessage(this CloudEvent cl
6161
return new MqttApplicationMessage
6262
{
6363
Topic = topic,
64-
Payload = BinaryDataUtilities.AsArray(formatter.EncodeStructuredModeMessage(cloudEvent, out _))
64+
PayloadSegment = BinaryDataUtilities.GetArraySegment(formatter.EncodeStructuredModeMessage(cloudEvent, out _))
6565
};
6666
default:
6767
throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
6868
}
6969
}
7070
}
71-
}
71+
}

src/CloudNative.CloudEvents/Core/BinaryDataUtilities.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,16 @@ public static byte[] AsArray(ReadOnlyMemory<byte> memory)
114114
}
115115

116116
// Note: when this returns, the Array property of the returned segment is guaranteed not to be null.
117-
private static ArraySegment<byte> GetArraySegment(ReadOnlyMemory<byte> memory) =>
117+
118+
/// <summary>
119+
/// Returns the data from <paramref name="memory"/> as a byte array, return the underlying array
120+
/// if there is one, or creating a copy otherwise. This method should be used with care, due to the
121+
/// "sometimes shared, sometimes not" nature of the result. (It is generally safe to use this with the result
122+
/// of encoding a CloudEvent, assuming the same memory is not used elsewhere.)
123+
/// </summary>
124+
/// <param name="memory">The memory to obtain the data from.</param>
125+
/// <returns>The data in <paramref name="memory"/> as an array segment.</returns>
126+
public static ArraySegment<byte> GetArraySegment(ReadOnlyMemory<byte> memory) =>
118127
MemoryMarshal.TryGetArray(memory, out var segment) && segment.Array is not null
119128
? segment
120129
: new ArraySegment<byte>(memory.ToArray());

test/CloudNative.CloudEvents.UnitTests/Mqtt/MqttTest.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// Copyright (c) Cloud Native Foundation.
1+
// Copyright (c) Cloud Native Foundation.
22
// Licensed under the Apache 2.0 license.
33
// See LICENSE file in the project root for full license information.
44

55
using CloudNative.CloudEvents.NewtonsoftJson;
66
using MQTTnet;
77
using MQTTnet.Client;
8-
using MQTTnet.Client.Options;
9-
using MQTTnet.Client.Receiving;
108
using MQTTnet.Server;
119
using System;
1210
using System.Net.Mime;
@@ -18,16 +16,17 @@ namespace CloudNative.CloudEvents.Mqtt.UnitTests
1816
{
1917
public class MqttTest : IDisposable
2018
{
21-
private readonly IMqttServer mqttServer;
19+
private readonly MqttServer mqttServer;
2220

2321
public MqttTest()
2422
{
2523
var optionsBuilder = new MqttServerOptionsBuilder()
2624
.WithConnectionBacklog(100)
25+
.WithDefaultEndpoint()
2726
.WithDefaultEndpointPort(52355);
2827

29-
this.mqttServer = new MqttFactory().CreateMqttServer();
30-
mqttServer.StartAsync(optionsBuilder.Build()).GetAwaiter().GetResult();
28+
this.mqttServer = new MqttFactory().CreateMqttServer(optionsBuilder.Build());
29+
mqttServer.StartAsync().GetAwaiter().GetResult();
3130
}
3231

3332
public void Dispose()
@@ -55,14 +54,17 @@ public async Task MqttSendTest()
5554

5655
var options = new MqttClientOptionsBuilder()
5756
.WithClientId("Client1")
58-
.WithTcpServer("localhost", 52355)
57+
.WithTcpServer("127.0.0.1", 52355)
5958
.WithCleanSession()
6059
.Build();
6160

6261
TaskCompletionSource<CloudEvent> tcs = new TaskCompletionSource<CloudEvent>();
6362
await client.ConnectAsync(options);
64-
client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(
65-
args => tcs.SetResult(args.ApplicationMessage.ToCloudEvent(jsonEventFormatter)));
63+
client.ApplicationMessageReceivedAsync += args =>
64+
{
65+
tcs.SetResult(args.ApplicationMessage.ToCloudEvent(jsonEventFormatter));
66+
return Task.CompletedTask;
67+
};
6668

6769
var result = await client.SubscribeAsync("abc");
6870
await client.PublishAsync(cloudEvent.ToMqttApplicationMessage(ContentMode.Structured, new JsonEventFormatter(), topic: "abc"));
@@ -79,4 +81,4 @@ public async Task MqttSendTest()
7981
Assert.Equal("value", (string?) receivedCloudEvent["comexampleextension1"]);
8082
}
8183
}
82-
}
84+
}

0 commit comments

Comments
 (0)