Channel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if (io) {
  18. fd_ = hio_fd(io);
  19. id_ = hio_id(io);
  20. hio_set_context(io, this);
  21. hio_setcb_read(io_, on_read);
  22. hio_setcb_write(io_, on_write);
  23. hio_setcb_close(io_, on_close);
  24. }
  25. status = isOpened() ? OPENED : CLOSED;
  26. }
  27. virtual ~Channel() {
  28. close();
  29. }
  30. hio_t* io() { return io_; }
  31. int fd() { return fd_; }
  32. uint32_t id() { return id_; }
  33. int error() { return hio_error(io_); }
  34. // context
  35. void* context() {
  36. return ctx_;
  37. }
  38. void setContext(void* ctx) {
  39. ctx_ = ctx;
  40. }
  41. template<class T>
  42. T* newContext() {
  43. ctx_ = new T;
  44. return (T*)ctx_;
  45. }
  46. template<class T>
  47. T* getContext() {
  48. return (T*)ctx_;
  49. }
  50. template<class T>
  51. void deleteContext() {
  52. if (ctx_) {
  53. delete (T*)ctx_;
  54. ctx_ = NULL;
  55. }
  56. }
  57. bool isOpened() {
  58. if (io_ == NULL) return false;
  59. return id_ == hio_id(io_) && hio_is_opened(io_);
  60. }
  61. bool isClosed() {
  62. return !isOpened();
  63. }
  64. int startRead() {
  65. if (!isOpened()) return 0;
  66. return hio_read_start(io_);
  67. }
  68. int stopRead() {
  69. if (!isOpened()) return 0;
  70. return hio_read_stop(io_);
  71. }
  72. int write(const void* data, int size) {
  73. if (!isOpened()) return 0;
  74. return hio_write(io_, data, size);
  75. }
  76. int write(Buffer* buf) {
  77. return write(buf->data(), buf->size());
  78. }
  79. int write(const std::string& str) {
  80. return write(str.data(), str.size());
  81. }
  82. int close() {
  83. if (!isOpened()) return 0;
  84. return hio_close(io_);
  85. }
  86. public:
  87. hio_t* io_;
  88. int fd_;
  89. uint32_t id_;
  90. void* ctx_;
  91. enum Status {
  92. // Channel::Status
  93. OPENED,
  94. CLOSED,
  95. // SocketChannel::Status
  96. CONNECTING,
  97. CONNECTED,
  98. DISCONNECTED,
  99. } status;
  100. std::function<void(Buffer*)> onread;
  101. std::function<void(Buffer*)> onwrite;
  102. std::function<void()> onclose;
  103. private:
  104. static void on_read(hio_t* io, void* data, int readbytes) {
  105. Channel* channel = (Channel*)hio_context(io);
  106. if (channel && channel->onread) {
  107. Buffer buf(data, readbytes);
  108. channel->onread(&buf);
  109. }
  110. }
  111. static void on_write(hio_t* io, const void* data, int writebytes) {
  112. Channel* channel = (Channel*)hio_context(io);
  113. if (channel && channel->onwrite) {
  114. Buffer buf((void*)data, writebytes);
  115. channel->onwrite(&buf);
  116. }
  117. }
  118. static void on_close(hio_t* io) {
  119. Channel* channel = (Channel*)hio_context(io);
  120. if (channel) {
  121. channel->status = CLOSED;
  122. if (channel->onclose) {
  123. channel->onclose();
  124. }
  125. }
  126. }
  127. };
  128. class SocketChannel : public Channel {
  129. public:
  130. // for TcpClient
  131. std::function<void()> onconnect;
  132. SocketChannel(hio_t* io) : Channel(io) {
  133. }
  134. virtual ~SocketChannel() {}
  135. int enableSSL() {
  136. return hio_enable_ssl(io_);
  137. }
  138. void setConnectTimeout(int timeout_ms) {
  139. hio_set_connect_timeout(io_, timeout_ms);
  140. }
  141. int startConnect(int port, const char* host = "127.0.0.1") {
  142. sockaddr_u peeraddr;
  143. memset(&peeraddr, 0, sizeof(peeraddr));
  144. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  145. if (ret != 0) {
  146. // hloge("unknown host %s", host);
  147. return ret;
  148. }
  149. return startConnect(&peeraddr.sa);
  150. }
  151. int startConnect(struct sockaddr* peeraddr) {
  152. hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
  153. return startConnect();
  154. }
  155. int startConnect() {
  156. status = CONNECTING;
  157. hio_setcb_connect(io_, on_connect);
  158. return hio_connect(io_);
  159. }
  160. bool isConnected() {
  161. return isOpened() && status == CONNECTED;
  162. }
  163. std::string localaddr() {
  164. struct sockaddr* addr = hio_localaddr(io_);
  165. char buf[SOCKADDR_STRLEN] = {0};
  166. return SOCKADDR_STR(addr, buf);
  167. }
  168. std::string peeraddr() {
  169. struct sockaddr* addr = hio_peeraddr(io_);
  170. char buf[SOCKADDR_STRLEN] = {0};
  171. return SOCKADDR_STR(addr, buf);
  172. }
  173. int send(const std::string& str) {
  174. return write(str);
  175. }
  176. private:
  177. static void on_connect(hio_t* io) {
  178. SocketChannel* channel = (SocketChannel*)hio_context(io);
  179. if (channel) {
  180. channel->status = CONNECTED;
  181. if (channel->onconnect) {
  182. channel->onconnect();
  183. }
  184. }
  185. }
  186. };
  187. typedef std::shared_ptr<Channel> ChannelPtr;
  188. typedef std::shared_ptr<SocketChannel> SocketChannelPtr;
  189. }
  190. #endif // HV_CHANNEL_HPP_