Channel.h 8.3 KB

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