Skip to content

Commit

Permalink
redefine undefined() in EndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lihuiba committed Dec 27, 2024
1 parent a6b533c commit 54a6d1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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)
Expand Down Expand Up @@ -97,6 +98,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());
Expand Down Expand Up @@ -169,7 +172,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();
}
};

Expand Down
2 changes: 1 addition & 1 deletion net/test/test-ipv6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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());
Expand Down

0 comments on commit 54a6d1d

Please sign in to comment.