Unverified Commit 62ed986a authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #285 from knowledge4igor/code_refactoring_1

Code refactoring: move port value convertion to string to Port class
parents 1931d904 7699e9d2
......@@ -34,6 +34,7 @@ public:
bool isReserved() const;
bool isUsed() const;
std::string toString() const;
static constexpr uint16_t min() {
return std::numeric_limits<uint16_t>::min();
......
......@@ -384,16 +384,9 @@ Connection::connect(Address addr)
hints.ai_flags = 0;
hints.ai_protocol = 0;
auto host = addr.host();
/* We rely on the fact that a string literal is an lvalue const char[N] */
static constexpr size_t MaxPortLen = sizeof("65535");
char port[MaxPortLen];
std::fill(port, port + MaxPortLen, 0);
std::snprintf(port, MaxPortLen, "%d", static_cast<uint16_t>(addr.port()));
TRY(::getaddrinfo(host.c_str(), port, &hints, &addrs));
const auto& host = addr.host();
const auto& port = addr.port().toString();
TRY(::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs));
int sfd = -1;
......
......@@ -33,6 +33,11 @@ Port::isUsed() const {
return false;
}
std::string
Port::toString() const {
return std::to_string(port);
}
Ipv4::Ipv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
: a(a)
, b(b)
......@@ -111,6 +116,9 @@ Address::init(const std::string& addr) {
throw std::invalid_argument("Invalid address");
host_ = addr.substr(0, pos);
if (host_ == "*") {
host_ = "0.0.0.0";
}
char *end;
const std::string portPart = addr.substr(pos + 1);
......
......@@ -153,20 +153,10 @@ Listener::bind(const Address& address) {
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
auto host = addr_.host();
if (host == "*") {
host = "0.0.0.0";
}
/* We rely on the fact that a string literal is an lvalue const char[N] */
static constexpr size_t MaxPortLen = sizeof("65535");
char port[MaxPortLen];
std::fill(port, port + MaxPortLen, 0);
std::snprintf(port, MaxPortLen, "%d", static_cast<uint16_t>(addr_.port()));
const auto& host = addr_.host();
const auto& port = addr_.port().toString();
struct addrinfo *addrs;
TRY(::getaddrinfo(host.c_str(), port, &hints, &addrs));
TRY(::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs));
int fd = -1;
......
......@@ -17,11 +17,13 @@ TEST(net_test, port_creation)
ASSERT_FALSE(port1.isReserved());
uint16_t value1 = port1;
ASSERT_EQ(value1, 3000);
ASSERT_EQ(port1.toString(), "3000");
Port port2(80);
ASSERT_TRUE(port2.isReserved());
uint16_t value2 = port2;
ASSERT_EQ(value2, 80);
ASSERT_EQ(port2.toString(), "80");
}
TEST(net_test, address_creation)
......@@ -42,6 +44,10 @@ TEST(net_test, address_creation)
Address address4(Ipv4::any(), Port(8080));
ASSERT_EQ(address4.host(), "0.0.0.0");
ASSERT_EQ(address4.port(), 8080);
Address address5("*:8080");
ASSERT_EQ(address4.host(), "0.0.0.0");
ASSERT_EQ(address4.port(), 8080);
}
TEST(net_test, invalid_address)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment