UdpServer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. assert(channel != NULL);
  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. // start thread-safe
  55. void start() {
  56. loop_->runInLoop(std::bind(&UdpServerEventLoopTmpl::startRecv, this));
  57. }
  58. // sendto thread-safe
  59. int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
  60. if (channel == NULL) return -1;
  61. std::lock_guard<std::mutex> locker(sendto_mutex);
  62. if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
  63. return channel->write(data, size);
  64. }
  65. int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
  66. return sendto(buf->data(), buf->size(), peeraddr);
  67. }
  68. int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
  69. return sendto(str.data(), str.size(), peeraddr);
  70. }
  71. public:
  72. TSocketChannelPtr channel;
  73. #if WITH_KCP
  74. bool enable_kcp;
  75. kcp_setting_t kcp_setting;
  76. #endif
  77. // Callback
  78. std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage;
  79. // NOTE: Use Channel::isWriteComplete in onWriteComplete callback to determine whether all data has been written.
  80. std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete;
  81. private:
  82. std::mutex sendto_mutex;
  83. EventLoopPtr loop_;
  84. };
  85. template<class TSocketChannel = SocketChannel>
  86. class UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
  87. public:
  88. UdpServerTmpl(EventLoopPtr loop = NULL)
  89. : EventLoopThread()
  90. , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
  91. {}
  92. virtual ~UdpServerTmpl() {
  93. stop(true);
  94. }
  95. const EventLoopPtr& loop() {
  96. return EventLoopThread::loop();
  97. }
  98. // start thread-safe
  99. void start(bool wait_threads_started = true) {
  100. EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
  101. }
  102. // stop thread-safe
  103. void stop(bool wait_threads_stopped = true) {
  104. EventLoopThread::stop(wait_threads_stopped);
  105. }
  106. };
  107. typedef UdpServerTmpl<SocketChannel> UdpServer;
  108. }
  109. #endif // HV_UDP_SERVER_HPP_