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