mirror of
https://github.com/SFML/SFML.git
synced 2024-11-25 04:41:05 +08:00
Manually inline sf::IpAddress::resolve
This code is better expressed as a constructor since that's the only place where it's used.
This commit is contained in:
parent
76223b07a1
commit
d7f09c1167
@ -190,14 +190,6 @@ private:
|
|||||||
|
|
||||||
friend SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);
|
friend SFML_NETWORK_API bool operator <(const IpAddress& left, const IpAddress& right);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
|
||||||
/// \brief Resolve the given address string
|
|
||||||
///
|
|
||||||
/// \param address Address string
|
|
||||||
///
|
|
||||||
////////////////////////////////////////////////////////////
|
|
||||||
void resolve(const std::string& address);
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
|
@ -50,14 +50,49 @@ IpAddress::IpAddress() = default;
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
IpAddress::IpAddress(const std::string& address)
|
IpAddress::IpAddress(const std::string& address)
|
||||||
{
|
{
|
||||||
resolve(address);
|
if (address == "255.255.255.255")
|
||||||
|
{
|
||||||
|
// The broadcast address needs to be handled explicitly,
|
||||||
|
// because it is also the value returned by inet_addr on error
|
||||||
|
m_address = INADDR_BROADCAST;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address == "0.0.0.0")
|
||||||
|
{
|
||||||
|
m_address = INADDR_ANY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
|
||||||
|
sf::Uint32 ip = inet_addr(address.c_str());
|
||||||
|
if (ip != INADDR_NONE)
|
||||||
|
{
|
||||||
|
m_address = ip;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not a valid address, try to convert it as a host name
|
||||||
|
addrinfo hints;
|
||||||
|
std::memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = AF_INET;
|
||||||
|
addrinfo* result = nullptr;
|
||||||
|
if (getaddrinfo(address.c_str(), nullptr, &hints, &result) == 0 && result)
|
||||||
|
{
|
||||||
|
sockaddr_in sin;
|
||||||
|
std::memcpy(&sin, result->ai_addr, sizeof(*result->ai_addr));
|
||||||
|
ip = sin.sin_addr.s_addr;
|
||||||
|
freeaddrinfo(result);
|
||||||
|
m_address = ip;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
IpAddress::IpAddress(const char* address)
|
IpAddress::IpAddress(const char* address) :
|
||||||
|
IpAddress(std::string(address))
|
||||||
{
|
{
|
||||||
resolve(address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -152,52 +187,6 @@ IpAddress IpAddress::getPublicAddress(Time timeout)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
|
||||||
void IpAddress::resolve(const std::string& address)
|
|
||||||
{
|
|
||||||
m_address = std::nullopt;
|
|
||||||
|
|
||||||
if (address == "255.255.255.255")
|
|
||||||
{
|
|
||||||
// The broadcast address needs to be handled explicitly,
|
|
||||||
// because it is also the value returned by inet_addr on error
|
|
||||||
m_address = INADDR_BROADCAST;
|
|
||||||
}
|
|
||||||
else if (address == "0.0.0.0")
|
|
||||||
{
|
|
||||||
m_address = INADDR_ANY;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Try to convert the address as a byte representation ("xxx.xxx.xxx.xxx")
|
|
||||||
Uint32 ip = inet_addr(address.c_str());
|
|
||||||
if (ip != INADDR_NONE)
|
|
||||||
{
|
|
||||||
m_address = ip;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Not a valid address, try to convert it as a host name
|
|
||||||
addrinfo hints;
|
|
||||||
std::memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = AF_INET;
|
|
||||||
addrinfo* result = nullptr;
|
|
||||||
if (getaddrinfo(address.c_str(), nullptr, &hints, &result) == 0)
|
|
||||||
{
|
|
||||||
if (result)
|
|
||||||
{
|
|
||||||
sockaddr_in sin;
|
|
||||||
std::memcpy(&sin, result->ai_addr, sizeof(*result->ai_addr));
|
|
||||||
ip = sin.sin_addr.s_addr;
|
|
||||||
freeaddrinfo(result);
|
|
||||||
m_address = ip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool operator ==(const IpAddress& left, const IpAddress& right)
|
bool operator ==(const IpAddress& left, const IpAddress& right)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user