Network: Replace some String by std::string

This commit is contained in:
Lynix 2020-07-25 14:32:53 +02:00
parent d298e93c13
commit aff617f147
13 changed files with 58 additions and 47 deletions

View File

@ -51,7 +51,7 @@ namespace Nz
bool CheckEvents(ENetEvent* event);
ENetPeer* Connect(const IpAddress& remoteAddress, std::size_t channelCount = 0, UInt32 data = 0);
ENetPeer* Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr, std::size_t channelCount = 0, UInt32 data = 0);
ENetPeer* Connect(const std::string& hostName, NetProtocol protocol = NetProtocol_Any, const std::string& service = "http", ResolveError* error = nullptr, std::size_t channelCount = 0, UInt32 data = 0);
inline bool Create(NetProtocol protocol, UInt16 port, std::size_t peerCount, std::size_t channelCount = 0);
bool Create(const IpAddress& listenAddress, std::size_t peerCount, std::size_t channelCount = 0);

View File

@ -47,7 +47,7 @@ namespace Nz
inline IPv4 ToIPv4() const;
inline IPv6 ToIPv6() const;
String ToString() const;
std::string ToString() const;
inline UInt32 ToUInt32() const;
inline explicit operator bool() const;
@ -55,8 +55,8 @@ namespace Nz
IpAddress& operator=(const IpAddress&) = default;
IpAddress& operator=(IpAddress&&) noexcept = default;
static String ResolveAddress(const IpAddress& address, String* service = nullptr, ResolveError* error = nullptr);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& protocol = "http", ResolveError* error = nullptr);
static std::string ResolveAddress(const IpAddress& address, std::string* service = nullptr, ResolveError* error = nullptr);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const std::string& hostname, const std::string& protocol = "http", ResolveError* error = nullptr);
inline friend std::ostream& operator<<(std::ostream& out, const IpAddress& address);
@ -91,7 +91,7 @@ namespace Nz
IpAddress address;
NetProtocol protocol;
SocketType socketType;
String canonicalName;
std::string canonicalName;
};
}

View File

@ -199,7 +199,6 @@ namespace Nz
* \param out The stream
* \param address The address to output
*/
inline std::ostream& operator<<(std::ostream& out, const IpAddress& address)
{
out << "IpAddress(" << address.ToString() << ')';

View File

@ -37,7 +37,7 @@ namespace Nz
inline void Close();
bool Connect(const IpAddress& remoteAddress);
bool Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr);
bool Connect(const std::string& hostName, NetProtocol protocol = NetProtocol_Any, const std::string& service = "http", ResolveError* error = nullptr);
inline void Disconnect();
inline IpAddress GetBoundAddress() const;

View File

@ -12,6 +12,7 @@
#include <Nazara/Core/Stream.hpp>
#include <Nazara/Network/AbstractSocket.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <string>
namespace Nz
{
@ -28,7 +29,7 @@ namespace Nz
~TcpClient() = default;
SocketState Connect(const IpAddress& remoteAddress);
SocketState Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr);
SocketState Connect(const std::string& hostName, NetProtocol protocol = NetProtocol_Any, const std::string& service = "http", ResolveError* error = nullptr);
inline void Disconnect();
void EnableLowDelay(bool lowDelay);

View File

