1
0

UdpClient.h 5.3 KB

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