UdpServer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. enable_kcp = false;
  16. #endif
  17. }
  18. virtual ~UdpServerEventLoopTmpl() {
  19. }
  20. const EventLoopPtr& loop() {
  21. return loop_;
  22. }
  23. //@retval >=0 bindfd, <0 error
  24. int createsocket(int port, const char* host = "0.0.0.0") {
  25. hio_t* io = hloop_create_udp_server(loop_->loop(), host, port);
  26. if (io == NULL) return -1;
  27. this->host = host;
  28. this->port = 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 bindfd = createsocket(port, host.c_str());
  41. if (bindfd < 0) {
  42. hloge("createsocket %s:%d return %d!\n", host.c_str(), port, bindfd);
  43. return bindfd;
  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(&UdpServerEventLoopTmpl::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. std::string host;
  99. int port;
  100. TSocketChannelPtr channel;
  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 UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
  115. public:
  116. UdpServerTmpl(EventLoopPtr loop = NULL)
  117. : EventLoopThread(loop)
  118. , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  119. , is_loop_owner(loop == NULL)
  120. {}
  121. virtual ~UdpServerTmpl() {
  122. stop(true);
  123. }
  124. const EventLoopPtr& loop() {
  125. return EventLoopThread::loop();
  126. }
  127. // start thread-safe
  128. void start(bool wait_threads_started = true) {
  129. if (isRunning()) {
  130. UdpServerEventLoopTmpl<TSocketChannel>::start();
  131. } else {
  132. EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
  133. }
  134. }
  135. // stop thread-safe
  136. void stop(bool wait_threads_stopped = true) {
  137. UdpServerEventLoopTmpl<TSocketChannel>::closesocket();
  138. if (is_loop_owner) {
  139. EventLoopThread::stop(wait_threads_stopped);
  140. }
  141. }
  142. private:
  143. bool is_loop_owner;
  144. };
  145. typedef UdpServerTmpl<SocketChannel> UdpServer;
  146. }
  147. #endif // HV_UDP_SERVER_HPP_