1
0

Channel.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #ifndef HV_CHANNEL_HPP_
  2. #define HV_CHANNEL_HPP_
  3. #include <string>
  4. #include <functional>
  5. #include <memory>
  6. #include "hloop.h"
  7. #include "hsocket.h"
  8. #include "Buffer.h"
  9. namespace hv {
  10. class Channel {
  11. public:
  12. Channel(hio_t* io = NULL) {
  13. io_ = io;
  14. fd_ = -1;
  15. id_ = 0;
  16. ctx_ = NULL;
  17. status = CLOSED;
  18. if (io) {
  19. fd_ = hio_fd(io);
  20. id_ = hio_id(io);
  21. ctx_ = hio_context(io);
  22. hio_set_context(io, this);
  23. if (hio_is_opened(io)) {
  24. status = OPENED;
  25. }
  26. if (hio_getcb_read(io) == NULL) {
  27. hio_setcb_read(io_, on_read);
  28. }
  29. if (hio_getcb_write(io) == NULL) {
  30. hio_setcb_write(io_, on_write);
  31. }
  32. if (hio_getcb_close(io) == NULL) {
  33. hio_setcb_close(io_, on_close);
  34. }
  35. }
  36. }
  37. virtual ~Channel() {
  38. close();
  39. // NOTE: Detach after destructor to avoid triggering onclose
  40. if (io_ && id_ == hio_id(io_)) {
  41. hio_set_context(io_, NULL);
  42. }
  43. }
  44. hio_t* io() { return io_; }
  45. int fd() { return fd_; }
  46. uint32_t id() { return id_; }
  47. int error() { return hio_error(io_); }
  48. // context
  49. void* context() {
  50. return ctx_;
  51. }
  52. void setContext(void* ctx) {
  53. ctx_ = ctx;
  54. }
  55. template<class T>
  56. T* newContext() {
  57. ctx_ = new T;
  58. return (T*)ctx_;
  59. }
  60. template<class T>
  61. T* getContext() {
  62. return (T*)ctx_;
  63. }
  64. template<class T>
  65. void deleteContext() {
  66. if (ctx_) {
  67. delete (T*)ctx_;
  68. ctx_ = NULL;
  69. }
  70. }
  71. bool isOpened() {
  72. if (io_ == NULL || status >= DISCONNECTED) return false;
  73. return id_ == hio_id(io_) && hio_is_opened(io_);
  74. }
  75. bool isClosed() {
  76. return !isOpened();
  77. }
  78. int startRead() {
  79. if (!isOpened()) return -1;
  80. return hio_read_start(io_);
  81. }
  82. int stopRead() {
  83. if (!isOpened()) return -1;
  84. return hio_read_stop(io_);
  85. }
  86. int readOnce() {
  87. if (!isOpened()) return -1;
  88. return hio_read_once(io_);
  89. }
  90. int readString() {
  91. if (!isOpened()) return -1;
  92. return hio_readstring(io_);
  93. }
  94. int readLine() {
  95. if (!isOpened()) return -1;
  96. return hio_readline(io_);
  97. }
  98. int readBytes(int len) {
  99. if (!isOpened() || len <= 0) return -1;
  100. return hio_readbytes(io_, len);
  101. }
  102. // write thread-safe
  103. int write(const void* data, int size) {
  104. if (!isOpened()) return -1;
  105. return hio_write(io_, data, size);
  106. }
  107. int write(Buffer* buf) {
  108. return write(buf->data(), buf->size());
  109. }
  110. int write(const std::string& str) {
  111. return write(str.data(), str.size());
  112. }
  113. // iobuf setting
  114. void setMaxReadBufsize(uint32_t size) {
  115. if (io_ == NULL) return;
  116. hio_set_max_read_bufsize(io_, size);
  117. }
  118. void setMaxWriteBufsize(uint32_t size) {
  119. if (io_ == NULL) return;
  120. hio_set_max_write_bufsize(io_, size);
  121. }
  122. size_t writeBufsize() {
  123. if (io_ == NULL) return 0;
  124. return hio_write_bufsize(io_);
  125. }
  126. bool isWriteComplete() {
  127. return writeBufsize() == 0;
  128. }
  129. // close thread-safe
  130. int close(bool async = false) {
  131. if (isClosed()) return -1;
  132. status = CLOSED;
  133. return async ? hio_close_async(io_) : hio_close(io_);
  134. }
  135. public:
  136. hio_t* io_;
  137. int fd_;
  138. uint32_t id_;
  139. void* ctx_;
  140. enum Status {
  141. OPENED,
  142. CONNECTING,
  143. CONNECTED,
  144. DISCONNECTED,
  145. CLOSED,
  146. } status;
  147. std::function<void(Buffer*)> onread;
  148. // NOTE: Use Channel::isWriteComplete in onwrite callback to determine whether all data has been written.
  149. std::function<void(Buffer*)> onwrite;
  150. std::function<void()> onclose;
  151. private:
  152. static void on_read(hio_t* io, void* data, int readbytes) {
  153. Channel* channel = (Channel*)hio_context(io);
  154. if (channel && channel->onread) {
  155. Buffer buf(data, readbytes);
  156. channel->onread(&buf);
  157. }
  158. }
  159. static void on_write(hio_t* io, const void* data, int writebytes) {
  160. Channel* channel = (Channel*)hio_context(io);
  161. if (channel && channel->onwrite) {
  162. Buffer buf((void*)data, writebytes);
  163. channel->onwrite(&buf);
  164. }
  165. }
  166. static void on_close(hio_t* io) {
  167. Channel* channel = (Channel*)hio_context(io);
  168. if (channel) {
  169. channel->status = CLOSED;
  170. if (channel->onclose) {
  171. channel->onclose();
  172. }
  173. }
  174. }
  175. };
  176. class SocketChannel : public Channel {
  177. public:
  178. std::function<void()> onconnect; // only for TcpClient
  179. std::function<void()> heartbeat;
  180. SocketChannel(hio_t* io) : Channel(io) {
  181. }
  182. virtual ~SocketChannel() {}
  183. // SSL/TLS
  184. int enableSSL() {
  185. if (io_ == NULL) return -1;
  186. return hio_enable_ssl(io_);
  187. }
  188. bool isSSL() {
  189. if (io_ == NULL) return false;
  190. return hio_is_ssl(io_);
  191. }
  192. int setSSL(hssl_t ssl) {
  193. if (io_ == NULL) return -1;
  194. return hio_set_ssl(io_, ssl);
  195. }
  196. int setSslCtx(hssl_ctx_t ssl_ctx) {
  197. if (io_ == NULL) return -1;
  198. return hio_set_ssl_ctx(io_, ssl_ctx);
  199. }
  200. int newSslCtx(hssl_ctx_opt_t* opt) {
  201. if (io_ == NULL) return -1;
  202. return hio_new_ssl_ctx(io_, opt);
  203. }
  204. // for hssl_set_sni_hostname
  205. int setHostname(const std::string& hostname) {
  206. if (io_ == NULL) return -1;
  207. return hio_set_hostname(io_, hostname.c_str());
  208. }
  209. // timeout
  210. void setConnectTimeout(int timeout_ms) {
  211. if (io_ == NULL) return;
  212. hio_set_connect_timeout(io_, timeout_ms);
  213. }
  214. void setCloseTimeout(int timeout_ms) {
  215. if (io_ == NULL) return;
  216. hio_set_close_timeout(io_, timeout_ms);
  217. }
  218. void setReadTimeout(int timeout_ms) {
  219. if (io_ == NULL) return;
  220. hio_set_read_timeout(io_, timeout_ms);
  221. }
  222. void setWriteTimeout(int timeout_ms) {
  223. if (io_ == NULL) return;
  224. hio_set_write_timeout(io_, timeout_ms);
  225. }
  226. void setKeepaliveTimeout(int timeout_ms) {
  227. if (io_ == NULL) return;
  228. hio_set_keepalive_timeout(io_, timeout_ms);
  229. }
  230. // heartbeat
  231. void setHeartbeat(int interval_ms, std::function<void()> fn) {
  232. if (io_ == NULL) return;
  233. heartbeat = std::move(fn);
  234. hio_set_heartbeat(io_, interval_ms, send_heartbeat);
  235. }
  236. // unpack
  237. void setUnpack(unpack_setting_t* setting) {
  238. if (io_ == NULL) return;
  239. hio_set_unpack(io_, setting);
  240. }
  241. int startConnect(int port, const char* host = "127.0.0.1") {
  242. sockaddr_u peeraddr;
  243. memset(&peeraddr, 0, sizeof(peeraddr));
  244. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  245. if (ret != 0) {
  246. // hloge("unknown host %s", host);
  247. return ret;
  248. }
  249. return startConnect(&peeraddr.sa);
  250. }
  251. int startConnect(struct sockaddr* peeraddr) {
  252. if (io_ == NULL) return -1;
  253. hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
  254. return startConnect();
  255. }
  256. int startConnect() {
  257. if (io_ == NULL) return -1;
  258. status = CONNECTING;
  259. hio_setcb_connect(io_, on_connect);
  260. return hio_connect(io_);
  261. }
  262. bool isConnected() {
  263. return status == CONNECTED && isOpened();
  264. }
  265. std::string localaddr() {
  266. if (io_ == NULL) return "";
  267. struct sockaddr* addr = hio_localaddr(io_);
  268. char buf[SOCKADDR_STRLEN] = {0};
  269. return SOCKADDR_STR(addr, buf);
  270. }
  271. std::string peeraddr() {
  272. if (io_ == NULL) return "";
  273. struct sockaddr* addr = hio_peeraddr(io_);
  274. char buf[SOCKADDR_STRLEN] = {0};
  275. return SOCKADDR_STR(addr, buf);
  276. }
  277. private:
  278. static void on_connect(hio_t* io) {
  279. SocketChannel* channel = (SocketChannel*)hio_context(io);
  280. if (channel) {
  281. channel->status = CONNECTED;
  282. if (channel->onconnect) {
  283. channel->onconnect();
  284. }
  285. }
  286. }
  287. static void send_heartbeat(hio_t* io) {
  288. SocketChannel* channel = (SocketChannel*)hio_context(io);
  289. if (channel && channel->heartbeat) {
  290. channel->heartbeat();
  291. }
  292. }
  293. };
  294. typedef std::shared_ptr<Channel> ChannelPtr;
  295. typedef std::shared_ptr<SocketChannel> SocketChannelPtr;
  296. }
  297. #endif // HV_CHANNEL_HPP_