UdpServer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2021 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef TOOLKIT_NETWORK_UDPSERVER_H
  11. #define TOOLKIT_NETWORK_UDPSERVER_H
  12. #include "Network/Server.h"
  13. #include "Network/Session.h"
  14. namespace toolkit {
  15. class UdpServer : public Server {
  16. public:
  17. using Ptr = std::shared_ptr<UdpServer>;
  18. using PeerIdType = uint64_t;
  19. explicit UdpServer(const EventPoller::Ptr &poller = nullptr);
  20. ~UdpServer() override;
  21. /**
  22. * @brief 开始监听服务器
  23. */
  24. template<typename SessionType>
  25. void start(uint16_t port, const std::string &host = "0.0.0.0") {
  26. // Session 创建器, 通过它创建不同类型的服务器
  27. _session_alloc = [](const UdpServer::Ptr &server, const Socket::Ptr &sock) {
  28. auto session = std::make_shared<SessionType>(sock);
  29. session->setOnCreateSocket(server->_on_create_socket);
  30. return std::make_shared<SessionHelper>(server, session);
  31. };
  32. start_l(port, host);
  33. }
  34. /**
  35. * @brief 获取服务器监听端口号, 服务器可以选择监听随机端口
  36. */
  37. uint16_t getPort();
  38. /**
  39. * @brief 自定义socket构建行为
  40. */
  41. void setOnCreateSocket(Socket::onCreateSocket cb);
  42. protected:
  43. virtual Ptr onCreatServer(const EventPoller::Ptr &poller);
  44. virtual void cloneFrom(const UdpServer &that);
  45. private:
  46. /**
  47. * @brief 开始udp server
  48. * @param port 本机端口,0则随机
  49. * @param host 监听网卡ip
  50. */
  51. void start_l(uint16_t port, const std::string &host = "0.0.0.0");
  52. /**
  53. * @brief 定时管理 Session, UDP 会话需要根据需要处理超时
  54. */
  55. void onManagerSession();
  56. void onRead(const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len);
  57. /**
  58. * @brief 接收到数据,可能来自server fd,也可能来自peer fd
  59. * @param is_server_fd 时候为server fd
  60. * @param id 客户端id
  61. * @param buf 数据
  62. * @param addr 客户端地址
  63. * @param addr_len 客户端地址长度
  64. */
  65. void onRead_l(bool is_server_fd, const PeerIdType &id, const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len);
  66. /**
  67. * @brief 根据对端信息获取或创建一个会话
  68. */
  69. const Session::Ptr& getOrCreateSession(const PeerIdType &id, struct sockaddr *addr, int addr_len, bool &is_new);
  70. /**
  71. * @brief 创建一个会话, 同时进行必要的设置
  72. */
  73. const Session::Ptr& createSession(const PeerIdType &id, struct sockaddr *addr, int addr_len);
  74. /**
  75. * @brief 创建socket
  76. */
  77. Socket::Ptr createSocket(const EventPoller::Ptr &poller);
  78. private:
  79. bool _cloned = false;
  80. Socket::Ptr _socket;
  81. std::shared_ptr<Timer> _timer;
  82. Socket::onCreateSocket _on_create_socket;
  83. //cloned server共享主server的session map,防止数据在不同server间漂移
  84. std::shared_ptr<std::recursive_mutex> _session_mutex;
  85. std::shared_ptr<std::unordered_map<PeerIdType, SessionHelper::Ptr> > _session_map;
  86. //主server持有cloned server的引用
  87. std::unordered_map<EventPoller *, Ptr> _cloned_server;
  88. std::function<SessionHelper::Ptr(const UdpServer::Ptr&, const Socket::Ptr&)> _session_alloc;
  89. // 对象个数统计
  90. ObjectStatistic<UdpServer> _statistic;
  91. };
  92. } // namespace toolkit
  93. #endif // TOOLKIT_NETWORK_UDPSERVER_H