@@ -39,6 +39,20 @@ public PublishPacket(MQTT5PublishMessage message, int packetIdentifier)
39
39
{
40
40
this . PacketIdentifier = ( ushort ) packetIdentifier ;
41
41
this . Message = message ;
42
+
43
+ // Setup the QoS 1 TaskCompletionSource so users can simply call
44
+ //
45
+ // await PublishPacket.OnPublishQoS1CompleteTCS
46
+ //
47
+ // to wait for the QoS 1 publish to complete.
48
+ this . OnPublishQoS1Complete += ( sender , args ) => this . OnPublishQoS1CompleteTCS . SetResult ( args . PubAckPacket ) ;
49
+
50
+ // Setup the QoS 2 TaskCompletionSource so users can simply call
51
+ //
52
+ // await PublishPacket.OnPublishQoS2CompleteTCS
53
+ //
54
+ // to wait for the QoS 2 publish to complete.
55
+ this . OnPublishQoS2Complete += ( sender , args ) => this . OnPublishQoS2CompleteTCS . SetResult ( args . PacketList ) ;
42
56
}
43
57
44
58
/// <summary>
@@ -66,23 +80,61 @@ public PublishPacket(ReadOnlySequence<byte> packetData)
66
80
67
81
internal virtual void OnPublishQoS1CompleteEventLauncher ( PubAckPacket packet )
68
82
{
69
- var eventArgs = new OnPublishQoS1CompleteEventArgs ( packet ) ;
70
- Logger . Trace ( "OnPublishQoS1CompleteEventLauncher" ) ;
71
- this . OnPublishQoS1Complete ? . Invoke ( this , eventArgs ) ;
83
+ if ( this . OnPublishQoS1Complete != null && this . OnPublishQoS1Complete . GetInvocationList ( ) . Length > 0 )
84
+ {
85
+ var eventArgs = new OnPublishQoS1CompleteEventArgs ( packet ) ;
86
+ Logger . Trace ( "OnPublishQoS1CompleteEventLauncher" ) ;
87
+ _ = Task . Run ( ( ) => this . OnPublishQoS1Complete ? . Invoke ( this , eventArgs ) ) . ContinueWith (
88
+ t =>
89
+ {
90
+ if ( t . IsFaulted )
91
+ {
92
+ Logger . Error ( "OnPublishQoS1CompleteEventLauncher exception: " + t . Exception . Message ) ;
93
+ }
94
+ } ,
95
+ TaskScheduler . Default ) ;
96
+ }
72
97
}
73
98
99
+ /// <summary>
100
+ /// Gets the awaitable TaskCompletionSource for the QoS 1 publish transaction.
101
+ /// <para>
102
+ /// Valid for outgoing Publish messages QoS 1. A TaskCompletionSource that is set when the QoS 1 publish transaction is complete.
103
+ /// </para>
104
+ /// </summary>
105
+ public TaskCompletionSource < PubAckPacket > OnPublishQoS1CompleteTCS { get ; } = new ( ) ;
106
+
74
107
/// <summary>
75
108
/// Valid for outgoing Publish messages QoS 2. An event that is fired after the the QoS 2 PubComp is received.
76
109
/// </summary>
77
110
public event EventHandler < OnPublishQoS2CompleteEventArgs > OnPublishQoS2Complete = new ( ( client , e ) => { } ) ;
78
111
79
112
internal virtual void OnPublishQoS2CompleteEventLauncher ( List < ControlPacket > packetList )
80
113
{
81
- var eventArgs = new OnPublishQoS2CompleteEventArgs ( packetList ) ;
82
- Logger . Trace ( "OnPublishQoS2CompleteEventLauncher" ) ;
83
- this . OnPublishQoS2Complete ? . Invoke ( this , eventArgs ) ;
114
+ if ( this . OnPublishQoS2Complete != null && this . OnPublishQoS2Complete . GetInvocationList ( ) . Length > 0 )
115
+ {
116
+ var eventArgs = new OnPublishQoS2CompleteEventArgs ( packetList ) ;
117
+ Logger . Trace ( "OnPublishQoS2CompleteEventLauncher" ) ;
118
+ _ = Task . Run ( ( ) => this . OnPublishQoS2Complete ? . Invoke ( this , eventArgs ) ) . ContinueWith (
119
+ t =>
120
+ {
121
+ if ( t . IsFaulted )
122
+ {
123
+ Logger . Error ( "OnPublishQoS2CompleteEventLauncher exception: " + t . Exception . Message ) ;
124
+ }
125
+ } ,
126
+ TaskScheduler . Default ) ;
127
+ }
84
128
}
85
129
130
+ /// <summary>
131
+ /// Gets the awaitable TaskCompletionSource for the QoS 2 publish transaction.
132
+ /// <para>
133
+ /// Valid for outgoing Publish messages QoS 2. A TaskCompletionSource that is set when the QoS 2 publish transaction is complete.
134
+ /// </para>
135
+ /// </summary>
136
+ public TaskCompletionSource < List < ControlPacket > > OnPublishQoS2CompleteTCS { get ; } = new ( ) ;
137
+
86
138
/// <summary>
87
139
/// Decode the received MQTT Publish packet.
88
140
/// </summary>
0 commit comments