@@ -92,7 +92,11 @@ async_client::~async_client() { MQTTAsync_destroy(&cli_); }
9292// 'context' should be the address of the async_client object that
9393// registered the callback.
9494
95- // Callback for MQTTAsync_setConnected()
95+ // Callback from the C library for when the client is initially
96+ // connected or reconnected.
97+ //
98+ // Installed via MQTTAsync_setConnected()
99+ //
96100// This is installed with the normal callbacks and with a call to
97101// reconnect() to indicate that it succeeded. It signals the client's
98102// connect token then calls any registered callbacks.
@@ -120,14 +124,23 @@ void async_client::on_connected(void* context, char* cause)
120124 if (connHandler)
121125 connHandler (cause_str);
122126
123- if (que)
124- que->put (connected_event{cause_str});
127+ if (que) {
128+ try {
129+ que->put (connected_event{cause_str});
130+ }
131+ catch (const queue_closed&) {
132+ }
133+ }
125134 }
126135}
127136
128- // Callback for when the connection is lost.
137+ // Callback for when the connection is (unecpectedly) lost. This
138+ // typically happens if the network goes down or if the server drops the
139+ // connection due to a protocol violation.
140+ //
129141// This is called from the MQTTAsync_connectionLost registered via
130142// MQTTAsync_setCallbacks().
143+ //
131144// It calls the registered handlers then, if there's a consumer queue, it
132145// places a null pointer in the queue to alert the consumer to a closed
133146// connection.
@@ -151,8 +164,13 @@ void async_client::on_connection_lost(void* context, char* cause)
151164 if (connLostHandler)
152165 connLostHandler (cause_str);
153166
154- if (que)
155- que->put (connection_lost_event{cause_str});
167+ if (que) {
168+ try {
169+ que->put (connection_lost_event{cause_str});
170+ }
171+ catch (const queue_closed&) {
172+ }
173+ }
156174 }
157175}
158176
@@ -176,8 +194,13 @@ void async_client::on_disconnected(
176194 if (disconnectedHandler)
177195 disconnectedHandler (props, ReasonCode (reasonCode));
178196
179- if (que)
180- que->put (disconnected_event{std::move (props), ReasonCode (reasonCode)});
197+ if (que) {
198+ try {
199+ que->put (disconnected_event{std::move (props), ReasonCode (reasonCode)});
200+ }
201+ catch (const queue_closed&) {
202+ }
203+ }
181204 }
182205}
183206
@@ -208,8 +231,13 @@ int async_client::on_message_arrived(
208231 if (cb)
209232 cb->message_arrived (m);
210233
211- if (que)
212- que->put (m);
234+ if (que) {
235+ try {
236+ que->put (m);
237+ }
238+ catch (const queue_closed&) {
239+ }
240+ }
213241 }
214242
215243 MQTTAsync_freeMessage (&msg);
@@ -854,6 +882,10 @@ token_ptr async_client::unsubscribe(
854882
855883void async_client::start_consuming ()
856884{
885+ // Don't do anything if the consumer queue is already up.
886+ if (que_ && !que_->closed ())
887+ return ;
888+
857889 // Make sure callbacks don't happen while we update the que, etc
858890 disable_callbacks ();
859891
0 commit comments