1
0

UdpServer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. public:
  87. std::string host;
  88. int port;
  89. TSocketChannelPtr channel;
  90. #if WITH_KCP
  91. bool enable_kcp;
  92. kcp_setting_t kcp_setting;
  93. #endif
  94. // Callback
  95. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  96. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  97. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  98. private:
  99. std::mutex sendto_mutex;
  100. EventLoopPtr loop_;
  101. };
  102. template<class TSocketChannel = SocketChannel>
  103. class UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
  104. public:
  105. UdpServerTmpl(EventLoopPtr loop = NULL)
  106. : EventLoopThread(loop)
  107. , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  108. {}
  109. virtual ~UdpServerTmpl() {
  110. stop(true);
  111. }
  112. const EventLoopPtr& loop() {
  113. return EventLoopThread::loop();
  114. }
  115. // start thread-safe
  116. void start(bool wait_threads_started = true) {
  117. if (isRunning()) {
  118. UdpServerEventLoopTmpl<TSocketChannel>::start();
  119. } else {
  120. EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
  121. }
  122. }
  123. // stop thread-safe
  124. void stop(bool wait_threads_stopped = true) {
  125. UdpServerEventLoopTmpl<TSocketChannel>::closesocket();
  126. EventLoopThread::stop(wait_threads_stopped);
  127. }
  128. };
  129. typedef UdpServerTmpl<SocketChannel> UdpServer;
  130. }
  131. #endif // HV_UDP_SERVER_HPP_