Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ngine into NDK
  • Loading branch information
SirLynix committed Nov 16, 2015
2 parents 41333fe + 7ce714c commit 553c652
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 21 deletions.
1 change: 1 addition & 0 deletions include/Nazara/Core/String.hpp
Expand Up @@ -317,6 +317,7 @@ namespace Nz
{
inline SharedString();
inline SharedString(unsigned int strSize);
inline SharedString(unsigned int strSize, unsigned int strCapacity);

unsigned int capacity;
unsigned int size;
Expand Down
10 changes: 9 additions & 1 deletion include/Nazara/Core/String.inl
Expand Up @@ -23,12 +23,20 @@ namespace Nz
}

inline String::SharedString::SharedString(unsigned int strSize) :
capacity(strSize),
capacity(strSize),
size(strSize),
string(new char[strSize + 1])
{
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
Expand Down
6 changes: 4 additions & 2 deletions include/Nazara/Network/Enums.hpp
Expand Up @@ -30,8 +30,9 @@ namespace Nz
NetProtocol_Any,
NetProtocol_IPv4,
NetProtocol_IPv6,
NetProtocol_Unknown,

NetProtocol_Max = NetProtocol_IPv6
NetProtocol_Max = NetProtocol_Unknown
};

enum SocketError
Expand Down Expand Up @@ -71,8 +72,9 @@ namespace Nz
SocketType_Raw,
SocketType_TCP,
SocketType_UDP,
SocketType_Unknown,

SocketType_Max = SocketType_UDP
SocketType_Max = SocketType_Unknown
};
}

Expand Down
5 changes: 2 additions & 3 deletions include/Nazara/Network/IpAddress.hpp
Expand Up @@ -89,10 +89,9 @@ namespace Nz
struct HostnameInfo
{
IpAddress address;
NetProtocol protocol;
SocketType socketType;
String canonicalName;
int flags;
int family; //< TODO: NetProtocol
int socketType; //< TODO: SocketType
};

}
Expand Down
2 changes: 2 additions & 0 deletions include/Nazara/Network/IpAddress.inl
Expand Up @@ -118,6 +118,7 @@ namespace Nz
switch (first.m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;

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

case NetProtocol_IPv4:
Expand Down
2 changes: 2 additions & 0 deletions include/Nazara/Network/TcpServer.inl
Expand Up @@ -31,11 +31,13 @@ namespace Nz
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_Unknown, "Invalid protocol");

IpAddress any;
switch (protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected;

Expand Down
3 changes: 3 additions & 0 deletions include/Nazara/Network/UdpSocket.inl
Expand Up @@ -30,6 +30,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point");
return SocketState_NotConnected;

Expand All @@ -48,6 +49,8 @@ namespace Nz

bool UdpSocket::Create(NetProtocol protocol)
{
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");

return Open(protocol);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Nazara/Core/String.cpp
Expand Up @@ -4195,9 +4195,9 @@ namespace Nz

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

m_sharedString = std::move(newSharedString);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Nazara/Graphics/TextSprite.cpp
Expand Up @@ -217,6 +217,8 @@ namespace Nz
for (auto& pair : m_renderInfos)
{
RenderIndices& indices = pair.second;
if (indices.count == 0)
continue; //< Ignore empty render indices

SparsePtr<Color> color = colorPtr + indices.first*4;
SparsePtr<Vector3f> pos = posPtr + indices.first*4;
Expand Down
6 changes: 6 additions & 0 deletions src/Nazara/Network/IpAddress.cpp
Expand Up @@ -57,6 +57,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;

case NetProtocol_IPv4:
Expand All @@ -80,6 +81,7 @@ namespace Nz
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
break;

case NetProtocol_IPv4:
Expand Down Expand Up @@ -151,6 +153,8 @@ namespace Nz

String IpAddress::ResolveAddress(const IpAddress& address, String* service, ResolveError* error)
{
NazaraAssert(address.IsValid(), "Invalid address");

String hostname;
IpAddressImpl::ResolveAddress(address, &hostname, service, error);

Expand All @@ -159,6 +163,8 @@ namespace Nz

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);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Nazara/Network/TcpClient.cpp
Expand Up @@ -57,10 +57,12 @@ namespace Nz
IpAddress hostnameAddress;
for (const HostnameInfo& result : results)
{
//TODO: Check PF_ type (TCP)
if (!result.address)
continue;

if (result.socketType != SocketType_TCP)
continue;

hostnameAddress = result.address;
break; //< Take first valid address
}
Expand Down
49 changes: 41 additions & 8 deletions src/Nazara/Network/Win32/IpAddressImpl.cpp
Expand Up @@ -21,12 +21,12 @@ namespace Nz
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_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 (hostname)
Expand All @@ -51,12 +51,12 @@ namespace Nz
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_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 (hostname)
Expand Down Expand Up @@ -153,7 +153,7 @@ namespace Nz
SockAddrBuffer socketAddress;
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)
*error = TranslateWSAErrorToResolveError(WSAGetLastError());
Expand All @@ -174,6 +174,7 @@ namespace Nz
Detail::addrinfoImpl hints;
std::memset(&hints, 0, sizeof(Detail::addrinfoImpl));
hints.ai_family = SocketImpl::TranslateNetProtocolToAF(procol);
hints.ai_flags = AI_CANONNAME;
hints.ai_socktype = SOCK_STREAM;

Detail::addrinfoImpl* servinfo;
Expand All @@ -196,9 +197,8 @@ namespace Nz
HostnameInfo result;
result.address = FromAddrinfo(p);
result.canonicalName = String::Unicode(p->ai_canonname);
result.family = p->ai_family;
result.flags = p->ai_flags;
result.socketType = p->ai_socktype;
result.protocol = TranslatePFToNetProtocol(p->ai_family);
result.socketType = TranslateSockToNetProtocol(p->ai_socktype);

results.push_back(result);
}
Expand Down Expand Up @@ -252,6 +252,39 @@ namespace Nz
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)
{
switch (error)
Expand Down
2 changes: 2 additions & 0 deletions src/Nazara/Network/Win32/IpAddressImpl.hpp
Expand Up @@ -26,6 +26,8 @@ namespace Nz
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 NetProtocol TranslatePFToNetProtocol(int family);
static SocketType TranslateSockToNetProtocol(int socketType);
static ResolveError TranslateWSAErrorToResolveError(int error);
};
}
10 changes: 6 additions & 4 deletions src/Nazara/Network/Win32/SocketImpl.cpp
Expand Up @@ -703,7 +703,8 @@ namespace Nz
static int addressFamily[] = {
AF_UNSPEC, //< NetProtocol_Any
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");

Expand All @@ -715,9 +716,10 @@ namespace Nz
NazaraAssert(type <= SocketType_Max, "Socket type has value out of enum");

static int socketType[] = {
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM //< SocketType_UDP
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM, //< SocketType_UDP
-1 //< SocketType_Unknown
};
static_assert(sizeof(socketType) / sizeof(int) == SocketType_Max + 1, "Socket type array is incomplete");

Expand Down

0 comments on commit 553c652

Please sign in to comment.