Skip to content

Commit e8ecd16

Browse files
authored
IHiveMQClient: Add Public Events to Interface (#231)
1 parent 93f7ff2 commit e8ecd16

File tree

1 file changed

+205
-14
lines changed

1 file changed

+205
-14
lines changed

Source/HiveMQtt/Client/IHiveMQClient.cs

Lines changed: 205 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace HiveMQtt.Client;
2222
using HiveMQtt.Client.Options;
2323
using HiveMQtt.Client.Results;
2424
using HiveMQtt.MQTT5.Types;
25+
using HiveMQtt.Client.Events;
2526

2627
/// <summary>
2728
/// The HiveMQClient interface.
@@ -35,45 +36,45 @@ public interface IHiveMQClient : IDisposable
3536
/// to store data that is specific to this HiveMQClient.
3637
/// </para>
3738
/// </summary>
38-
Dictionary<string, string> LocalStore { get; }
39+
public Dictionary<string, string> LocalStore { get; }
3940

4041
/// <summary>
4142
/// Gets or sets the options for this client.
4243
/// </summary>
43-
HiveMQClientOptions Options { get; set; }
44+
public HiveMQClientOptions Options { get; set; }
4445

4546
/// <summary>
4647
/// Gets the list of subscriptions for this client.
4748
/// </summary>
48-
List<Subscription> Subscriptions { get; }
49+
public List<Subscription> Subscriptions { get; }
4950

5051
/// <summary>
5152
/// Indicates if the client is currently connected to the MQTT broker.
5253
/// </summary>
5354
/// <returns>True if connected, false otherwise.</returns>
54-
bool IsConnected();
55+
public bool IsConnected();
5556

5657
/// <summary>
5758
/// Asynchronously makes a TCP connection to the remote specified in HiveMQClientOptions and then
5859
/// proceeds to make an MQTT Connect request.
5960
/// </summary>
6061
/// <returns>A ConnectResult class representing the result of the MQTT connect call.</returns>
61-
Task<ConnectResult> ConnectAsync();
62+
public Task<ConnectResult> ConnectAsync();
6263

6364
/// <summary>
6465
/// Asynchronous disconnect from the previously connected MQTT broker.
6566
/// </summary>
6667
/// <param name="options">The options for the MQTT Disconnect call.</param>
6768
/// <returns>A boolean indicating on success or failure.</returns>
68-
Task<bool> DisconnectAsync(DisconnectOptions options);
69+
public Task<bool> DisconnectAsync(DisconnectOptions options);
6970

7071
/// <summary>
7172
/// Publish a message to an MQTT topic.
7273
/// </summary>
7374
/// <param name="message">The <seealso cref="MQTT5PublishMessage"/> for the Publish.</param>
7475
/// <param name="cancellationToken">A <seealso cref="CancellationToken"/> to cancel the operation.</param>
7576
/// <returns>A <seealso cref="PublishResult"/> representing the result of the publish operation.</returns>
76-
Task<PublishResult> PublishAsync(MQTT5PublishMessage message, CancellationToken cancellationToken = default);
77+
public Task<PublishResult> PublishAsync(MQTT5PublishMessage message, CancellationToken cancellationToken = default);
7778

7879
/// <summary>
7980
/// Publish a message to an MQTT topic.
@@ -85,7 +86,7 @@ public interface IHiveMQClient : IDisposable
8586
/// <param name="payload">The string message to publish.</param>
8687
/// <param name="qos">The <seealso cref="QualityOfService"/> to use for the publish.</param>
8788
/// <returns>A <seealso cref="PublishResult"/> representing the result of the publish operation.</returns>
88-
Task<PublishResult> PublishAsync(string topic, string payload, QualityOfService qos = QualityOfService.AtMostOnceDelivery);
89+
public Task<PublishResult> PublishAsync(string topic, string payload, QualityOfService qos = QualityOfService.AtMostOnceDelivery);
8990

9091
/// <summary>
9192
/// Publish a message to an MQTT topic.
@@ -97,7 +98,7 @@ public interface IHiveMQClient : IDisposable
9798
/// <param name="payload">The UTF-8 encoded array of bytes to publish.</param>
9899
/// <param name="qos">The <seealso cref="QualityOfService"/> to use for the publish.</param>
99100
/// <returns>A <seealso cref="PublishResult"/> representing the result of the publish operation.</returns>
100-
Task<PublishResult> PublishAsync(string topic, byte[] payload, QualityOfService qos = QualityOfService.AtMostOnceDelivery);
101+
public Task<PublishResult> PublishAsync(string topic, byte[] payload, QualityOfService qos = QualityOfService.AtMostOnceDelivery);
101102

102103
/// <summary>
103104
/// Subscribe with a single topic filter on the MQTT broker.
@@ -114,7 +115,7 @@ public interface IHiveMQClient : IDisposable
114115
/// <param name="retainAsPublished">A boolean indicating whether Application Messages forwarded using this subscription keep the RETAIN flag they were published with.</param>
115116
/// <param name="retainHandling">A RetainHandling value indicating whether retained messages are sent when the subscription is established.</param>
116117
/// <returns>SubscribeResult reflecting the result of the operation.</returns>
117-
Task<SubscribeResult> SubscribeAsync(string topic, QualityOfService qos = QualityOfService.AtMostOnceDelivery, bool noLocal = false, bool retainAsPublished = false, RetainHandling retainHandling = RetainHandling.SendAtSubscribe);
118+
public Task<SubscribeResult> SubscribeAsync(string topic, QualityOfService qos = QualityOfService.AtMostOnceDelivery, bool noLocal = false, bool retainAsPublished = false, RetainHandling retainHandling = RetainHandling.SendAtSubscribe);
118119

119120
/// <summary>
120121
/// Subscribe with SubscribeOptions on the MQTT broker.
@@ -130,29 +131,219 @@ public interface IHiveMQClient : IDisposable
130131
/// </example>
131132
/// <param name="options">The options for the subscribe request.</param>
132133
/// <returns>SubscribeResult reflecting the result of the operation.</returns>
133-
Task<SubscribeResult> SubscribeAsync(SubscribeOptions options);
134+
public Task<SubscribeResult> SubscribeAsync(SubscribeOptions options);
134135

135136
/// <summary>
136137
/// Unsubscribe from a single topic filter on the MQTT broker.
137138
/// </summary>
138139
/// <exception cref="HiveMQttClientException">Thrown if no subscription is found for the topic.</exception>
139140
/// <param name="topic">The topic filter to unsubscribe from.</param>
140141
/// <returns>UnsubscribeResult reflecting the result of the operation.</returns>
141-
Task<UnsubscribeResult> UnsubscribeAsync(string topic);
142+
public Task<UnsubscribeResult> UnsubscribeAsync(string topic);
142143

143144
/// <summary>
144145
/// Unsubscribe from a single topic filter on the MQTT broker.
145146
/// </summary>
146147
/// <exception cref="HiveMQttClientException">Thrown if no subscription is found for the topic.</exception>
147148
/// <param name="subscription">The subscription from client.Subscriptions to unsubscribe from.</param>
148149
/// <returns>UnsubscribeResult reflecting the result of the operation.</returns>
149-
Task<UnsubscribeResult> UnsubscribeAsync(Subscription subscription);
150+
public Task<UnsubscribeResult> UnsubscribeAsync(Subscription subscription);
150151

151152
/// <summary>
152153
/// Unsubscribe from a single topic filter on the MQTT broker.
153154
/// </summary>
154155
/// <exception cref="HiveMQttClientException">Thrown if no subscription is found for the topic.</exception>
155156
/// <param name="subscriptions">The subscriptions from client.Subscriptions to unsubscribe from.</param>
156157
/// <returns>UnsubscribeResult reflecting the result of the operation.</returns>
157-
Task<UnsubscribeResult> UnsubscribeAsync(List<Subscription> subscriptions);
158+
public Task<UnsubscribeResult> UnsubscribeAsync(List<Subscription> subscriptions);
159+
160+
/// <summary>
161+
/// Event that is fired before the client connects to the broker.
162+
/// </summary>
163+
/// <remarks>
164+
/// This event allows you to modify connection options before the connection attempt is made.
165+
/// </remarks>
166+
public event EventHandler<BeforeConnectEventArgs>? BeforeConnect;
167+
168+
/// <summary>
169+
/// Event that is fired after the client successfully connects to the broker.
170+
/// </summary>
171+
/// <remarks>
172+
/// The event arguments contain the results of the connection attempt.
173+
/// </remarks>
174+
public event EventHandler<AfterConnectEventArgs>? AfterConnect;
175+
176+
/// <summary>
177+
/// Event that is fired before the client disconnects from the broker.
178+
/// </summary>
179+
public event EventHandler<BeforeDisconnectEventArgs>? BeforeDisconnect;
180+
181+
/// <summary>
182+
/// Event that is fired after the client is disconnected from the broker.
183+
/// </summary>
184+
/// <remarks>
185+
/// The event arguments indicate whether the disconnect was clean or not.
186+
/// </remarks>
187+
public event EventHandler<AfterDisconnectEventArgs>? AfterDisconnect;
188+
189+
/// <summary>
190+
/// Event that is fired before the client sends a subscribe request.
191+
/// </summary>
192+
/// <remarks>
193+
/// This event allows you to modify subscription options before the request is sent.
194+
/// </remarks>
195+
public event EventHandler<BeforeSubscribeEventArgs>? BeforeSubscribe;
196+
197+
/// <summary>
198+
/// Event that is fired after the client completes a subscribe request.
199+
/// </summary>
200+
/// <remarks>
201+
/// The event arguments contain the results of the subscription attempt.
202+
/// </remarks>
203+
public event EventHandler<AfterSubscribeEventArgs>? AfterSubscribe;
204+
205+
/// <summary>
206+
/// Event that is fired before the client sends an unsubscribe request.
207+
/// </summary>
208+
public event EventHandler<BeforeUnsubscribeEventArgs>? BeforeUnsubscribe;
209+
210+
/// <summary>
211+
/// Event that is fired after the client completes an unsubscribe request.
212+
/// </summary>
213+
/// <remarks>
214+
/// The event arguments contain the results of the unsubscribe operation.
215+
/// </remarks>
216+
public event EventHandler<AfterUnsubscribeEventArgs>? AfterUnsubscribe;
217+
218+
/// <summary>
219+
/// Event that is fired when a message is received from the broker.
220+
/// </summary>
221+
/// <remarks>
222+
/// This is the main event for receiving published messages from subscribed topics.
223+
/// </remarks>
224+
public event EventHandler<OnMessageReceivedEventArgs>? OnMessageReceived;
225+
226+
/// <summary>
227+
/// Event that is fired after the client sends a CONNECT packet to the broker.
228+
/// </summary>
229+
public event EventHandler<OnConnectSentEventArgs>? OnConnectSent;
230+
231+
/// <summary>
232+
/// Event that is fired after the client receives a CONNACK packet from the broker.
233+
/// </summary>
234+
public event EventHandler<OnConnAckReceivedEventArgs>? OnConnAckReceived;
235+
236+
/// <summary>
237+
/// Event that is fired after the client sends a DISCONNECT packet to the broker.
238+
/// </summary>
239+
public event EventHandler<OnDisconnectSentEventArgs>? OnDisconnectSent;
240+
241+
/// <summary>
242+
/// Event that is fired after the client receives a DISCONNECT packet from the broker.
243+
/// </summary>
244+
public event EventHandler<OnDisconnectReceivedEventArgs>? OnDisconnectReceived;
245+
246+
/// <summary>
247+
/// Event that is fired after the client sends a PINGREQ packet to the broker.
248+
/// </summary>
249+
public event EventHandler<OnPingReqSentEventArgs>? OnPingReqSent;
250+
251+
/// <summary>
252+
/// Event that is fired after the client receives a PINGRESP packet from the broker.
253+
/// </summary>
254+
public event EventHandler<OnPingRespReceivedEventArgs>? OnPingRespReceived;
255+
256+
/// <summary>
257+
/// Event that is fired after the client sends a SUBSCRIBE packet to the broker.
258+
/// </summary>
259+
public event EventHandler<OnSubscribeSentEventArgs>? OnSubscribeSent;
260+
261+
/// <summary>
262+
/// Event that is fired after the client receives a SUBACK packet from the broker.
263+
/// </summary>
264+
public event EventHandler<OnSubAckReceivedEventArgs>? OnSubAckReceived;
265+
266+
/// <summary>
267+
/// Event that is fired after the client sends an UNSUBSCRIBE packet to the broker.
268+
/// </summary>
269+
public event EventHandler<OnUnsubscribeSentEventArgs>? OnUnsubscribeSent;
270+
271+
/// <summary>
272+
/// Event that is fired after the client receives an UNSUBACK packet from the broker.
273+
/// </summary>
274+
public event EventHandler<OnUnsubAckReceivedEventArgs>? OnUnsubAckReceived;
275+
276+
/// <summary>
277+
/// Event that is fired after the client receives a PUBLISH packet from the broker.
278+
/// </summary>
279+
public event EventHandler<OnPublishReceivedEventArgs>? OnPublishReceived;
280+
281+
/// <summary>
282+
/// Event that is fired after the client sends a PUBLISH packet to the broker.
283+
/// </summary>
284+
public event EventHandler<OnPublishSentEventArgs>? OnPublishSent;
285+
286+
/// <summary>
287+
/// Event that is fired after the client receives a PUBACK packet from the broker.
288+
/// </summary>
289+
/// <remarks>
290+
/// This event is part of QoS 1 message delivery protocol.
291+
/// </remarks>
292+
public event EventHandler<OnPubAckReceivedEventArgs>? OnPubAckReceived;
293+
294+
/// <summary>
295+
/// Event that is fired after the client sends a PUBACK packet to the broker.
296+
/// </summary>
297+
/// <remarks>
298+
/// This event is part of QoS 1 message delivery protocol.
299+
/// </remarks>
300+
public event EventHandler<OnPubAckSentEventArgs>? OnPubAckSent;
301+
302+
/// <summary>
303+
/// Event that is fired after the client receives a PUBREC packet from the broker.
304+
/// </summary>
305+
/// <remarks>
306+
/// This event is part of QoS 2 message delivery protocol.
307+
/// </remarks>
308+
public event EventHandler<OnPubRecReceivedEventArgs>? OnPubRecReceived;
309+
310+
/// <summary>
311+
/// Event that is fired after the client sends a PUBREC packet to the broker.
312+
/// </summary>
313+
/// <remarks>
314+
/// This event is part of QoS 2 message delivery protocol.
315+
/// </remarks>
316+
public event EventHandler<OnPubRecSentEventArgs>? OnPubRecSent;
317+
318+
/// <summary>
319+
/// Event that is fired after the client receives a PUBREL packet from the broker.
320+
/// </summary>
321+
/// <remarks>
322+
/// This event is part of QoS 2 message delivery protocol.
323+
/// </remarks>
324+
public event EventHandler<OnPubRelReceivedEventArgs>? OnPubRelReceived;
325+
326+
/// <summary>
327+
/// Event that is fired after the client sends a PUBREL packet to the broker.
328+
/// </summary>
329+
/// <remarks>
330+
/// This event is part of QoS 2 message delivery protocol.
331+
/// </remarks>
332+
public event EventHandler<OnPubRelSentEventArgs>? OnPubRelSent;
333+
334+
/// <summary>
335+
/// Event that is fired after the client receives a PUBCOMP packet from the broker.
336+
/// </summary>
337+
/// <remarks>
338+
/// This event is part of QoS 2 message delivery protocol.
339+
/// </remarks>
340+
public event EventHandler<OnPubCompReceivedEventArgs>? OnPubCompReceived;
341+
342+
/// <summary>
343+
/// Event that is fired after the client sends a PUBCOMP packet to the broker.
344+
/// </summary>
345+
/// <remarks>
346+
/// This event is part of QoS 2 message delivery protocol.
347+
/// </remarks>
348+
public event EventHandler<OnPubCompSentEventArgs>? OnPubCompSent;
158349
}

0 commit comments

Comments
 (0)