Added a new constructor taking a string and port number (including

test cases) - mostly useful for debugging.
This commit is contained in:
hiker 2018-04-15 00:32:30 +10:00
parent 6490551533
commit b4107bfc25
2 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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<uint32_t> 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, ":", ".");