1
0

UdpServer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. return hio_sendto(channel->io(), data, size, peeraddr);
  81. }
  82. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  83. return sendto(buf->data(), buf->size(), peeraddr);
  84. }
  85. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  86. return sendto(str.data(), str.size(), peeraddr);
  87. }
  88. #if WITH_KCP
  89. void setKcp(kcp_setting_t* setting) {
  90. if (setting == NULL) {
  91. HV_FREE(kcp_setting);
  92. return;
  93. }
  94. if (kcp_setting == NULL) {
  95. HV_ALLOC_SIZEOF(kcp_setting);
  96. }
  97. *kcp_setting = *setting;
  98. }
  99. #endif
  100. public:
  101. std::string host;
  102. int port;
  103. TSocketChannelPtr channel;
  104. #if WITH_KCP
  105. kcp_setting_t* kcp_setting;
  106. #endif
  107. // Callback
  108. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  109. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  110. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  111. private:
  112. EventLoopPtr loop_;
  113. };
  114. template<class TSocketChannel = SocketChannel>
  115. class UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
  116. public:
  117. UdpServerTmpl(EventLoopPtr loop = NULL)
  118. : EventLoopThread(loop)
  119. , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  120. , is_loop_owner(loop == NULL)
  121. {}
  122. virtual ~UdpServerTmpl() {
  123. stop(true);
  124. }
  125. const EventLoopPtr& loop() {
  126. return EventLoopThread::loop();
  127. }
  128. // start thread-safe
  129. void start(bool wait_threads_started = true) {
  130. if (isRunning()) {
  131. UdpServerEventLoopTmpl<TSocketChannel>::start();
  132. } else {
  133. EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
  134. }
  135. }
  136. // stop thread-safe
  137. void stop(bool wait_threads_stopped = true) {
  138. UdpServerEventLoopTmpl<TSocketChannel>::closesocket();
  139. if (is_loop_owner) {
  140. EventLoopThread::stop(wait_threads_stopped);
  141. }
  142. }
  143. private:
  144. bool is_loop_owner;
  145. };
  146. typedef UdpServerTmpl<SocketChannel> UdpServer;
  147. }
  148. #endif // HV_UDP_SERVER_HPP_