UdpClient.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 system api 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. // closesocket thread-safe
  33. void closesocket() {
  34. if (channel) {
  35. channel->close(true);
  36. }
  37. }
  38. int startRecv() {
  39. if (channel == NULL || channel->isClosed()) {
  40. int sockfd = createsocket(remote_port, remote_host.c_str());
  41. if (sockfd < 0) {
  42. hloge("createsocket %s:%d return %d!\n", remote_host.c_str(), remote_port, sockfd);
  43. return sockfd;
  44. }
  45. }
  46. if (channel == NULL || channel->isClosed()) {
  47. return -1;
  48. }
  49. channel->onread = [this](Buffer* buf) {
  50. if (onMessage) {
  51. onMessage(channel, buf);
  52. }
  53. };
  54. channel->onwrite = [this](Buffer* buf) {
  55. if (onWriteComplete) {
  56. onWriteComplete(channel, buf);
  57. }
  58. };
  59. #if WITH_KCP
  60. if (enable_kcp) {
  61. hio_set_kcp(channel->io(), &kcp_setting);
  62. }
  63. #endif
  64. return channel->startRead();
  65. }
  66. int stopRecv() {
  67. if (channel == NULL) return -1;
  68. return channel->stopRead();
  69. }
  70. // start thread-safe
  71. void start() {
  72. loop_->runInLoop(std::bind(&UdpClientEventLoopTmpl::startRecv, this));
  73. }
  74. // sendto thread-safe
  75. int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
  76. if (channel == NULL) return -1;
  77. std::lock_guard<std::mutex> locker(sendto_mutex);
  78. if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
  79. return channel->write(data, size);
  80. }
  81. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  82. return sendto(buf->data(), buf->size(), peeraddr);
  83. }
  84. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  85. return sendto(str.data(), str.size(), peeraddr);
  86. }
  87. #if WITH_KCP
  88. void setKcp(kcp_setting_t* setting) {
  89. if (setting) {
  90. enable_kcp = true;
  91. kcp_setting = *setting;
  92. } else {
  93. enable_kcp = false;
  94. }
  95. }
  96. #endif
  97. public:
  98. TSocketChannelPtr channel;
  99. std::string remote_host;
  100. int remote_port;
  101. #if WITH_KCP
  102. bool enable_kcp;
  103. kcp_setting_t kcp_setting;
  104. #endif
  105. // Callback
  106. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  107. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  108. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  109. private:
  110. std::mutex sendto_mutex;
  111. EventLoopPtr loop_;
  112. };
  113. template<class TSocketChannel = SocketChannel>
  114. class UdpClientTmpl : private EventLoopThread, public UdpClientEventLoopTmpl<TSocketChannel> {
  115. public:
  116. UdpClientTmpl(EventLoopPtr loop = NULL)
  117. : EventLoopThread(loop)
  118. , UdpClientEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  119. {}
  120. virtual ~UdpClientTmpl() {
  121. stop(true);
  122. }
  123. const EventLoopPtr& loop() {
  124. return EventLoopThread::loop();
  125. }
  126. // start thread-safe
  127. void start(bool wait_threads_started = true) {
  128. if (isRunning()) {
  129. UdpClientEventLoopTmpl<TSocketChannel>::start();
  130. } else {
  131. EventLoopThread::start(wait_threads_started, std::bind(&UdpClientTmpl::startRecv, this));
  132. }
  133. }
  134. // stop thread-safe
  135. void stop(bool wait_threads_stopped = true) {
  136. UdpClientEventLoopTmpl<TSocketChannel>::closesocket();
  137. EventLoopThread::stop(wait_threads_stopped);
  138. }
  139. };
  140. typedef UdpClientTmpl<SocketChannel> UdpClient;
  141. }
  142. #endif // HV_UDP_CLIENT_HPP_