-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add C++17 UDPServer ID handling #260
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI Review for src/Network/UdpServer.h: Code Review: Patch to UdpServer.hSummaryThis patch modifies the Detailed FeedbackCode OverviewThe patch introduces a Strengths
Areas for Improvement1.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,45 @@ | |
#ifndef TOOLKIT_NETWORK_UDPSERVER_H | ||
#define TOOLKIT_NETWORK_UDPSERVER_H | ||
|
||
#if __cplusplus >= 201703L | ||
#include <array> | ||
#include <string_view> | ||
#endif | ||
#include "Server.h" | ||
#include "Session.h" | ||
|
||
#define PEERTYPELEN (18) | ||
namespace toolkit { | ||
|
||
class UdpServer : public Server { | ||
public: | ||
using Ptr = std::shared_ptr<UdpServer>; | ||
#if __cplusplus >= 201703L | ||
class PeerIdType : public std::array<char, PEERTYPELEN> { | ||
public: | ||
PeerIdType() { | ||
fill(' '); | ||
} | ||
PeerIdType(size_t len, char fillChar) { | ||
if (len > size()) { | ||
len = size(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest removing the PeerIdType(size_t len, char fillChar) constructor,
|
||
} | ||
fill(fillChar); | ||
} | ||
bool operator==(const PeerIdType &that) const { | ||
return (as<uint64_t>(0) == that.as<uint64_t>(0)) && (as<uint64_t>(8) == that.as<uint64_t>(8)) && (as<uint16_t>(16) == that.as<uint16_t>(16)); | ||
} | ||
|
||
private: | ||
template <class T> | ||
const T& as(size_t offset) const { | ||
return *(reinterpret_cast<const T *>(data() + offset)); | ||
} | ||
}; | ||
#else | ||
using PeerIdType = std::string; | ||
#endif | ||
|
||
using Ptr = std::shared_ptr<UdpServer>; | ||
using onCreateSocket = std::function<Socket::Ptr(const EventPoller::Ptr &, const Buffer::Ptr &, struct sockaddr *, int)>; | ||
|
||
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> {}(std::string_view(v.data(), v.size())); } | ||
}; | ||
using SessionMapType = std::unordered_map<PeerIdType, SessionHelper::Ptr, PeerIdHash>; | ||
#else | ||
using SessionMapType = std::unordered_map<PeerIdType, SessionHelper::Ptr>; | ||
#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<std::recursive_mutex> _session_mutex; | ||
std::shared_ptr<std::unordered_map<PeerIdType, SessionHelper::Ptr> > _session_map; | ||
std::shared_ptr<SessionMapType> _session_map; | ||
//主server持有cloned server的引用 [AUTO-TRANSLATED:04a6403a] | ||
//Main server holds a reference to the cloned server | ||
std::unordered_map<EventPoller *, Ptr> _cloned_server; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI Review for src/Network/UdpServer.cpp:
Code Review: Patch to src/Network/UdpServer.cpp
Summary
This patch modifies the
UdpServer
class in the ZLToolKit project to improve UDP session ID handling using C++17 features. The primary changes involve usingstd::array
instead ofstd::string
for PeerIdType and usingstd::unordered_map
with a custom type alias for better type safety.Detailed Feedback
Code Overview
The patch refactors the
PeerIdType
to usestd::array<uint8_t, PEERTYPELEN>
which is more efficient and type-safe than the previousstd::string
. It also introduces a type aliasSessionMapType
forstd::unordered_map<PeerIdType, SessionHelper::Ptr>
, improving readability and maintainability. The changes aim to enhance performance and code clarity.Strengths
std::array
forPeerIdType
eliminates potential issues related to string manipulation and improves type safety.SessionMapType
alias makes the code easier to read and understand.std::array
might offer slight performance gains compared tostd::string
in this context, especially for memory allocation and access.Areas for Improvement
1.
makeSockId
Functionassert(0)
in thedefault
case of theswitch
statement is not ideal. While it indicates an error, it abruptly terminates the program. A more robust approach would be to throw an exception or return an error code.assert(0)
with a more graceful error handling mechanism. For example, throw astd::runtime_error
exception with a descriptive message.2. Error Handling and Logging
ErrorL
,WarnL
) to categorize messages.3. Const Correctness
makeSockId
function takessockaddr *addr
as a non-const parameter, even though it doesn't modify the address.const sockaddr *addr
to improve const correctness.4.
SessionMapType
ScopeSessionMapType
alias is defined locally within theUdpServer
class. If this type is used elsewhere, it might be better to define it in a more accessible location (e.g., a header file).SessionMapType
alias to a header file if it's used outside ofUdpServer.cpp
.Conclusion
The patch is a positive step towards improving the codebase. The use of
std::array
and theSessionMapType
alias enhances type safety and readability. However, addressing the suggested improvements in error handling, logging, and const correctness will further enhance the robustness and maintainability of the code. The suggestion to potentially moveSessionMapType
depends on its usage elsewhere in the project.TRANS_BY_GITHUB_AI_ASSISTANT