acl  3.5.3.0
mqtt_client.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include "../stdlib/string.hpp"
4 #include "../connpool/connect_client.hpp"
5 #include "../stream/socket_stream.hpp"
6 
7 namespace acl {
8 
9 class mqtt_header;
10 class mqtt_message;
11 
12 /**
13  * mqtt communication class in sync mode, used by mqtt client or mqtt server.
14  */
16 public:
17  /**
18  * used to construct one mqtt client for connecting one mqtt server.
19  * @param addr {const char*} the mqtt server's addr with the format
20  * like ip|port or domain|port.
21  * @param conn_timeout {int} the timeout for connecting mqtt server.
22  * @param rw_timeout {int} the timeout for reading from mqtt connection.
23  */
24  mqtt_client(const char* addr, int conn_timeout = 10, int rw_timeout = 10);
25 
26  /**
27  * used to construct one mqtt client for connecting mqtt server, or
28  * receiving from mqtt client.
29  * @param conn {acl::socket_stream&}
30  */
32 
33  ~mqtt_client(void);
34 
35  /**
36  * send ont mqtt message to the peer mqtt.
37  * @param message {mqtt_message&} the mqtt message to be sent, where
38  * some viriable method of the mesage will be called, so we can't add
39  * the const limit on it.
40  * @return {bool} return true if sending successfully.
41  */
42  bool send(mqtt_message& message);
43 
44  /**
45  * read mqtt data from mqtt connection and create the correspondingly
46  * mqtt message object with the mqtt type from mqtt header.
47  * @return {mqtt_message*} return NULL if reading error or data invalid.
48  */
49  mqtt_message* get_message(void);
50 
51 public:
52  /**
53  * read mqtt header information from mqtt connection.
54  * @param header {const mqtt_header&} will store the mqtt headeer info.
55  * @return {bool} return true if reading successfully.
56  */
57  bool read_header(mqtt_header& header);
58 
59  /**
60  * read mqtt body information from mqtt connection.
61  * @param header {const mqtt_header&} used to parse mqtt body.
62  * @param body {mqtt_message&} will store mqtt body info.
63  * @return {bool} return true if reading successfully.
64  */
65  bool read_message(const mqtt_header& header, mqtt_message& body);
66 
67 protected:
68  // @override
69  bool open(void);
70 
71 private:
72  string addr_;
73  int conn_timeout_;
74  int rw_timeout_;
75 
76  socket_stream* conn_;
77  socket_stream* conn_internal_;
78 };
79 
80 } // namespace acl
#define ACL_CPP_API