Documentation for module: Network

Former-commit-id: d36042138d0883eb118cc9a70f94f3522214dd46
This commit is contained in:
Gawaboumga
2016-05-30 14:22:31 +02:00
parent 96b958d655
commit 0a99058c4d
25 changed files with 1368 additions and 25 deletions

View File

@@ -9,11 +9,22 @@
namespace Nz
{
/*!
* \brief Constructs a IpAddress object by default
*/
inline IpAddress::IpAddress() :
m_isValid(false)
{
}
/*!
* \brief Constructs a IpAddress object with an IP and a port
*
* \param ip IPv4 address
* \param port Port of the IP
*/
inline IpAddress::IpAddress(const IPv4& ip, UInt16 port) :
m_ipv4(ip),
m_protocol(NetProtocol_IPv4),
@@ -22,6 +33,13 @@ namespace Nz
{
}
/*!
* \brief Constructs a IpAddress object with an IP and a port
*
* \param ip IPv6 address
* \param port Port of the IP
*/
inline IpAddress::IpAddress(const IPv6& ip, UInt16 port) :
m_ipv6(ip),
m_protocol(NetProtocol_IPv6),
@@ -30,46 +48,100 @@ namespace Nz
{
}
/*!
* \brief Constructs a IpAddress object with an IP and a port
*
* \param ip IPv4 address (a.b.c.d)
* \param port Port of the IP
*/
inline IpAddress::IpAddress(const UInt8& a, const UInt8& b, const UInt8& c, const UInt8& d, UInt16 port) :
IpAddress(IPv4{a, b, c, d}, port)
{
}
/*!
* \brief Constructs a IpAddress object with an IP and a port
*
* \param ip IPv6 address (a.b.c.d.e.f.g.h)
* \param port Port of the IP
*/
inline IpAddress::IpAddress(const UInt16& a, const UInt16& b, const UInt16& c, const UInt16& d, const UInt16& e, const UInt16& f, const UInt16& g, const UInt16& h, UInt16 port) :
IpAddress(IPv6{a, b, c, d, e, f, g, h}, port)
{
}
/*!
* Constructs a IpAddress object with a C-string
*
* \param address Hostname or textual IP address
*/
inline IpAddress::IpAddress(const char* address)
{
BuildFromAddress(address);
}
/*!
* Constructs a IpAddress object with a string
*
* \param address Hostname or textual IP address
*/
inline IpAddress::IpAddress(const String& address)
{
BuildFromAddress(address.GetConstBuffer());
}
/*!
* \brief Gets the port
* \return Port attached to the IP address
*/
inline UInt16 IpAddress::GetPort() const
{
return m_port;
}
/*!
* \brief Gets the net protocol
* \return Protocol attached to the IP address
*/
inline NetProtocol IpAddress::GetProtocol() const
{
return m_protocol;
}
/*!
* \brief Checks whether the IP address is valid
* \return true If successful
*/
inline bool IpAddress::IsValid() const
{
return m_isValid;
}
/*!
* \brief Sets the port
*
* \param port Port attached to the IP address
*/
inline void IpAddress::SetPort(UInt16 port)
{
m_port = port;
}
/*!
* \brief Converts IpAddress to IPv4
* \return Corresponding IPv4
*
* \remark Produces a NazaraAssert if net protocol is not IPv4
*/
inline IpAddress::IPv4 IpAddress::ToIPv4() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv4, "Address is not a valid IPv4");
@@ -77,6 +149,13 @@ namespace Nz
return m_ipv4;
}
/*!
* \brief Converts IpAddress to IPv6
* \return Corresponding IPv6
*
* \remark Produces a NazaraAssert if net protocol is not IPv6
*/
inline IpAddress::IPv6 IpAddress::ToIPv6() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv6, "IP is not a valid IPv6");
@@ -84,6 +163,13 @@ namespace Nz
return m_ipv6;
}
/*!
* \brief Converts IpAddress to UInt32
* \return Corresponding UInt32
*
* \remark Produces a NazaraAssert if net protocol is not IPv4
*/
inline UInt32 IpAddress::ToUInt32() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv4, "Address is not a valid IPv4");
@@ -94,17 +180,40 @@ namespace Nz
UInt32(m_ipv4[3]) << 0;
}
/*!
* \brief Converts IpAddress to boolean
* \return true If IpAddress is valid
*
* \see IsValid
*/
inline IpAddress::operator bool() const
{
return IsValid();
}
/*!
* \brief Output operator
* \return The stream
*
* \param out The stream
* \param address The address to output
*/
inline std::ostream& operator<<(std::ostream& out, const IpAddress& address)
{
out << "IpAddress(" << address.ToString() << ')';
return out;
}
/*!
* \brief Compares the IpAddress to other one
* \return true if the ip addresses are the same
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator==(const IpAddress& first, const IpAddress& second)
{
// We need to check the validity of each address before comparing them
@@ -146,11 +255,27 @@ namespace Nz
return true;
}
/*!
* \brief Compares the IpAddress to other one
* \return false if the ip addresses are the same
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator!=(const IpAddress& first, const IpAddress& second)
{
return !operator==(first, second);
}
/*!
* \brief Compares the IpAddress to other one
* \return true if this ip address is inferior to the other one
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator<(const IpAddress& first, const IpAddress& second)
{
// If the second address is invalid, there's no way we're lower than it
@@ -196,16 +321,40 @@ namespace Nz
return false; //< Same address
}
/*!
* \brief Compares the IpAddress to other one
* \return true if this ip address is inferior or equal to the other one
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator<=(const IpAddress& first, const IpAddress& second)
{
return !operator<(second, first);
}
/*!
* \brief Compares the IpAddress to other one
* \return true if this ip address is greather to the other one
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator>(const IpAddress& first, const IpAddress& second)
{
return second < first;
}
/*!
* \brief Compares the IpAddress to other one
* \return true if this ip address is greather or equal to the other one
*
* \param first First ip address to compare
* \param second Second ip address to compare with
*/
inline bool operator>=(const IpAddress& first, const IpAddress& second)
{
return !operator<(first, second);
@@ -217,6 +366,13 @@ namespace std
template<>
struct hash<Nz::IpAddress>
{
/*!
* \brief Converts IpAddress to hash
* \return Hash of the IpAddress
*
* \param ip IpAddress to hash
*/
size_t operator()(const Nz::IpAddress& ip) const
{
if (!ip)
@@ -224,7 +380,7 @@ namespace std
// This is SDBM adapted for IP addresses, tested to generate the least collisions possible
// (It doesn't mean it cannot be improved though)
std::size_t h = 0;
std::size_t hash = 0;
switch (ip.GetProtocol())
{
case Nz::NetProtocol_Any:
@@ -233,20 +389,20 @@ namespace std
case Nz::NetProtocol_IPv4:
{
h = ip.ToUInt32() + (h << 6) + (h << 16) - h;
hash = ip.ToUInt32() + (hash << 6) + (hash << 16) - hash;
break;
}
case Nz::NetProtocol_IPv6:
{
Nz::IpAddress::IPv6 v6 = ip.ToIPv6();
for (std::size_t i = 0; i < v6.size(); i++)
h = v6[i] + (h << 6) + (h << 16) - h;
hash = v6[i] + (hash << 6) + (hash << 16) - hash;
break;
}
}
return ip.GetPort() + (h << 6) + (h << 16) - h;
return ip.GetPort() + (hash << 6) + (hash << 16) - hash;
}
};
}