UdpServer.h 4.5 KB

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