UdpClient.h 4.2 KB

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