acl  3.5.3.0
mqtt_aclient.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include "../acl_cpp_define.hpp"
4 #include "../stream/aio_socket_stream.hpp"
5 
6 namespace acl {
7 
8 class sslbase_conf;
9 class mqtt_header;
10 class mqtt_message;
11 
12 /**
13  * mqtt communication class in aio mode, used by mqtt client or mqtt server.
14  */
16 public:
17  /**
18  * constructor
19  * @param handle {aio_handle&}
20  * @param ssl_conf {sslbase_conf*} if not NULL, ssl will be used
21  */
22  mqtt_aclient(aio_handle& handle, sslbase_conf* ssl_conf = NULL);
23 
24  /**
25  * because the subclass object was created dynamically, the method
26  * will be called when the subclass object is to be freed.
27  */
28  virtual void destroy(void) = 0;
29 
30  /**
31  * get the ssl conf object passed in constructor.
32  * @return {sslbase_conf*} return NULL if not set.
33  */
34  sslbase_conf* get_ssl_conf(void) const {
35  return ssl_conf_;
36  }
37 
38  /**
39  * connect the remote mqtt server, when connected with the server,
40  * the callback on_connect() will be called
41  * @param addr {const char*} the mqtt server's addr with the format
42  * ip|port, or domain|port
43  * @param conn_timeout {int} the timeout for connecting to the server
44  * @param rw_timeout {int} the timeout read/write with the server
45  * @return bool {bool} if return false, you should call destroy() to
46  * delete the subclass object
47  */
48  bool open(const char* addr, int conn_timeout, int rw_timeout);
49 
50  /**
51  * called when connect or accept one connection
52  * @param conn {aio_socket_stream*}
53  * @return bool {bool} if return false, you should call destroy() to
54  * delete the subclass object
55  */
56  bool open(aio_socket_stream* conn);
57 
58  /**
59  * close the connection with the mqtt server async
60  */
61  void close(void);
62 
63  /**
64  * get the connection with the mqtt server
65  * @return {aio_socket_stream*} return NULL if not connected
66  */
67  aio_socket_stream* get_conn(void) const {
68  return conn_;
69  }
70 
71  /**
72  * set the remote host name to specify the SSL SNI for SSL handshake,
73  * used to connect one mqtt server as a connection client.
74  * @param host {const char*} the host name
75  */
76  void set_host(const char* host);
77 
78 public:
79  /**
80  * send one mqtt message to one mqtt peer.
81  * @param message {mqtt_message&}
82  * @return {bool} return true if sending successfully, or false if
83  * some error happened.
84  */
85  bool send(mqtt_message& message);
86 
87 public:
88  /**
89  * get the current dns addr when connection one mqtt server
90  * @param out {string&} store the result.
91  * @return {bool} return true if getting dns address successfully.
92  */
93  bool get_ns_addr(string& out) const;
94 
95  /**
96  * get the mqtt server addr after resolving the domain's address.
97  * @param out {string&} store the result.
98  * @return {bool} return true if getting server's address successfully.
99  */
100  bool get_server_addr(string& out) const;
101 
102 protected:
103  // the subclass should be created dynamically
104  virtual ~mqtt_aclient(void);
105 
106  // @override dummy
107  bool open_callback(void) { return true; }
108 
109  // @override
110  bool timeout_callback(void);
111 
112  // @override
113  void close_callback(void);
114 
115  // @override
116  bool read_wakeup(void);
117 
118  // @override
119  bool read_callback(char* data, int len);
120 
121 protected:
122  // wait for reading data from peer
123  bool message_await(void);
124 
125  // virtual method called when resolving DNS failed.
126  virtual void on_ns_failed(void) {}
127 
128  // virtual method called when it's timeout to connect mqtt server.
129  virtual void on_connect_timeout(void) {}
130 
131  // virtual method called when it's failed to connect mqtt server.
132  virtual void on_connect_failed(void) {}
133 
134  // virtual method called when reading timeout.
135  virtual bool on_read_timeout(void) { return false; }
136 
137  // virtual method when connection was disconnected.
138  virtual void on_disconnect(void) {};
139 
140  // should be implemented by subclass.
141  virtual bool on_open(void) = 0;
142 
143  // subclass can implement the method to override the default method.
144  virtual bool on_header(const mqtt_header&) { return true; };
145 
146  // should be implemented by subclass.
147  virtual bool on_body(const mqtt_message&) = 0;
148 
149 private:
150  aio_handle& handle_;
151  sslbase_conf* ssl_conf_;
152  aio_socket_stream* conn_;
153  int rw_timeout_;
154  string host_;
155  struct sockaddr_storage ns_addr_;
156  struct sockaddr_storage serv_addr_;
157  mqtt_header* header_;
158  mqtt_message* body_;
159 
160  // callbed when mqtt connection was created, which can be used
161  // for client or server.
162  bool open_done(void);
163 
164  // used for ssl communication.
165  bool handle_ssl_handshake(void);
166 
167  // handle the data received from mqtt connection.
168  int handle_data(char* data, int len);
169 
170  // called if it's ok for connecting one mqtt server.
171  bool handle_connect(const ACL_ASTREAM_CTX* ctx);
172 
173  // called by aio module of acl.
174  static int connect_callback(const ACL_ASTREAM_CTX* ctx);
175 };
176 
177 } // namespace acl
virtual void on_connect_timeout(void)
virtual bool on_header(const mqtt_header &)
aio_socket_stream * get_conn(void) const
virtual void on_ns_failed(void)
virtual bool on_read_timeout(void)
bool open_callback(void)
sslbase_conf * get_ssl_conf(void) const
virtual void on_connect_failed(void)
struct ACL_ASTREAM_CTX ACL_ASTREAM_CTX
Definition: acl_aio.h:98
#define ACL_CPP_API
virtual void on_disconnect(void)