Former-commit-id: 8aec2e99cfa90bff230e2390d1bdc5908db41c27
This commit is contained in:
Lynix 2015-11-16 10:05:48 +01:00
commit 86da939520
14 changed files with 85 additions and 21 deletions

View File

@ -317,6 +317,7 @@ namespace Nz
{ {
inline SharedString(); inline SharedString();
inline SharedString(unsigned int strSize); inline SharedString(unsigned int strSize);
inline SharedString(unsigned int strSize, unsigned int strCapacity);
unsigned int capacity; unsigned int capacity;
unsigned int size; unsigned int size;

View File

@ -23,12 +23,20 @@ namespace Nz
} }
inline String::SharedString::SharedString(unsigned int strSize) : inline String::SharedString::SharedString(unsigned int strSize) :
capacity(strSize), capacity(strSize),
size(strSize), size(strSize),
string(new char[strSize + 1]) string(new char[strSize + 1])
{ {
string[strSize] = '\0'; string[strSize] = '\0';
} }
inline String::SharedString::SharedString(unsigned int strSize, unsigned int strCapacity) :
capacity(strCapacity),
size(strSize),
string(new char[strCapacity + 1])
{
string[strSize] = '\0';
}
} }
namespace std namespace std

View File

@ -30,8 +30,9 @@ namespace Nz
NetProtocol_Any, NetProtocol_Any,
NetProtocol_IPv4, NetProtocol_IPv4,
NetProtocol_IPv6, NetProtocol_IPv6,
NetProtocol_Unknown,
NetProtocol_Max = NetProtocol_IPv6 NetProtocol_Max = NetProtocol_Unknown
}; };
enum SocketError enum SocketError
@ -71,8 +72,9 @@ namespace Nz
SocketType_Raw, SocketType_Raw,
SocketType_TCP, SocketType_TCP,
SocketType_UDP, SocketType_UDP,
SocketType_Unknown,
SocketType_Max = SocketType_UDP SocketType_Max = SocketType_Unknown
}; };
} }

View File

@ -89,10 +89,9 @@ namespace Nz
struct HostnameInfo struct HostnameInfo
{ {
IpAddress address; IpAddress address;
NetProtocol protocol;
SocketType socketType;
String canonicalName; String canonicalName;
int flags;
int family; //< TODO: NetProtocol
int socketType; //< TODO: SocketType
}; };
} }

View File

