-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
namespace HiveMQtt.MQTT5.Packets; | ||
|
||
using System.IO; | ||
|
||
using HiveMQtt.Client.Events; | ||
using HiveMQtt.Client.Options; | ||
using HiveMQtt.MQTT5.Types; | ||
|
||
|
@@ -43,6 +43,14 @@ public SubscribePacket(SubscribeOptions options, ushort packetIdentifier, Dictio | |
{ | ||
this.Properties.UserProperties = userProperties; | ||
} | ||
|
||
// Setup the TaskCompletionSource so users can simply call | ||
// | ||
// await SubscribePacket.OnCompleteTCS | ||
// | ||
// to wait for the subscribe transaction to complete. | ||
this.OnComplete += (sender, args) => this.OnCompleteTCS.SetResult(args.SubAckPacket); | ||
|
||
} | ||
Check warning on line 54 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x
Check warning on line 54 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x
Check warning on line 54 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x
|
||
|
||
/// <summary> | ||
|
@@ -53,6 +61,46 @@ public SubscribePacket(SubscribeOptions options, ushort packetIdentifier, Dictio | |
/// <inheritdoc/> | ||
public override ControlPacketType ControlPacketType => ControlPacketType.Subscribe; | ||
|
||
Check warning on line 63 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x
Check warning on line 63 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x
Check warning on line 63 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x
|
||
|
||
/// <summary> | ||
/// Valid for outgoing Subscribe packets. An event that is fired after the the subscribe transaction is complete. | ||
/// </summary> | ||
public event EventHandler<OnSubAckReceivedEventArgs> OnComplete = new((client, e) => { }); | ||
|
||
internal virtual void OnCompleteEventLauncher(SubAckPacket packet) | ||
{ | ||
if (this.OnComplete != null && this.OnComplete.GetInvocationList().Length > 0) | ||
{ | ||
var eventArgs = new OnSubAckReceivedEventArgs(packet); | ||
Logger.Trace("SubscribePacket.OnCompleteEventLauncher"); | ||
_ = Task.Run(() => this.OnComplete?.Invoke(this, eventArgs)).ContinueWith( | ||
t => | ||
{ | ||
if (t.IsFaulted) | ||
{ | ||
if (t.Exception is not null) | ||
{ | ||
Logger.Error("SubscribePacket.OnCompleteEventLauncher exception: " + t.Exception.Message); | ||
foreach (var ex in t.Exception.InnerExceptions) | ||
{ | ||
Logger.Error("SubscribePacket.OnCompleteEventLauncher inner exception: " + ex.Message); | ||
} | ||
} | ||
} | ||
}, | ||
TaskScheduler.Default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the awaitable TaskCompletionSource for the subscribe transaction. | ||
/// <para> | ||
/// Valid for outgoing subscribe packets. A TaskCompletionSource that is set when the subscribe transaction is complete. | ||
/// </para> | ||
/// </summary> | ||
public TaskCompletionSource<SubAckPacket> OnCompleteTCS { get; } = new(); | ||
|
||
Check warning on line 102 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-6.0.x
Check warning on line 102 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-7.0.x
Check warning on line 102 in Source/HiveMQtt/MQTT5/Packets/SubscribePacket.cs GitHub Actions / pipeline-ubuntu-latest-dotnet-8.0.x
|
||
|
||
/// <summary> | ||
/// Encode this packet to be sent on the wire. | ||
/// </summary> | ||
|