Channel.h 7.5 KB

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