UdpClient.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef HV_UDP_CLIENT_HPP_
  2. #define HV_UDP_CLIENT_HPP_
  3. #include "hsocket.h"
  4. #include "EventLoopThread.h"
  5. #include "Channel.h"
  6. namespace hv {
  7. template<class TSocketChannel = SocketChannel>
  8. class UdpClientEventLoopTmpl {
  9. public:
  10. typedef std::shared_ptr<TSocketChannel> TSocketChannelPtr;
  11. UdpClientEventLoopTmpl(EventLoopPtr loop = NULL) {
  12. loop_ = loop ? loop : std::make_shared<EventLoop>();
  13. #if WITH_KCP
  14. enable_kcp = false;
  15. #endif
  16. }
  17. virtual ~UdpClientEventLoopTmpl() {
  18. }
  19. const EventLoopPtr& loop() {
  20. return loop_;
  21. }
  22. // NOTE: By default, not bind local port. If necessary, you can call bind() after createsocket().
  23. // @retval >=0 sockfd, <0 error
  24. int createsocket(int remote_port, const char* remote_host = "127.0.0.1") {
  25. hio_t* io = hloop_create_udp_client(loop_->loop(), remote_host, remote_port);
  26. if (io == NULL) return -1;
  27. this->remote_host = remote_host;
  28. this->remote_port = remote_port;
  29. channel.reset(new TSocketChannel(io));
  30. return channel->fd();
  31. }
  32. int bind(int local_port, const char* local_host = "0.0.0.0") {
  33. if (channel == NULL || channel->isClosed()) {
  34. return -1;
  35. }
  36. sockaddr_u local_addr;
  37. memset(&local_addr, 0, sizeof(local_addr));
  38. int ret = sockaddr_set_ipport(&local_addr, local_host, local_port);
  39. if (ret != 0) {
  40. return NABS(ret);
  41. }
  42. ret = ::bind(channel->fd(), &local_addr.sa, SOCKADDR_LEN(&local_addr));
  43. if (ret != 0) {
  44. perror("bind");
  45. }
  46. return ret;
  47. }
  48. // closesocket thread-safe
  49. void closesocket() {
  50. if (channel) {
  51. channel->close(true);
  52. }
  53. }
  54. int startRecv() {
  55. if (channel == NULL || channel->isClosed()) {
  56. int sockfd = createsocket(remote_port, remote_host.c_str());
  57. if (sockfd < 0) {
  58. hloge("createsocket %s:%d return %d!\n", remote_host.c_str(), remote_port, sockfd);
  59. return sockfd;
  60. }
  61. }
  62. if (channel == NULL || channel->isClosed()) {
  63. return -1;
  64. }
  65. channel->onread = [this](Buffer* buf) {
  66. if (onMessage) {
  67. onMessage(channel, buf);
  68. }
  69. };
  70. channel->onwrite = [this](Buffer* buf) {
  71. if (onWriteComplete) {
  72. onWriteComplete(channel, buf);
  73. }
  74. };
  75. #if WITH_KCP
  76. if (enable_kcp) {
  77. hio_set_kcp(channel->io(), &kcp_setting);
  78. }
  79. #endif
  80. return channel->startRead();
  81. }
  82. int stopRecv() {
  83. if (channel == NULL) return -1;
  84. return channel->stopRead();
  85. }
  86. // start thread-safe
  87. void start() {
  88. loop_->runInLoop(std::bind(&UdpClientEventLoopTmpl::startRecv, this));
  89. }
  90. // sendto thread-safe
  91. int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
  92. if (channel == NULL) return -1;
  93. std::lock_guard<std::mutex> locker(sendto_mutex);
  94. if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
  95. return channel->write(data, size);
  96. }
  97. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  98. return sendto(buf->data(), buf->size(), peeraddr);
  99. }
  100. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  101. return sendto(str.data(), str.size(), peeraddr);
  102. }
  103. #if WITH_KCP
  104. void setKcp(kcp_setting_t* setting) {
  105. if (setting) {
  106. enable_kcp = true;
  107. kcp_setting = *setting;
  108. } else {
  109. enable_kcp = false;
  110. }
  111. }
  112. #endif
  113. public:
  114. TSocketChannelPtr channel;
  115. std::string remote_host;
  116. int remote_port;
  117. #if WITH_KCP
  118. bool enable_kcp;
  119. kcp_setting_t kcp_setting;
  120. #endif
  121. // Callback
  122. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  123. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  124. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  125. private:
  126. std::mutex sendto_mutex;
  127. EventLoopPtr loop_;
  128. };
  129. template<class TSocketChannel = SocketChannel>
  130. class UdpClientTmpl : private EventLoopThread, public UdpClientEventLoopTmpl<TSocketChannel> {
  131. public:
  132. UdpClientTmpl(EventLoopPtr loop = NULL)
  133. : EventLoopThread(loop)
  134. , UdpClientEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  135. {}
  136. virtual ~UdpClientTmpl() {
  137. stop(true);
  138. }
  139. const EventLoopPtr& loop() {
  140. return EventLoopThread::loop();
  141. }
  142. // start thread-safe
  143. void start(bool wait_threads_started = true) {
  144. if (isRunning()) {
  145. UdpClientEventLoopTmpl<TSocketChannel>::start();
  146. } else {
  147. EventLoopThread::start(wait_threads_started, std::bind(&UdpClientTmpl::startRecv, this));
  148. }
  149. }
  150. // stop thread-safe
  151. void stop(bool wait_threads_stopped = true) {
  152. UdpClientEventLoopTmpl<TSocketChannel>::closesocket();
  153. EventLoopThread::stop(wait_threads_stopped);
  154. }
  155. };
  156. typedef UdpClientTmpl<SocketChannel> UdpClient;
  157. }
  158. #endif // HV_UDP_CLIENT_HPP_