mqttclient.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <errno.h>
  2. #include <unistd.h>
  3. #include "mqttclient.h"
  4. using namespace mosqpp;
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. template <typename T>
  8. static T _min(T v1, T v2)
  9. {
  10. return v1 < v2 ? v1 : v2;
  11. }
  12. /////////////////////////////////////////////////////////////////////////////
  13. //
  14. #define MQTTCL_DEFAULT_LOOP_TIMEOUT 30
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. CMqttClient::CMqttClient(const char *id) : mosquittopp(id),
  18. m_pEvtCallback(NULL),
  19. m_evtMask(0),
  20. m_pUserParam(NULL)
  21. {
  22. }
  23. CMqttClient::~CMqttClient(void)
  24. {
  25. }
  26. /////////////////////////////////////////////////////////////////////////////
  27. //
  28. int CMqttClient::Init(void)
  29. {
  30. return lib_init();
  31. }
  32. int CMqttClient::Cleanup(void)
  33. {
  34. return lib_cleanup();
  35. }
  36. /////////////////////////////////////////////////////////////////////////////
  37. //
  38. int CMqttClient::publish(CMqttMessage *pMsg)
  39. {
  40. return mosquittopp::publish(&pMsg->mid, pMsg->topic, pMsg->payloadlen, pMsg->payload, pMsg->qos, pMsg->retain);
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. //
  44. CMqttMessage* CMqttClient::PopRcvMsg(void)
  45. {
  46. return m_rcvMsg.Pop();
  47. }
  48. void CMqttClient::PushRcvMsg(CMqttMessage *pMsg)
  49. {
  50. m_rcvMsg.Push(pMsg);
  51. }
  52. CMqttMessage* CMqttClient::PopSndMsg(void)
  53. {
  54. return m_sndMsg.Pop();
  55. }
  56. void CMqttClient::PushSndMsg(CMqttMessage *pMsg)
  57. {
  58. m_sndMsg.Push(pMsg);
  59. }
  60. int CMqttClient::timed_loop(pc_time64_t nLoopTime)
  61. {
  62. int nRet, nLoopTimeout;
  63. pc_time64_t nRemaining = nLoopTime;
  64. m_pc.ClockTrigger();
  65. do
  66. {
  67. nLoopTimeout = _min(MQTTCL_DEFAULT_LOOP_TIMEOUT, (int)(nRemaining / _PC_NS_PER_MS));
  68. if((nRet = loop(nLoopTimeout)) != MOSQ_ERR_SUCCESS)
  69. break;
  70. if((nRemaining = nLoopTime - m_pc.ClockGetElapsed()) > (10 * _PC_NS_PER_MS))
  71. {
  72. m_pc.MSleep(10);
  73. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  74. }
  75. }
  76. while(nRemaining > (MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS));
  77. return nRet;
  78. }
  79. /*
  80. MOSQ_ERR_CONN_PENDING = -1,
  81. MOSQ_ERR_SUCCESS = 0,
  82. MOSQ_ERR_NOMEM = 1,
  83. MOSQ_ERR_PROTOCOL = 2,
  84. MOSQ_ERR_INVAL = 3,
  85. MOSQ_ERR_NO_CONN = 4,
  86. MOSQ_ERR_CONN_REFUSED = 5,
  87. MOSQ_ERR_NOT_FOUND = 6,
  88. MOSQ_ERR_CONN_LOST = 7,
  89. MOSQ_ERR_TLS = 8,
  90. MOSQ_ERR_PAYLOAD_SIZE = 9,
  91. MOSQ_ERR_NOT_SUPPORTED = 10,
  92. MOSQ_ERR_AUTH = 11,
  93. MOSQ_ERR_ACL_DENIED = 12,
  94. MOSQ_ERR_UNKNOWN = 13,
  95. MOSQ_ERR_ERRNO = 14,
  96. MOSQ_ERR_EAI = 15,
  97. MOSQ_ERR_PROXY = 16
  98. */
  99. bool CMqttClient::EvalError(int nErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr)
  100. {
  101. bool bRet;
  102. rbReconnect = rbConnPending = false;
  103. if(rbIntr)
  104. return false;
  105. switch(nErr)
  106. {
  107. case MOSQ_ERR_SUCCESS: // no error
  108. rbReconnect = false;
  109. rbConnPending = false;
  110. bRet = true;
  111. break;
  112. case MOSQ_ERR_CONN_PENDING: // just wait
  113. rbReconnect = false;
  114. rbConnPending = true;
  115. bRet = false;
  116. break;
  117. case MOSQ_ERR_NO_CONN: // (maybe) recoverable errors
  118. case MOSQ_ERR_CONN_LOST:
  119. rbReconnect = true;
  120. rbConnPending = false;
  121. bRet = false;
  122. break;
  123. case MOSQ_ERR_ERRNO: // system error
  124. switch(errno)
  125. {
  126. case ECONNREFUSED:
  127. case ENETUNREACH:
  128. case ENETRESET:
  129. case ENETDOWN:
  130. case ETIMEDOUT:
  131. case ECONNRESET:
  132. case ECONNABORTED:
  133. case ENOTCONN:
  134. case ESHUTDOWN:
  135. case EHOSTDOWN:
  136. case EHOSTUNREACH:
  137. rbReconnect = true;
  138. rbConnPending = false;
  139. bRet = false;
  140. break;
  141. case EISCONN:
  142. case EALREADY:
  143. case EINPROGRESS:
  144. rbReconnect = false;
  145. rbConnPending = true;
  146. bRet = false;
  147. break;
  148. case EINTR:
  149. rbIntr = true;
  150. // fall through
  151. default:
  152. TRACE("errno: %d\n", errno);
  153. rbReconnect = false;
  154. rbConnPending = false;
  155. bRet = false;
  156. break;
  157. }
  158. break;
  159. default: // unrecoverable errors
  160. rbReconnect = false;
  161. rbConnPending = false;
  162. bRet = false;
  163. break;
  164. }
  165. if(!bRet)
  166. {
  167. const char *pszErr = ::mosquitto_strerror(nErr);
  168. strErr = pszErr;
  169. }
  170. return bRet;
  171. }
  172. bool CMqttClient::TimedLoop(pc_time64_t nLoopTime /* in nano seconds */, int &rnErr, bool &rbReconnect, bool &rbConnPending, bool &rbIntr, std::string &strErr)
  173. {
  174. bool bRet;
  175. int nLoopTimeout;
  176. pc_time64_t nRemaining = nLoopTime;
  177. m_pc.ClockTrigger();
  178. do
  179. {
  180. nLoopTimeout = (int)_min((pc_time64_t)(MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS), nRemaining);
  181. rnErr = loop(nLoopTimeout / _PC_NS_PER_MS);
  182. if(rbIntr)
  183. {
  184. rbReconnect = false;
  185. rbConnPending = false;
  186. bRet = false;
  187. rnErr = MOSQ_ERR_ERRNO;
  188. break;
  189. }
  190. bRet = EvalError(rnErr, rbReconnect, rbConnPending, rbIntr, strErr);
  191. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  192. if(bRet && !rbReconnect && (nRemaining > (10 * _PC_NS_PER_MS)))
  193. {
  194. if(usleep(10 * _PC_US_PER_MS) < 0)
  195. {
  196. if(errno == EINTR)
  197. {
  198. rbReconnect = false;
  199. rbConnPending = false;
  200. bRet = false;
  201. rnErr = MOSQ_ERR_ERRNO;
  202. rbIntr = true;
  203. break;
  204. }
  205. }
  206. nRemaining = nLoopTime - m_pc.ClockGetElapsed();
  207. }
  208. else
  209. {
  210. break;
  211. }
  212. }
  213. while(nRemaining >= (MQTTCL_DEFAULT_LOOP_TIMEOUT * _PC_NS_PER_MS));
  214. return bRet;
  215. }
  216. /////////////////////////////////////////////////////////////////////////////
  217. //
  218. void CMqttClient::on_connect(int rc)
  219. {
  220. if(m_pEvtCallback && m_evtMask & NEVT_Connect)
  221. {
  222. MQTT_GENERIC_NOTIFICATION ntf;
  223. ntf.evt = NEVT_Connect;
  224. ntf.con.rc = rc;
  225. (*m_pEvtCallback)(&ntf, m_pUserParam);
  226. }
  227. }
  228. void CMqttClient::on_connect_with_flags(int rc, int flags)
  229. {
  230. if(m_pEvtCallback && m_evtMask & NEVT_Connect_With_Flags)
  231. {
  232. MQTT_GENERIC_NOTIFICATION ntf;
  233. ntf.evt = NEVT_Connect_With_Flags;
  234. ntf.con.rc = rc;
  235. ntf.con.flags = flags;
  236. (*m_pEvtCallback)(&ntf, m_pUserParam);
  237. }
  238. }
  239. void CMqttClient::on_disconnect(int rc)
  240. {
  241. if(m_pEvtCallback && m_evtMask & NEVT_Disconnect)
  242. {
  243. MQTT_GENERIC_NOTIFICATION ntf;
  244. ntf.evt = NEVT_Disconnect;
  245. ntf.con.rc = rc;
  246. (*m_pEvtCallback)(&ntf, m_pUserParam);
  247. }
  248. }
  249. void CMqttClient::on_subscribe(int mid, int qos_count, const int *granted_qos)
  250. {
  251. if(m_pEvtCallback && m_evtMask & NEVT_Subscribe)
  252. {
  253. MQTT_GENERIC_NOTIFICATION ntf;
  254. ntf.evt = NEVT_Subscribe;
  255. ntf.sub.mid = mid;
  256. ntf.sub.qos_count = qos_count;
  257. ntf.sub.granted_qos = granted_qos;
  258. (*m_pEvtCallback)(&ntf, m_pUserParam);
  259. }
  260. }
  261. void CMqttClient::on_unsubscribe(int mid)
  262. {
  263. if(m_pEvtCallback && m_evtMask & NEVT_Unsubscribe)
  264. {
  265. MQTT_GENERIC_NOTIFICATION ntf;
  266. ntf.evt = NEVT_Unsubscribe;
  267. ntf.sub.mid = mid;
  268. (*m_pEvtCallback)(&ntf, m_pUserParam);
  269. }
  270. }
  271. void CMqttClient::on_publish(int mid)
  272. {
  273. if(m_pEvtCallback && m_evtMask & NEVT_Publish)
  274. {
  275. MQTT_GENERIC_NOTIFICATION ntf;
  276. ntf.evt = NEVT_Publish;
  277. ntf.pub.mid = mid;
  278. (*m_pEvtCallback)(&ntf, m_pUserParam);
  279. }
  280. }
  281. void CMqttClient::on_message(const struct mosquitto_message *message)
  282. {
  283. CMqttMessage *pMsg = CMqttMessage::CreateMessage(message);
  284. PushRcvMsg(pMsg);
  285. if(m_pEvtCallback && m_evtMask & NEVT_Message)
  286. {
  287. MQTT_GENERIC_NOTIFICATION ntf;
  288. ntf.evt = NEVT_Message;
  289. ntf.msg.msg = pMsg;
  290. (*m_pEvtCallback)(&ntf, m_pUserParam);
  291. }
  292. }
  293. void CMqttClient::on_log(int level, const char *str)
  294. {
  295. if(m_pEvtCallback && m_evtMask & NEVT_Log)
  296. {
  297. MQTT_GENERIC_NOTIFICATION ntf;
  298. ntf.evt = NEVT_Log;
  299. ntf.log.level = level;
  300. ntf.log.str = str;
  301. (*m_pEvtCallback)(&ntf, m_pUserParam);
  302. }
  303. }
  304. void CMqttClient::on_error(void)
  305. {
  306. if(m_pEvtCallback && m_evtMask & NEVT_Error)
  307. {
  308. MQTT_GENERIC_NOTIFICATION ntf;
  309. ntf.evt = NEVT_Error;
  310. (*m_pEvtCallback)(NULL, m_pUserParam);
  311. }
  312. }