Channel.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. size_t writeBufsize() {
  110. if (io_ == NULL) return 0;
  111. return hio_write_bufsize(io_);
  112. }
  113. bool isWriteComplete() {
  114. return writeBufsize() == 0;
  115. }
  116. // close thread-safe
  117. int close(bool async = false) {
  118. if (!isOpened()) return -1;
  119. if (async) {
  120. return hio_close_async(io_);
  121. }
  122. return hio_close(io_);
  123. }
  124. public:
  125. hio_t* io_;
  126. int fd_;
  127. uint32_t id_;
  128. void* ctx_;
  129. enum Status {
  130. OPENED,
  131. CONNECTING,
  132. CONNECTED,
  133. DISCONNECTED,
  134. CLOSED,
  135. } status;
  136. std::function<void(Buffer*)> onread;
  137. // NOTE: Use Channel::isWriteComplete in onwrite callback to determine whether all data has been written.
  138. std::function<void(Buffer*)> onwrite;
  139. std::function<void()> onclose;
  140. private:
  141. static void on_read(hio_t* io, void* data, int readbytes) {
  142. Channel* channel = (Channel*)hio_context(io);
  143. if (channel && channel->onread) {
  144. Buffer buf(data, readbytes);
  145. channel->onread(&buf);
  146. }
  147. }
  148. static void on_write(hio_t* io, const void* data, int writebytes) {
  149. Channel* channel = (Channel*)hio_context(io);
  150. if (channel && channel->onwrite) {
  151. Buffer buf((void*)data, writebytes);
  152. channel->onwrite(&buf);
  153. }
  154. }
  155. static void on_close(hio_t* io) {
  156. Channel* channel = (Channel*)hio_context(io);
  157. if (channel) {
  158. channel->status = CLOSED;
  159. if (channel->onclose) {
  160. channel->onclose();
  161. }
  162. }
  163. }
  164. };
  165. class SocketChannel : public Channel {
  166. public:
  167. std::function<void()> onconnect; // only for TcpClient
  168. std::function<void()> heartbeat;
  169. SocketChannel(hio_t* io) : Channel(io) {
  170. }
  171. virtual ~SocketChannel() {}
  172. // SSL/TLS
  173. int enableSSL() {
  174. if (io_ == NULL) return -1;
  175. return hio_enable_ssl(io_);
  176. }
  177. bool isSSL() {
  178. if (io_ == NULL) return false;
  179. return hio_is_ssl(io_);
  180. }
  181. int setSSL(hssl_t ssl) {
  182. if (io_ == NULL) return -1;
  183. return hio_set_ssl(io_, ssl);
  184. }
  185. int setSslCtx(hssl_ctx_t ssl_ctx) {
  186. if (io_ == NULL) return -1;
  187. return hio_set_ssl_ctx(io_, ssl_ctx);
  188. }
  189. int newSslCtx(hssl_ctx_opt_t* opt) {
  190. if (io_ == NULL) return -1;
  191. return hio_new_ssl_ctx(io_, opt);
  192. }
  193. // timeout
  194. void setConnectTimeout(int timeout_ms) {
  195. if (io_ == NULL) return;
  196. hio_set_connect_timeout(io_, timeout_ms);
  197. }
  198. void setCloseTimeout(int timeout_ms) {
  199. if (io_ == NULL) return;
  200. hio_set_close_timeout(io_, timeout_ms);
  201. }
  202. void setReadTimeout(int timeout_ms) {
  203. if (io_ == NULL) return;
  204. hio_set_read_timeout(io_, timeout_ms);
  205. }
  206. void setWriteTimeout(int timeout_ms) {
  207. if (io_ == NULL) return;
  208. hio_set_write_timeout(io_, timeout_ms);
  209. }
  210. void setKeepaliveTimeout(int timeout_ms) {
  211. if (io_ == NULL) return;
  212. hio_set_keepalive_timeout(io_, timeout_ms);
  213. }
  214. // heartbeat
  215. void setHeartbeat(int interval_ms, std::function<void()> fn) {
  216. if (io_ == NULL) return;
  217. heartbeat = std::move(fn);
  218. hio_set_heartbeat(io_, interval_ms, send_heartbeat);
  219. }
  220. // unpack
  221. void setUnpack(unpack_setting_t* setting) {
  222. if (io_ == NULL) return;
  223. hio_set_unpack(io_, setting);
  224. }
  225. int startConnect(int port, const char* host = "127.0.0.1") {
  226. sockaddr_u peeraddr;
  227. memset(&peeraddr, 0, sizeof(peeraddr));
  228. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  229. if (ret != 0) {
  230. // hloge("unknown host %s", host);
  231. return ret;
  232. }
  233. return startConnect(&peeraddr.sa);
  234. }
  235. int startConnect(struct sockaddr* peeraddr) {
  236. if (io_ == NULL) return -1;
  237. hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
  238. return startConnect();
  239. }
  240. int startConnect() {
  241. if (io_ == NULL) return -1;
  242. status = CONNECTING;
  243. hio_setcb_connect(io_, on_connect);
  244. return hio_connect(io_);
  245. }
  246. bool isConnected() {
  247. return status == CONNECTED && isOpened();
  248. }
  249. std::string localaddr() {
  250. if (io_ == NULL) return "";
  251. struct sockaddr* addr = hio_localaddr(io_);
  252. char buf[SOCKADDR_STRLEN] = {0};
  253. return SOCKADDR_STR(addr, buf);
  254. }
  255. std::string peeraddr() {
  256. if (io_ == NULL) return "";
  257. struct sockaddr* addr = hio_peeraddr(io_);
  258. char buf[SOCKADDR_STRLEN] = {0};
  259. return SOCKADDR_STR(addr, buf);
  260. }
  261. private:
  262. static void on_connect(hio_t* io) {
  263. SocketChannel* channel = (SocketChannel*)hio_context(io);
  264. if (channel) {
  265. channel->status = CONNECTED;
  266. if (channel->onconnect) {
  267. channel->onconnect();
  268. }
  269. }
  270. }
  271. static void send_heartbeat(hio_t* io) {
  272. SocketChannel* channel = (SocketChannel*)hio_context(io);
  273. if (channel && channel->heartbeat) {
  274. channel->heartbeat();
  275. }
  276. }
  277. };
  278. typedef std::shared_ptr<Channel> ChannelPtr;
  279. typedef std::shared_ptr<SocketChannel> SocketChannelPtr;
  280. }
  281. #endif // HV_CHANNEL_HPP_