From 54a63d92c497af21bc3effb18844b6336c696cfa Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 5 Dec 2024 21:55:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96UDP=E9=93=BE=E6=8E=A5id?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Network/UdpServer.cpp | 8 +++----- src/Network/UdpServer.h | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Network/UdpServer.cpp b/src/Network/UdpServer.cpp index 45a78e03..963f0ccc 100644 --- a/src/Network/UdpServer.cpp +++ b/src/Network/UdpServer.cpp @@ -25,7 +25,6 @@ static UdpServer::PeerIdType makeSockId(sockaddr *addr, int) { UdpServer::PeerIdType ret; switch (addr->sa_family) { case AF_INET : { - ret.resize(18); ret[0] = ((struct sockaddr_in *) addr)->sin_port >> 8; ret[1] = ((struct sockaddr_in *) addr)->sin_port & 0xFF; //ipv4地址统一转换为ipv6方式处理 [AUTO-TRANSLATED:ad7cf8c3] @@ -35,13 +34,12 @@ static UdpServer::PeerIdType makeSockId(sockaddr *addr, int) { return ret; } case AF_INET6 : { - ret.resize(18); ret[0] = ((struct sockaddr_in6 *) addr)->sin6_port >> 8; ret[1] = ((struct sockaddr_in6 *) addr)->sin6_port & 0xFF; memcpy(&ret[2], &(((struct sockaddr_in6 *)addr)->sin6_addr), 16); return ret; } - default: assert(0); return ""; + default: throw std::invalid_argument("invalid sockaddr address"); } } @@ -78,7 +76,7 @@ void UdpServer::start_l(uint16_t port, const std::string &host) { //主server才创建session map,其他cloned server共享之 [AUTO-TRANSLATED:113cf4fd] //Only the main server creates a session map, other cloned servers share it _session_mutex = std::make_shared(); - _session_map = std::make_shared >(); + _session_map = std::make_shared(); // 新建一个定时器定时管理这些 udp 会话,这些对象只由主server做超时管理,cloned server不管理 [AUTO-TRANSLATED:d20478a2] //Create a timer to manage these udp sessions periodically, these objects are only managed by the main server, cloned servers do not manage them @@ -207,7 +205,7 @@ void UdpServer::onManagerSession() { std::lock_guard lock(*_session_mutex); //拷贝map,防止遍历时移除对象 [AUTO-TRANSLATED:ebbc7595] //Copy the map to prevent objects from being removed during traversal - copy_map = std::make_shared >(*_session_map); + copy_map = std::make_shared(*_session_map); } auto lam = [copy_map]() { for (auto &pr : *copy_map) { diff --git a/src/Network/UdpServer.h b/src/Network/UdpServer.h index 63c6129f..7e4eb06d 100644 --- a/src/Network/UdpServer.h +++ b/src/Network/UdpServer.h @@ -11,6 +11,7 @@ #ifndef TOOLKIT_NETWORK_UDPSERVER_H #define TOOLKIT_NETWORK_UDPSERVER_H +#include #include "Server.h" #include "Session.h" @@ -18,8 +19,22 @@ namespace toolkit { class UdpServer : public Server { public: + class PeerIdType : public std::array { + public: + bool operator==(const PeerIdType &that) const { + return as(0) == that.as(0) && + as(8) == that.as(8) && + as(16) == that.as(16); + } + + private: + template + const T& as(size_t offset) const { + return *(reinterpret_cast(data() + offset)); + } + }; + using Ptr = std::shared_ptr; - using PeerIdType = std::string; using onCreateSocket = std::function; explicit UdpServer(const EventPoller::Ptr &poller = nullptr); @@ -75,6 +90,11 @@ class UdpServer : public Server { virtual void cloneFrom(const UdpServer &that); private: + struct PeerIdHash { + size_t operator()(const PeerIdType &v) const noexcept { return std::hash {}(std::string_view(v.data(), v.size())); } + }; + using SessionMapType = std::unordered_map; + /** * @brief 开始udp server * @param port 本机端口,0则随机 @@ -150,7 +170,7 @@ class UdpServer : public Server { //cloned server共享主server的session map,防止数据在不同server间漂移 [AUTO-TRANSLATED:9a149e52] //Cloned server shares the session map with the main server, preventing data drift between different servers std::shared_ptr _session_mutex; - std::shared_ptr > _session_map; + std::shared_ptr _session_map; //主server持有cloned server的引用 [AUTO-TRANSLATED:04a6403a] //Main server holds a reference to the cloned server std::unordered_map _cloned_server;