@ -118,6 +118,7 @@ namespace Nz
switch (first.m_protocol) switch (first.m_protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
break; break;
case NetProtocol_IPv4: case NetProtocol_IPv4:
@ -167,6 +168,7 @@ namespace Nz
switch (first.m_protocol) switch (first.m_protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
break; break;
case NetProtocol_IPv4: case NetProtocol_IPv4:

View File

@ -31,11 +31,13 @@ namespace Nz
inline SocketState TcpServer::Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize) inline SocketState TcpServer::Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize)
{ {
NazaraAssert(protocol != NetProtocol_Any, "Any protocol not supported for Listen"); //< TODO NazaraAssert(protocol != NetProtocol_Any, "Any protocol not supported for Listen"); //< TODO
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
IpAddress any; IpAddress any;
switch (protocol) switch (protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point"); NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected; return SocketState_NotConnected;

View File

@ -30,6 +30,7 @@ namespace Nz
switch (m_protocol) switch (m_protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point"); NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected; return SocketState_NotConnected;
@ -48,6 +49,8 @@ namespace Nz
bool UdpSocket::Create(NetProtocol protocol) bool UdpSocket::Create(NetProtocol protocol)
{ {
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
return Open(protocol); return Open(protocol);
} }

View File

@ -4195,9 +4195,9 @@ namespace Nz
if (!m_sharedString.unique()) if (!m_sharedString.unique())
{ {
auto newSharedString = std::make_shared<SharedString>(GetSize()); auto newSharedString = std::make_shared<SharedString>(GetSize(), GetCapacity());
if (!discardContent) if (!discardContent)
std::memcpy(newSharedString->string.get(), GetConstBuffer(), GetSize()); std::memcpy(newSharedString->string.get(), GetConstBuffer(), GetSize()+1);
m_sharedString = std::move(newSharedString); m_sharedString = std::move(newSharedString);
} }

View File

@ -217,6 +217,8 @@ namespace Nz
for (auto& pair : m_renderInfos) for (auto& pair : m_renderInfos)
{ {
RenderIndices& indices = pair.second; RenderIndices& indices = pair.second;
if (indices.count == 0)
continue; //< Ignore empty render indices
SparsePtr<Color> color = colorPtr + indices.first*4; SparsePtr<Color> color = colorPtr + indices.first*4;
SparsePtr<Vector3f> pos = posPtr + indices.first*4; SparsePtr<Vector3f> pos = posPtr + indices.first*4;

View File

@ -57,6 +57,7 @@ namespace Nz
switch (m_protocol) switch (m_protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
break; break;
case NetProtocol_IPv4: case NetProtocol_IPv4:
@ -80,6 +81,7 @@ namespace Nz
switch (m_protocol) switch (m_protocol)
{ {
case NetProtocol_Any: case NetProtocol_Any:
case NetProtocol_Unknown:
break; break;
case NetProtocol_IPv4: case NetProtocol_IPv4:
@ -151,6 +153,8 @@ namespace Nz
String IpAddress::ResolveAddress(const IpAddress& address, String* service, ResolveError* error) String IpAddress::ResolveAddress(const IpAddress& address, String* service, ResolveError* error)
{ {
NazaraAssert(address.IsValid(), "Invalid address");
String hostname; String hostname;
IpAddressImpl::ResolveAddress(address, &hostname, service, error); IpAddressImpl::ResolveAddress(address, &hostname, service, error);
@ -159,6 +163,8 @@ namespace Nz
std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const String& hostname, const String& service, ResolveError* error) std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const String& hostname, const String& service, ResolveError* error)
{ {
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
return IpAddressImpl::ResolveHostname(protocol, hostname, service, error); return IpAddressImpl::ResolveHostname(protocol, hostname, service, error);
} }

View File

@ -57,10 +57,12 @@ namespace Nz
IpAddress hostnameAddress; IpAddress hostnameAddress;
for (const HostnameInfo& result : results) for (const HostnameInfo& result : results)
{ {
//TODO: Check PF_ type (TCP)
if (!result.address) if (!result.address)
continue; continue;
if (result.socketType != SocketType_TCP)
continue;
hostnameAddress = result.address; hostnameAddress = result.address;
break; //< Take first valid address break; //< Take first valid address
} }

View File

@ -21,12 +21,12 @@ namespace Nz
return GetAddrInfoW(hostname.GetWideString().c_str(), service.GetWideString().c_str(), &hints, &servinfo); return GetAddrInfoW(hostname.GetWideString().c_str(), service.GetWideString().c_str(), &hints, &servinfo);
} }
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service) int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
{ {
std::array<wchar_t, NI_MAXHOST> hostnameBuffer; std::array<wchar_t, NI_MAXHOST> hostnameBuffer;
std::array<wchar_t, NI_MAXSERV> serviceBuffer; std::array<wchar_t, NI_MAXSERV> serviceBuffer;
int result = GetNameInfoW(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), NI_NUMERICSERV); int result = GetNameInfoW(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), flags);
if (result == 0) if (result == 0)
{ {
if (hostname) if (hostname)
@ -51,12 +51,12 @@ namespace Nz
return getaddrinfo(hostname.GetConstBuffer(), service.GetConstBuffer(), hints, results); return getaddrinfo(hostname.GetConstBuffer(), service.GetConstBuffer(), hints, results);
} }
int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service) int GetHostnameInfo(sockaddr* socketAddress, socklen_t socketLen, String* hostname, String* service, INT flags)
{ {
std::array<char, NI_MAXHOST> hostnameBuffer; std::array<char, NI_MAXHOST> hostnameBuffer;
std::array<char, NI_MAXSERV> serviceBuffer; std::array<char, NI_MAXSERV> serviceBuffer;
int result = getnameinfo(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), NI_NUMERICSERV); int result = getnameinfo(socketAddress, socketLen, hostnameBuffer.data(), hostnameBuffer.size(), serviceBuffer.data(), serviceBuffer.size(), flags);
if (result == 0) if (result == 0)
{ {
if (hostname) if (hostname)
@ -153,7 +153,7 @@ namespace Nz
SockAddrBuffer socketAddress; SockAddrBuffer socketAddress;
socklen_t socketAddressLen = ToSockAddr(ipAddress, socketAddress.data()); socklen_t socketAddressLen = ToSockAddr(ipAddress, socketAddress.data());
if (Detail::GetHostnameInfo(reinterpret_cast<sockaddr*>(socketAddress.data()), socketAddressLen, hostname, service) != 0) if (Detail::GetHostnameInfo(reinterpret_cast<sockaddr*>(socketAddress.data()), socketAddressLen, hostname, service, NI_NUMERICSERV) != 0)
{ {
if (error) if (error)
*error = TranslateWSAErrorToResolveError(WSAGetLastError()); *error = TranslateWSAErrorToResolveError(WSAGetLastError());
@ -174,6 +174,7 @@ namespace Nz
Detail::addrinfoImpl hints; Detail::addrinfoImpl hints;
std::memset(&hints, 0, sizeof(Detail::addrinfoImpl)); std::memset(&hints, 0, sizeof(Detail::addrinfoImpl));
hints.ai_family = SocketImpl::TranslateNetProtocolToAF(procol); hints.ai_family = SocketImpl::TranslateNetProtocolToAF(procol);
hints.ai_flags = AI_CANONNAME;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
Detail::addrinfoImpl* servinfo; Detail::addrinfoImpl* servinfo;
@ -196,9 +197,8 @@ namespace Nz
HostnameInfo result; HostnameInfo result;
result.address = FromAddrinfo(p); result.address = FromAddrinfo(p);
result.canonicalName = String::Unicode(p->ai_canonname); result.canonicalName = String::Unicode(p->ai_canonname);
result.family = p->ai_family; result.protocol = TranslatePFToNetProtocol(p->ai_family);
result.flags = p->ai_flags; result.socketType = TranslateSockToNetProtocol(p->ai_socktype);
result.socketType = p->ai_socktype;
results.push_back(result); results.push_back(result);
} }
@ -252,6 +252,39 @@ namespace Nz
return 0; return 0;
} }
NetProtocol IpAddressImpl::TranslatePFToNetProtocol(int family)
{
switch (family)
{
case PF_INET:
return NetProtocol_IPv4;
case PF_INET6:
return NetProtocol_IPv6;
default:
return NetProtocol_Unknown;
}
}
SocketType IpAddressImpl::TranslateSockToNetProtocol(int socketType)
{
switch (socketType)
{
case SOCK_STREAM:
return SocketType_TCP;
case SOCK_DGRAM:
return SocketType_UDP;
case SOCK_RAW:
return SocketType_Raw;
default:
return SocketType_Unknown;
}
}
ResolveError IpAddressImpl::TranslateWSAErrorToResolveError(int error) ResolveError IpAddressImpl::TranslateWSAErrorToResolveError(int error)
{ {
switch (error) switch (error)

View File

@ -26,6 +26,8 @@ namespace Nz
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error); static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& service, ResolveError* error);
static socklen_t ToSockAddr(const IpAddress& ipAddress, void* buffer); static socklen_t ToSockAddr(const IpAddress& ipAddress, void* buffer);
static NetProtocol TranslatePFToNetProtocol(int family);
static SocketType TranslateSockToNetProtocol(int socketType);
static ResolveError TranslateWSAErrorToResolveError(int error); static ResolveError TranslateWSAErrorToResolveError(int error);
}; };
} }

View File

@ -703,7 +703,8 @@ namespace Nz
static int addressFamily[] = { static int addressFamily[] = {
AF_UNSPEC, //< NetProtocol_Any AF_UNSPEC, //< NetProtocol_Any
AF_INET, //< NetProtocol_IPv4 AF_INET, //< NetProtocol_IPv4
AF_INET6 //< NetProtocol_IPv6 AF_INET6, //< NetProtocol_IPv6
-1 //< NetProtocol_Unknown
}; };
static_assert(sizeof(addressFamily) / sizeof(int) == NetProtocol_Max + 1, "Address family array is incomplete"); static_assert(sizeof(addressFamily) / sizeof(int) == NetProtocol_Max + 1, "Address family array is incomplete");
@ -715,9 +716,10 @@ namespace Nz
NazaraAssert(type <= SocketType_Max, "Socket type has value out of enum"); NazaraAssert(type <= SocketType_Max, "Socket type has value out of enum");
static int socketType[] = { static int socketType[] = {
SOCK_RAW, //< SocketType_Raw SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM //< SocketType_UDP SOCK_DGRAM, //< SocketType_UDP
-1 //< SocketType_Unknown
}; };
static_assert(sizeof(socketType) / sizeof(int) == SocketType_Max + 1, "Socket type array is incomplete"); static_assert(sizeof(socketType) / sizeof(int) == SocketType_Max + 1, "Socket type array is incomplete");