From b4107bfc251f236c05cfa5fd513e51c077e9a8e3 Mon Sep 17 00:00:00 2001 From: hiker Date: Sun, 15 Apr 2018 00:32:30 +1000 Subject: [PATCH] Added a new constructor taking a string and port number (including test cases) - mostly useful for debugging. --- src/network/transport_address.cpp | 13 +++++++++++++ src/network/transport_address.hpp | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/network/transport_address.cpp b/src/network/transport_address.cpp index ee5b5fc61..8a0fee3fb 100644 --- a/src/network/transport_address.cpp +++ b/src/network/transport_address.cpp @@ -203,4 +203,17 @@ void TransportAddress::unitTesting() assert(t17.getIP() == (128u << 24)); assert(!t17.isLAN()); + // Test constructors + TransportAddress t18("128.0.0.0"); + assert(t18.getIP() == (128u << 24)); + assert(t18.getPort() == 0); + + TransportAddress t19("128.0.0.0:1"); + assert(t19.getIP() == (128u << 24)); + assert(t19.getPort() == 1); + + TransportAddress t20("128.0.0.0", 123); + assert(t20.getIP() == (128u << 24)); + assert(t20.getPort() == 123); + } // unitTesting diff --git a/src/network/transport_address.hpp b/src/network/transport_address.hpp index 4a9237e8c..fbe827094 100644 --- a/src/network/transport_address.hpp +++ b/src/network/transport_address.hpp @@ -58,7 +58,21 @@ public: } // TransportAddress(EnetAddress) // ------------------------------------------------------------------------ - /** Construct an IO address from a string in the format x.x.x.x:xxx. */ + /** Construct an IO address from a string in the format x.x.x.x with a + * port number. */ + TransportAddress(const std::string& str, uint16_t port_number) + { + std::vector ip = StringUtils::splitToUInt(str, '.'); + m_ip = 0; + m_port = 0; + if (ip.size() >= 4) + m_ip = (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; + + m_port = port_number; + } // TransportAddress(string of ip, port number) + + // ------------------------------------------------------------------------ + /** Construct an IO address from a string in the format x.x.x.x:x. */ TransportAddress(const std::string& str) { std::string combined = StringUtils::replace(str, ":", ".");