@ -115,7 +115,7 @@ namespace Nz
return &peer;
}
ENetPeer* ENetHost::Connect(const String& hostName, NetProtocol protocol, const String& service, ResolveError* error, std::size_t channelCount, UInt32 data)
ENetPeer* ENetHost::Connect(const std::string& hostName, NetProtocol protocol, const std::string& service, ResolveError* error, std::size_t channelCount, UInt32 data)
{
std::vector<HostnameInfo> results = IpAddress::ResolveHostname(protocol, hostName, service, error);
if (results.empty())

View File

@ -4,10 +4,10 @@
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Network/Algorithm.hpp>
#include <algorithm>
#include <limits>
#include <sstream>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Network/Win32/IpAddressImpl.hpp>
@ -99,9 +99,9 @@ namespace Nz
* \remark Produces a NazaraAssert if internal protocol is invalid (should never happen)
*/
String IpAddress::ToString() const
std::string IpAddress::ToString() const
{
StringStream stream;
std::ostringstream stream;
if (m_isValid)
{
@ -176,7 +176,7 @@ namespace Nz
stream << ':' << m_port;
}
return stream;
return stream.str();
}
/*!
@ -189,12 +189,11 @@ namespace Nz
*
* \remark Produces a NazaraAssert if address is invalid
*/
String IpAddress::ResolveAddress(const IpAddress& address, String* service, ResolveError* error)
std::string IpAddress::ResolveAddress(const IpAddress& address, std::string* service, ResolveError* error)
{
NazaraAssert(address.IsValid(), "Invalid address");
String hostname;
std::string hostname;
IpAddressImpl::ResolveAddress(address, &hostname, service, error);
return hostname;
@ -211,8 +210,7 @@ namespace Nz
*
* \remark Produces a NazaraAssert if net protocol is set to unknown
*/
std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const String& hostname, const String& service, ResolveError* error)
std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const std::string& hostname, const std::string& service, ResolveError* error)
{
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");

View File

@ -15,12 +15,12 @@ namespace Nz
{
using addrinfoImpl = addrinfo;
int GetAddressInfo(const String& hostname, const String& service, const addrinfoImpl* hints, addrinfoImpl** results)
int GetAddressInfo(const std::string& hostname, const std::string& service, const addrinfoImpl* hints, addrinfoImpl** results)
{
return getaddrinfo(hostname.GetConstBuffer(), service.GetConstBuffer(), hints, results);
return getaddrinfo(hostname.c_str(), service.c_str(), hints, results);
}
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, int flags)
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, std::string* hostname, std::string* service, int flags)
{
std::array<char, NI_MAXHOST> hostnameBuffer;
std::array<char, NI_MAXSERV> serviceBuffer;
@ -29,10 +29,10 @@ namespace Nz
if (result == 0)
{
if (hostname)
hostname->Set(hostnameBuffer.data());
hostname->assign(hostnameBuffer.data());
if (service)
service->Set(serviceBuffer.data());
service->assign(serviceBuffer.data());
}
return result;
@ -117,7 +117,7 @@ namespace Nz
return IpAddress(ip6Address, ntohs(addressv6->sin6_port));
}
bool IpAddressImpl::ResolveAddress(const IpAddress& ipAddress, String* hostname, String* service, ResolveError* error)
bool IpAddressImpl::ResolveAddress(const IpAddress& ipAddress, std::string* hostname, std::string* service, ResolveError* error)
{
SockAddrBuffer socketAddress;
socklen_t socketAddressLen = ToSockAddr(ipAddress, socketAddress.data());
@ -136,7 +136,7 @@ namespace Nz
return true;
}
std::vector<HostnameInfo> IpAddressImpl::ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error)
std::vector<HostnameInfo> IpAddressImpl::ResolveHostname(NetProtocol procol, const std::string& hostname, const std::string& service, ResolveError* error)
{
std::vector<HostnameInfo> results;
@ -165,7 +165,7 @@ namespace Nz
{
HostnameInfo result;
result.address = FromAddrinfo(p);
result.canonicalName = String::Unicode(p->ai_canonname);
result.canonicalName = p->ai_canonname;
result.protocol = TranslatePFToNetProtocol(p->ai_family);
result.socketType = TranslateSockToNetProtocol(p->ai_socktype);

View File

@ -8,6 +8,7 @@
#define NAZARA_IPADDRESSIMPL_HPP
#include <Nazara/Network/IpAddress.hpp>
#include <string>
#include <netdb.h>
#include <netinet/in.h>
@ -26,8 +27,8 @@ namespace Nz
static IpAddress FromSockAddr(const sockaddr_in* addressv4);
static IpAddress FromSockAddr(const sockaddr_in6* addressv6);
static bool ResolveAddress(const IpAddress& ipAddress, String* hostname, String* service, ResolveError* error);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error);
static bool ResolveAddress(const IpAddress& ipAddress, std::string* hostname, std::string* service, ResolveError* error);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const std::string& hostname, const std::string& service, ResolveError* error);
static socklen_t ToSockAddr(const IpAddress& ipAddress, void* buffer);
static NetProtocol TranslatePFToNetProtocol(int family);

View File

