1
0

UdpServer.h 4.7 KB

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