Skip to content

Commit

Permalink
Improve the UDP server encapsulation and remove redundant constructor…
Browse files Browse the repository at this point in the history
… implementations
  • Loading branch information
PioLing committed Dec 9, 2024
1 parent f431aca commit 680bb97
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Network/UdpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<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));
Expand All @@ -45,8 +45,15 @@ class UdpServer : public Server {
return *(reinterpret_cast<const T *>(data() + offset));
}
};

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 PeerIdType = std::string;
using SessionMapType = std::unordered_map<PeerIdType, SessionHelper::Ptr>;
#endif

using Ptr = std::shared_ptr<UdpServer>;
Expand Down Expand Up @@ -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> {}(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则随机
Expand Down

0 comments on commit 680bb97

Please sign in to comment.