Channel.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. return hio_set_max_read_bufsize(io_, size);
  113. }
  114. void setMaxWriteBufsize(uint32_t size) {
  115. if (io_ == NULL) return;
  116. return 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. // timeout
  203. void setConnectTimeout(int timeout_ms) {
  204. if (io_ == NULL) return;
  205. hio_set_connect_timeout(io_, timeout_ms);
  206. }
  207. void setCloseTimeout(int timeout_ms) {
  208. if (io_ == NULL) return;
  209. hio_set_close_timeout(io_, timeout_ms);
  210. }
  211. void setReadTimeout(int timeout_ms) {
  212. if (io_ == NULL) return;
  213. hio_set_read_timeout(io_, timeout_ms);
  214. }
  215. void setWriteTimeout(int timeout_ms) {
  216. if (io_ == NULL) return;
  217. hio_set_write_timeout(io_, timeout_ms);
  218. }
  219. void setKeepaliveTimeout(int timeout_ms) {
  220. if (io_ == NULL) return;
  221. hio_set_keepalive_timeout(io_, timeout_ms);
  222. }
  223. // heartbeat
  224. void setHeartbeat(int interval_ms, std::function<void()> fn) {
  225. if (io_ == NULL) return;
  226. heartbeat = std::move(fn);
  227. hio_set_heartbeat(io_, interval_ms, send_heartbeat);
  228. }
  229. // unpack
  230. void setUnpack(unpack_setting_t* setting) {
  231. if (io_ == NULL) return;
  232. hio_set_unpack(io_, setting);
  233. }
  234. int startConnect(int port, const char* host = "127.0.0.1") {
  235. sockaddr_u peeraddr;
  236. memset(&peeraddr, 0, sizeof(peeraddr));
  237. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  238. if (ret != 0) {
  239. // hloge("unknown host %s", host);
  240. return ret;
  241. }
  242. return startConnect(&peeraddr.sa);
  243. }
  244. int startConnect(struct sockaddr* peeraddr) {
  245. if (io_ == NULL) return -1;
  246. hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
  247. return startConnect();
  248. }
  249. int startConnect() {
  250. if (io_ == NULL) return -1;
  251. status = CONNECTING;
  252. hio_setcb_connect(io_, on_connect);
  253. return hio_connect(io_);
  254. }
  255. bool isConnected() {
  256. return status == CONNECTED && isOpened();
  257. }
  258. std::string localaddr() {
  259. if (io_ == NULL) return "";
  260. struct sockaddr* addr = hio_localaddr(io_);
  261. char buf[SOCKADDR_STRLEN] = {0};
  262. return SOCKADDR_STR(addr, buf);
  263. }
  264. std::string peeraddr() {
  265. if (io_ == NULL) return "";
  266. struct sockaddr* addr = hio_peeraddr(io_);
  267. char buf[SOCKADDR_STRLEN] = {0};
  268. return SOCKADDR_STR(addr, buf);
  269. }
  270. private:
  271. static void on_connect(hio_t* io) {
  272. SocketChannel* channel = (SocketChannel*)hio_context(io);
  273. if (channel) {
  274. channel->status = CONNECTED;
  275. if (channel->onconnect) {
  276. channel->onconnect();
  277. }
  278. }
  279. }
  280. static void send_heartbeat(hio_t* io) {
  281. SocketChannel* channel = (SocketChannel*)hio_context(io);
  282. if (channel && channel->heartbeat) {
  283. channel->heartbeat();
  284. }
  285. }
  286. };
  287. typedef std::shared_ptr<Channel> ChannelPtr;
  288. typedef std::shared_ptr<SocketChannel> SocketChannelPtr;
  289. }
  290. #endif // HV_CHANNEL_HPP_