Channel.h 5.0 KB

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