@ -70,7 +70,7 @@ namespace Nz
* \param error Optional argument to get the error
*/
bool RUdpConnection::Connect(const String& hostName, NetProtocol protocol, const String& service, ResolveError* error)
bool RUdpConnection::Connect(const std::string& hostName, NetProtocol protocol, const std::string& service, ResolveError* error)
{
std::vector<HostnameInfo> results = IpAddress::ResolveHostname(protocol, hostName, service, error);
if (results.empty())

View File

@ -72,7 +72,7 @@ namespace Nz
* \param error Optional argument to get the error
*/
SocketState TcpClient::Connect(const String& hostName, NetProtocol protocol, const String& service, ResolveError* error)
SocketState TcpClient::Connect(const std::string& hostName, NetProtocol protocol, const std::string& service, ResolveError* error)
{
UpdateState(SocketState_Resolving);
std::vector<HostnameInfo> results = IpAddress::ResolveHostname(protocol, hostName, service, error);

View File

@ -5,6 +5,7 @@
#include <Nazara/Network/Win32/IpAddressImpl.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Network/Win32/SocketImpl.hpp>
#include <cstring>
#include <Nazara/Network/Debug.hpp>
@ -25,12 +26,12 @@ namespace Nz
#if NAZARA_CORE_WINDOWS_NT6
using addrinfoImpl = addrinfoW;
int GetAddressInfo(const String& hostname, const String& service, const addrinfoImpl* hints, addrinfoImpl** results)
int GetAddressInfo(const std::string& hostname, const std::string& service, const addrinfoImpl* hints, addrinfoImpl** results)
{
return GetAddrInfoW(hostname.GetWideString().c_str(), service.GetWideString().c_str(), hints, results);
return GetAddrInfoW(ToWideString(hostname).c_str(), ToWideString(service).c_str(), hints, results);
}
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, std::string* hostname, std::string* service, INT flags)
{
std::array<wchar_t, NI_MAXHOST> hostnameBuffer;
std::array<wchar_t, NI_MAXSERV> serviceBuffer;
@ -39,10 +40,10 @@ namespace Nz
if (result == 0)
{
if (hostname)
*hostname = std::move(String::Unicode(hostnameBuffer.data()));
*hostname = FromWideString(hostnameBuffer.data());
if (service)
*service = std::move(String::Unicode(serviceBuffer.data()));
*service = FromWideString(serviceBuffer.data());
}
return result;
@ -52,15 +53,20 @@ namespace Nz
{
FreeAddrInfoW(results);
}
std::string TranslateCanonicalName(const wchar_t* str)
{
return FromWideString(str);
}
#else
using addrinfoImpl = addrinfo;
int GetAddressInfo(const String& hostname, const String& service, const addrinfoImpl* hints, addrinfoImpl** results)
int GetAddressInfo(const std::string& hostname, const std::string& service, const addrinfoImpl* hints, addrinfoImpl** results)
{
return getaddrinfo(hostname.GetConstBuffer(), service.GetConstBuffer(), hints, results);
return getaddrinfo(hostname.c_str(), service.c_str(), hints, results);
}
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, std::string* hostname, std::string* service, INT flags)
{
std::array<char, NI_MAXHOST> hostnameBuffer;
std::array<char, NI_MAXSERV> serviceBuffer;
@ -69,10 +75,10 @@ namespace Nz
if (result == 0)
{
if (hostname)
hostname->Set(hostnameBuffer.data());
hostname->assign(hostnameBuffer.data());
if (service)
service->Set(serviceBuffer.data());
service->assign(serviceBuffer.data());
}
return result;
@ -82,6 +88,11 @@ namespace Nz
{
freeaddrinfo(results);
}
std::string TranslateCanonicalName(const char* str)
{
return str;
}
#endif
}
@ -162,7 +173,7 @@ namespace Nz
return IpAddress(ipv6, ntohs(addressv6->sin6_port));
}
bool IpAddressImpl::ResolveAddress(const IpAddress& ipAddress, String* hostname, String* service, ResolveError* error)
bool IpAddressImpl::ResolveAddress(const IpAddress& ipAddress, std::string* hostname, std::string* service, ResolveError* error)
{
SockAddrBuffer socketAddress;
socklen_t socketAddressLen = ToSockAddr(ipAddress, socketAddress.data());
@ -181,7 +192,7 @@ namespace Nz
return true;
}
std::vector<HostnameInfo> IpAddressImpl::ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error)
std::vector<HostnameInfo> IpAddressImpl::ResolveHostname(NetProtocol procol, const std::string& hostname, const std::string& service, ResolveError* error)
{
std::vector<HostnameInfo> results;
@ -209,7 +220,7 @@ namespace Nz
{
HostnameInfo result;
result.address = FromAddrinfo(p);
result.canonicalName = String::Unicode(p->ai_canonname);
result.canonicalName = Detail::TranslateCanonicalName(p->ai_canonname);
result.protocol = TranslatePFToNetProtocol(p->ai_family);
result.socketType = TranslateSockToNetProtocol(p->ai_socktype);

View File

@ -8,6 +8,7 @@
#define NAZARA_IPADDRESSIMPL_HPP
#include <Nazara/Network/IpAddress.hpp>
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>
@ -29,8 +30,8 @@ namespace Nz
static IpAddress FromSockAddr(const sockaddr_in* addressv4);
static IpAddress FromSockAddr(const sockaddr_in6* addressv6);
static bool ResolveAddress(const IpAddress& ipAddress, String* hostname, String* service, ResolveError* error);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error);
static bool ResolveAddress(const IpAddress& ipAddress, std::string* hostname, std::string* service, ResolveError* error);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const std::string& hostname, const std::string& service, ResolveError* error);
static socklen_t ToSockAddr(const IpAddress& ipAddress, void* buffer);
static NetProtocol TranslatePFToNetProtocol(int family);