UdpServer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. channel.reset(new TSocketChannel(io));
  27. return channel->fd();
  28. }
  29. // closesocket thread-safe
  30. void closesocket() {
  31. if (channel) {
  32. channel->close(true);
  33. }
  34. }
  35. int startRecv() {
  36. if (channel == NULL) return -1;
  37. channel->onread = [this](Buffer* buf) {
  38. if (onMessage) {
  39. onMessage(channel, buf);
  40. }
  41. };
  42. channel->onwrite = [this](Buffer* buf) {
  43. if (onWriteComplete) {
  44. onWriteComplete(channel, buf);
  45. }
  46. };
  47. #if WITH_KCP
  48. if (enable_kcp) {
  49. hio_set_kcp(channel->io(), &kcp_setting);
  50. }
  51. #endif
  52. return channel->startRead();
  53. }
  54. int stopRecv() {
  55. if (channel == NULL) return -1;
  56. return channel->stopRead();
  57. }
  58. // start thread-safe
  59. void start() {
  60. loop_->runInLoop(std::bind(&UdpServerEventLoopTmpl::startRecv, this));
  61. }
  62. // sendto thread-safe
  63. int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
  64. if (channel == NULL) return -1;
  65. std::lock_guard<std::mutex> locker(sendto_mutex);
  66. if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
  67. return channel->write(data, size);
  68. }
  69. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  70. return sendto(buf->data(), buf->size(), peeraddr);
  71. }
  72. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  73. return sendto(str.data(), str.size(), peeraddr);
  74. }
  75. public:
  76. TSocketChannelPtr channel;
  77. #if WITH_KCP
  78. bool enable_kcp;
  79. kcp_setting_t kcp_setting;
  80. #endif
  81. // Callback
  82. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  83. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  84. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  85. private:
  86. std::mutex sendto_mutex;
  87. EventLoopPtr loop_;
  88. };
  89. template<class TSocketChannel = SocketChannel>
  90. class UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
  91. public:
  92. UdpServerTmpl(EventLoopPtr loop = NULL)
  93. : EventLoopThread()
  94. , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  95. {}
  96. virtual ~UdpServerTmpl() {
  97. stop(true);
  98. }
  99. const EventLoopPtr& loop() {
  100. return EventLoopThread::loop();
  101. }
  102. // start thread-safe
  103. void start(bool wait_threads_started = true) {
  104. EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
  105. }
  106. // stop thread-safe
  107. void stop(bool wait_threads_stopped = true) {
  108. UdpServerEventLoopTmpl<TSocketChannel>::closesocket();
  109. EventLoopThread::stop(wait_threads_stopped);
  110. }
  111. };
  112. typedef UdpServerTmpl<SocketChannel> UdpServer;
  113. }
  114. #endif // HV_UDP_SERVER_HPP_