From f431aca72ea1b43b6b6f7daad6f067dffc84bba2 Mon Sep 17 00:00:00 2001 From: PioLing <964472638@qq.com> Date: Fri, 6 Dec 2024 15:49:40 +0800 Subject: [PATCH 1/2] Adding C++17 UDPServer ID handling --- src/Network/UdpServer.cpp | 10 ++++------ src/Network/UdpServer.h | 42 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Network/UdpServer.cpp b/src/Network/UdpServer.cpp index 45a78e03..9b28cc65 100644 --- a/src/Network/UdpServer.cpp +++ b/src/Network/UdpServer.cpp @@ -22,10 +22,9 @@ static const uint8_t s_in6_addr_maped[] static constexpr auto kUdpDelayCloseMS = 3 * 1000; static UdpServer::PeerIdType makeSockId(sockaddr *addr, int) { - UdpServer::PeerIdType ret; + UdpServer::PeerIdType ret(PEERTYPELEN, ' '); 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: assert(0); return ret; } } @@ -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..a6701c0d 100644 --- a/src/Network/UdpServer.h +++ b/src/Network/UdpServer.h @@ -11,15 +11,45 @@ #ifndef TOOLKIT_NETWORK_UDPSERVER_H #define TOOLKIT_NETWORK_UDPSERVER_H +#if __cplusplus >= 201703L +#include +#include +#endif #include "Server.h" #include "Session.h" +#define PEERTYPELEN (18) namespace toolkit { class UdpServer : public Server { public: - using Ptr = std::shared_ptr; +#if __cplusplus >= 201703L + class PeerIdType : public std::array { + public: + PeerIdType() { + fill(' '); + } + PeerIdType(size_t len, char fillChar) { + if (len > size()) { + len = size(); + } + fill(fillChar); + } + 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)); + } + }; +#else using PeerIdType = std::string; +#endif + + using Ptr = std::shared_ptr; using onCreateSocket = std::function; explicit UdpServer(const EventPoller::Ptr &poller = nullptr); @@ -75,6 +105,14 @@ class UdpServer : public Server { virtual void cloneFrom(const UdpServer &that); private: +#if __cplusplus >= 201703L + 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; +#else + using SessionMapType = std::unordered_map; +#endif /** * @brief 开始udp server * @param port 本机端口,0则随机 @@ -150,7 +188,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; From 680bb97252a36df3c4a7390c02742f3817a94c4a Mon Sep 17 00:00:00 2001 From: PioLing <964472638@qq.com> Date: Mon, 9 Dec 2024 17:55:29 +0800 Subject: [PATCH 2/2] Improve the UDP server encapsulation and remove redundant constructor implementations --- src/Network/UdpServer.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Network/UdpServer.h b/src/Network/UdpServer.h index a6701c0d..c4f27258 100644 --- a/src/Network/UdpServer.h +++ b/src/Network/UdpServer.h @@ -30,10 +30,10 @@ class UdpServer : public Server { fill(' '); } PeerIdType(size_t len, char fillChar) { - if (len > size()) { - len = size(); - } - fill(fillChar); + //if (len > size()) { + // len = size(); + //} + //fill(fillChar); } bool operator==(const PeerIdType &that) const { return (as(0) == that.as(0)) && (as(8) == that.as(8)) && (as(16) == that.as(16)); @@ -45,8 +45,15 @@ class UdpServer : public Server { return *(reinterpret_cast(data() + offset)); } }; + + 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; + #else using PeerIdType = std::string; + using SessionMapType = std::unordered_map; #endif using Ptr = std::shared_ptr; @@ -105,14 +112,6 @@ class UdpServer : public Server { virtual void cloneFrom(const UdpServer &that); private: -#if __cplusplus >= 201703L - 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; -#else - using SessionMapType = std::unordered_map; -#endif /** * @brief 开始udp server * @param port 本机端口,0则随机