@@ -78,6 +78,10 @@ namespace mtconnect {
7878 identity.str (" " );
7979 identity << std::hex << digest[0 ] << digest[1 ] << digest[2 ];
8080 m_identity = std::string (" _" ) + (identity.str ()).substr (0 , 10 );
81+
82+ auto ci = GetOption<Seconds>(options, configuration::MqttConnectInterval);
83+ if (ci)
84+ m_connectInterval = *ci;
8185 }
8286
8387 ~MqttClientImpl () { stop (); }
@@ -101,14 +105,21 @@ namespace mtconnect {
101105 }
102106 else if (ec == mqtt::connect_return_code::accepted)
103107 {
104- LOG (info) << " MQTT Connected" ;
105- m_connected = true ;
108+ LOG (info) << " MQTT ConnAck: MQTT Connected" ;
109+
106110 if (m_handler && m_handler->m_connected )
111+ {
107112 m_handler->m_connected (shared_from_this ());
113+ }
114+ else
115+ {
116+ LOG (debug) << " No connect handler, setting connected" ;
117+ m_connected = true ;
118+ }
108119 }
109120 else
110121 {
111- LOG (info) << " MQTT connection failed: " << ec;
122+ LOG (info) << " MQTT ConnAck: MQTT connection failed: " << ec;
112123 reconnect ();
113124 }
114125 return true ;
@@ -236,7 +247,18 @@ namespace mtconnect {
236247 if (m_handler && m_handler->m_connecting )
237248 m_handler->m_connecting (shared_from_this ());
238249
239- derived ().getClient ()->async_connect ();
250+ derived ().getClient ()->async_connect ([this ](mqtt::error_code ec) {
251+ if (ec)
252+ {
253+ LOG (warning) << " MqttClientImpl::connect: cannot connect: " << ec.message ()
254+ << " , will retry" ;
255+ reconnect ();
256+ }
257+ else
258+ {
259+ LOG (info) << " MqttClientImpl::connect: connected" ;
260+ }
261+ });
240262 }
241263
242264 void receive (mqtt::buffer &topic, mqtt::buffer &contents)
@@ -258,23 +280,24 @@ namespace mtconnect {
258280 LOG (info) << " Start reconnect timer" ;
259281
260282 // Set an expiry time relative to now.
261- m_reconnectTimer.expires_after (std::chrono::seconds (5 ));
262-
263- m_reconnectTimer.async_wait ([this ](const boost::system::error_code &error) {
264- if (error != boost::asio::error::operation_aborted)
265- {
266- LOG (info) << " MqttClientImpl::reconnect: reconnect now" ;
283+ m_reconnectTimer.expires_after (m_connectInterval);
267284
268- // Connect
269- derived ().getClient ()->async_connect ([this ](mqtt::error_code ec) {
270- LOG (info) << " MqttClientImpl::reconnect async_connect callback: " << ec.message ();
271- if (ec && ec != boost::asio::error::operation_aborted)
285+ m_reconnectTimer.async_wait (boost::asio::bind_executor (
286+ derived ().getClient ()->get_executor (), [this ](const boost::system::error_code &error) {
287+ if (error != boost::asio::error::operation_aborted)
272288 {
273- reconnect ();
289+ LOG (info) << " MqttClientImpl::reconnect: reconnect now" ;
290+
291+ // Connect
292+ derived ().getClient ()->async_connect ([this ](mqtt::error_code ec) {
293+ LOG (info) << " MqttClientImpl::reconnect async_connect callback: " << ec.message ();
294+ if (ec && ec != boost::asio::error::operation_aborted)
295+ {
296+ reconnect ();
297+ }
298+ });
274299 }
275- });
276- }
277- });
300+ }));
278301 }
279302
280303 protected:
0 commit comments