From 80ac53162368b5e34cac92136ad9886935032b04 Mon Sep 17 00:00:00 2001 From: lihuiba Date: Wed, 25 Dec 2024 16:31:03 +0800 Subject: [PATCH] redefine undefined() in EndPoint --- net/socket.h | 12 +++++++++++- net/test/test-ipv6.cpp | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/net/socket.h b/net/socket.h index 415f4d79..af68f436 100644 --- a/net/socket.h +++ b/net/socket.h @@ -45,6 +45,7 @@ namespace net { public: union { in6_addr addr = {}; + // all data is in network byte order struct { uint16_t _1, _2, _3, _4, _5, _6; uint8_t a, b, c, d; }; } __attribute__((packed)); // For compatibility, the default constructor is still 0.0.0.0 (IPv4) @@ -96,6 +97,8 @@ namespace net { bool undefined() const { return mem_equal(V4Any()); } + void reset() { *this = IPAddr(); } + void clear() { reset(); } // Should ONLY be used for IPv4 address uint32_t to_nl() const { assert(is_ipv4()); @@ -167,7 +170,14 @@ namespace net { return !operator==(rhs); } bool undefined() const { - return addr.undefined(); + return addr.undefined() && port == 0; + } + void reset() { + addr.reset(); + port = 0; + } + void clear() { + reset(); } }; diff --git a/net/test/test-ipv6.cpp b/net/test/test-ipv6.cpp index b05d2f47..f41836ed 100644 --- a/net/test/test-ipv6.cpp +++ b/net/test/test-ipv6.cpp @@ -14,7 +14,7 @@ TEST(ipv6, endpoint) { c = photon::net::EndPoint("[::1]:8888"); EXPECT_FALSE(c.undefined()); c = photon::net::EndPoint("0.0.0.0:8888"); - EXPECT_TRUE(c.undefined()); + EXPECT_FALSE(c.undefined()); EXPECT_EQ(8888, c.port); c = photon::net::EndPoint("::", 8888); EXPECT_TRUE(!c.undefined());