Channel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. int 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(Buffer* buf) {
  73. if (!isOpened()) return 0;
  74. return hio_write(io_, buf->data(), buf->size());
  75. }
  76. int write(const std::string& str) {
  77. if (!isOpened()) return 0;
  78. return hio_write(io_, str.data(), str.size());
  79. }
  80. int close() {
  81. if (!isOpened()) return 0;
  82. return hio_close(io_);
  83. }
  84. public:
  85. hio_t* io_;
  86. int fd_;
  87. uint32_t id_;
  88. void* ctx_;
  89. enum Status {
  90. // Channel::Status
  91. OPENED,
  92. CLOSED,
  93. // SocketChannel::Status
  94. CONNECTING,
  95. CONNECTED,
  96. DISCONNECTED,
  97. } status;
  98. std::function<void(Buffer*)> onread;
  99. std::function<void(Buffer*)> onwrite;
  100. std::function<void()> onclose;
  101. private:
  102. static void on_read(hio_t* io, void* data, int readbytes) {
  103. Channel* channel = (Channel*)hio_context(io);
  104. if (channel && channel->onread) {
  105. Buffer buf(data, readbytes);
  106. channel->onread(&buf);
  107. }
  108. }
  109. static void on_write(hio_t* io, const void* data, int writebytes) {
  110. Channel* channel = (Channel*)hio_context(io);
  111. if (channel && channel->onwrite) {
  112. Buffer buf((void*)data, writebytes);
  113. channel->onwrite(&buf);
  114. }
  115. }
  116. static void on_close(hio_t* io) {
  117. Channel* channel = (Channel*)hio_context(io);
  118. if (channel) {
  119. channel->status = CLOSED;
  120. if (channel->onclose) {
  121. channel->onclose();
  122. }
  123. }
  124. }
  125. };
  126. class SocketChannel : public Channel {
  127. public:
  128. // for TcpClient
  129. std::function<void()> onconnect;
  130. SocketChannel(hio_t* io) : Channel(io) {
  131. }
  132. virtual ~SocketChannel() {}
  133. int enableSSL() {
  134. return hio_enable_ssl(io_);
  135. }
  136. void setConnectTimeout(int timeout_ms) {
  137. hio_set_connect_timeout(io_, timeout_ms);
  138. }
  139. int startConnect(int port, const char* host = "127.0.0.1") {
  140. sockaddr_u peeraddr;
  141. memset(&peeraddr, 0, sizeof(peeraddr));
  142. int ret = sockaddr_set_ipport(&peeraddr, host, port);
  143. if (ret != 0) {
  144. // hloge("unknown host %s", host);
  145. return ret;
  146. }
  147. return startConnect(&peeraddr.sa);
  148. }
  149. int startConnect(struct sockaddr* peeraddr) {
  150. hio_set_peeraddr(io_, peeraddr, SOCKADDR_LEN(peeraddr));
  151. return startConnect();
  152. }
  153. int startConnect() {
  154. status = CONNECTING;
  155. hio_setcb_connect(io_, on_connect);
  156. return hio_connect(io_);
  157. }
  158. bool isConnected() {
  159. return isOpened() && status == CONNECTED;
  160. }
  161. std::string localaddr() {
  162. struct sockaddr* addr = hio_localaddr(io_);
  163. char buf[SOCKADDR_STRLEN] = {0};
  164. return SOCKADDR_STR(addr, buf);
  165. }
  166. std::string peeraddr() {
  167. struct sockaddr* addr = hio_peeraddr(io_);
  168. char buf[SOCKADDR_STRLEN] = {0};
  169. return SOCKADDR_STR(addr, buf);
  170. }
  171. int send(const std::string& str) {
  172. return write(str);
  173. }
  174. private:
  175. static void on_connect(hio_t* io) {
  176. SocketChannel* channel = (SocketChannel*)hio_context(io);
  177. if (channel) {
  178. channel->status = CONNECTED;
  179. if (channel->onconnect) {
  180. channel->onconnect();
  181. }
  182. }
  183. }
  184. };
  185. typedef std::shared_ptr<Channel> ChannelPtr;
  186. typedef std::shared_ptr<SocketChannel> SocketChannelPtr;
  187. }
  188. #endif // HV_CHANNEL_HPP_