1
0

Channel.h 5.3 KB

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