Convert all remaining enums to enum classes (!)

This commit is contained in:
Jérôme Leclercq
2021-05-25 00:08:50 +02:00
parent 8cdd0b51cb
commit 874fb3542e
122 changed files with 1082 additions and 2169 deletions

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/AbstractSocket.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Network/Algorithm.hpp>
@@ -32,9 +33,9 @@ namespace Nz
*/
AbstractSocket::AbstractSocket(SocketType type) :
m_lastError(SocketError_NoError),
m_lastError(SocketError::NoError),
m_handle(SocketImpl::InvalidHandle),
m_state(SocketState_NotConnected),
m_state(SocketState::NotConnected),
m_type(type),
m_isBlockingEnabled(true)
{
@@ -165,7 +166,7 @@ namespace Nz
void AbstractSocket::OnClose()
{
UpdateState(SocketState_NotConnected);
UpdateState(SocketState::NotConnected);
}
/*!
@@ -178,7 +179,7 @@ namespace Nz
{
SocketError errorCode;
if (!SocketImpl::SetBlocking(m_handle, m_isBlockingEnabled, &errorCode))
NazaraWarning("Failed to set socket blocking mode (0x" + NumberToString(errorCode, 16) + ')');
NazaraWarning("Failed to set socket blocking mode (0x" + NumberToString(UnderlyingCast(errorCode), 16) + ')');
}
/*!
@@ -190,11 +191,11 @@ namespace Nz
{
if (m_handle == SocketImpl::InvalidHandle || m_protocol != protocol)
{
SocketHandle handle = SocketImpl::Create((protocol == NetProtocol_Any) ? NetProtocol_IPv6 : protocol, m_type, &m_lastError);
SocketHandle handle = SocketImpl::Create((protocol == NetProtocol::Any) ? NetProtocol::IPv6 : protocol, m_type, &m_lastError);
if (handle == SocketImpl::InvalidHandle)
return false;
if (protocol == NetProtocol_Any)
if (protocol == NetProtocol::Any)
{
if (!SocketImpl::SetIPv6Only(handle, false, &m_lastError))
{
@@ -204,7 +205,7 @@ namespace Nz
return false;
}
protocol = NetProtocol_IPv6;
protocol = NetProtocol::IPv6;
}
m_protocol = protocol;

View File

@@ -88,31 +88,31 @@ namespace Nz
{
switch (resolveError)
{
case ResolveError_NoError:
case ResolveError::NoError:
return "No error";
case ResolveError_Internal:
case ResolveError::Internal:
return "An internal error occurred";
case ResolveError_ResourceError:
case ResolveError::ResourceError:
return "The operating system lacks the resources to proceed";
case ResolveError_NonRecoverable:
case ResolveError::NonRecoverable:
return "A nonrecoverable error occurred";
case ResolveError_NotFound:
case ResolveError::NotFound:
return "No such host is known";
case ResolveError_NotInitialized:
case ResolveError::NotInitialized:
return "Nazara Network has not been initialized";
case ResolveError_ProtocolNotSupported:
case ResolveError::ProtocolNotSupported:
return "A specified protocol is not supported by the server";
case ResolveError_TemporaryFailure:
case ResolveError::TemporaryFailure:
return "A temporary failure occurred, try again";
case ResolveError_Unknown:
case ResolveError::Unknown:
return "An unknown error occurred";
default:
@@ -131,52 +131,52 @@ namespace Nz
{
switch (socketError)
{
case SocketError_NoError:
case SocketError::NoError:
return "No error";
case SocketError_AddressNotAvailable:
case SocketError::AddressNotAvailable:
return "The address is already in use";
case SocketError_ConnectionClosed:
case SocketError::ConnectionClosed:
return "The connection has been closed";
case SocketError_ConnectionRefused:
case SocketError::ConnectionRefused:
return "The connection attempt was refused";
case SocketError_DatagramSize:
case SocketError::DatagramSize:
return "The datagram size is over the system limit";
case SocketError_Internal:
case SocketError::Internal:
return "An internal error occurred";
case SocketError_Interrupted:
case SocketError::Interrupted:
return "The operation was interrupted by a signal";
case SocketError_Packet:
case SocketError::Packet:
return "Packet encoding or decoding failed";
case SocketError_NetworkError:
case SocketError::NetworkError:
return "Networking subsystem failed";
case SocketError_NotInitialized:
case SocketError::NotInitialized:
return "Network module has not been initialized";
case SocketError_NotSupported:
case SocketError::NotSupported:
return "This operation is not supported";
case SocketError_ResolveError:
case SocketError::ResolveError:
return "The hostname couldn't be resolved";
case SocketError_ResourceError:
case SocketError::ResourceError:
return "The operating system lacks the resources to proceed";
case SocketError_TimedOut:
case SocketError::TimedOut:
return "The operation timed out";
case SocketError_Unknown:
case SocketError::Unknown:
return "An unknown error occurred";
case SocketError_UnreachableHost:
case SocketError::UnreachableHost:
return "The host is not reachable";
default:

View File

@@ -89,9 +89,9 @@ namespace Nz
UInt32 windowSize;
if (m_outgoingBandwidth == 0)
windowSize = ENetProtocol_MaximumWindowSize;
windowSize = ENetConstants::ENetProtocol_MaximumWindowSize;
else
windowSize = (m_outgoingBandwidth / ENetConstants::ENetPeer_WindowSizeScale) * ENetProtocol_MinimumWindowSize;
windowSize = (m_outgoingBandwidth / ENetConstants::ENetPeer_WindowSizeScale) * ENetConstants::ENetProtocol_MinimumWindowSize;
ENetPeer& peer = m_peers[peerId];
peer.InitOutgoing(channelCount, remoteAddress, ++m_randomSeed, windowSize);
@@ -134,7 +134,7 @@ namespace Nz
if (!hostnameAddress.IsValid())
{
if (error)
*error = ResolveError_NotFound;
*error = ResolveError::NotFound;
return nullptr;
}
@@ -320,7 +320,7 @@ namespace Nz
bool ENetHost::InitSocket(const IpAddress& address)
{
if (!m_socket.Create((m_isUsingDualStack) ? NetProtocol_Any : address.GetProtocol()))
if (!m_socket.Create((m_isUsingDualStack) ? NetProtocol::Any : address.GetProtocol()))
return false;
m_socket.EnableBlocking(false);
@@ -330,14 +330,14 @@ namespace Nz
if (address.IsValid() && !address.IsLoopback())
{
if (m_socket.Bind(address) != SocketState_Bound)
if (m_socket.Bind(address) != SocketState::Bound)
{
NazaraError("Failed to bind address " + address.ToString());
return false;
}
}
m_poller.RegisterSocket(m_socket, SocketPollEvent_Read);
m_poller.RegisterSocket(m_socket, SocketPollEvent::Read);
return true;
}
@@ -417,7 +417,7 @@ namespace Nz
UInt32 channelCount = NetToHost(command->connect.channelCount);
if (channelCount < ENetProtocol_MinimumChannelCount || channelCount > ENetProtocol_MaximumChannelCount)
if (channelCount < ENetConstants::ENetProtocol_MinimumChannelCount || channelCount > ENetConstants::ENetProtocol_MaximumChannelCount)
return nullptr;
std::size_t duplicatePeers = 0;

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Network/Algorithm.hpp>
@@ -47,14 +48,14 @@ namespace Nz
m_isValid = true;
if (isIPv6)
{
m_protocol = NetProtocol_IPv6;
m_protocol = NetProtocol::IPv6;
for (unsigned int i = 0; i < 8; ++i)
m_ipv6[i] = UInt32(result[i*2]) << 8 | result[i*2 + 1];
}
else
{
m_protocol = NetProtocol_IPv4;
m_protocol = NetProtocol::IPv4;
for (unsigned int i = 0; i < 4; ++i)
m_ipv4[i] = result[i];
@@ -75,21 +76,21 @@ namespace Nz
if (!m_isValid)
return false;
NazaraAssert(m_protocol <= NetProtocol_Max, "Protocol has value out of enum");
NazaraAssert(m_protocol <= NetProtocol::Max, "Protocol has value out of enum");
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
case NetProtocol::Any:
case NetProtocol::Unknown:
break;
case NetProtocol_IPv4:
case NetProtocol::IPv4:
return m_ipv4[0] == 127;
case NetProtocol_IPv6:
case NetProtocol::IPv6:
return m_ipv6 == LoopbackIpV6.m_ipv6; // Only compare the ip value
}
NazaraInternalError("Invalid protocol for IpAddress (0x" + NumberToString(m_protocol) + ')');
NazaraInternalError("Invalid protocol for IpAddress (0x" + NumberToString(UnderlyingCast(m_protocol), 16) + ')');
return false;
}
@@ -106,14 +107,14 @@ namespace Nz
if (m_isValid)
{
NazaraAssert(m_protocol <= NetProtocol_Max, "Protocol has value out of enum");
NazaraAssert(m_protocol <= NetProtocol::Max, "Protocol has value out of enum");
switch (m_protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
case NetProtocol::Any:
case NetProtocol::Unknown:
break;
case NetProtocol_IPv4:
case NetProtocol::IPv4:
for (unsigned int i = 0; i < 4; ++i)
{
stream << int(m_ipv4[i]);
@@ -122,7 +123,7 @@ namespace Nz
}
break;
case NetProtocol_IPv6:
case NetProtocol::IPv6:
// Canonical representation of an IPv6
// https://tools.ietf.org/html/rfc5952
@@ -213,7 +214,7 @@ namespace Nz
*/
std::vector<HostnameInfo> IpAddress::ResolveHostname(NetProtocol protocol, const std::string& hostname, const std::string& service, ResolveError* error)
{
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
NazaraAssert(protocol != NetProtocol::Unknown, "Invalid protocol");
return IpAddressImpl::ResolveHostname(protocol, hostname, service, error);
}

View File

@@ -53,10 +53,10 @@ namespace Nz
entry.data.fd = socket;
if (eventFlags & SocketPollEvent_Read)
if (eventFlags & SocketPollEvent::Read)
entry.events |= EPOLLIN;
if (eventFlags & SocketPollEvent_Write)
if (eventFlags & SocketPollEvent::Write)
entry.events |= EPOLLOUT;
if (epoll_ctl(m_handle, EPOLL_CTL_ADD, socket, &entry) != 0)
@@ -123,7 +123,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return activeSockets;
}

View File

@@ -41,7 +41,7 @@ namespace Nz
const void* NetPacket::OnSend(std::size_t* newSize) const
{
NazaraAssert(newSize, "Invalid size pointer");
NazaraAssert(m_netCode != NetCode_Invalid, "Invalid NetCode");
NazaraAssert(m_netCode != 0, "Invalid NetCode");
std::size_t size = m_buffer->GetSize();
if (!EncodeHeader(m_buffer->GetBuffer(), static_cast<UInt32>(size), m_netCode))

View File

@@ -9,7 +9,6 @@
#include <Nazara/Core/Log.hpp>
#include <Nazara/Network/Config.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/RUdpConnection.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Network/Win32/SocketImpl.hpp>
@@ -40,15 +39,11 @@ namespace Nz
if (!NetPacket::Initialize())
throw std::runtime_error("failed to initialize packets");
if (!RUdpConnection::Initialize())
throw std::runtime_error("failed to initialize RUDP protocol");
}
Network::~Network()
{
// Uninitialize module here
RUdpConnection::Uninitialize();
NetPacket::Uninitialize();
SocketImpl::Uninitialize();
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Posix/IpAddressImpl.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
@@ -132,7 +133,7 @@ namespace Nz
}
if (error)
*error = ResolveError_NoError;
*error = ResolveError::NoError;
return true;
}
@@ -174,7 +175,7 @@ namespace Nz
}
if (error)
*error = ResolveError_NoError;
*error = ResolveError::NoError;
return results;
}
@@ -185,7 +186,7 @@ namespace Nz
{
switch (ipAddress.GetProtocol())
{
case NetProtocol_IPv4:
case NetProtocol::IPv4:
{
sockaddr_in* socketAddress = reinterpret_cast<sockaddr_in*>(buffer);
@@ -197,7 +198,7 @@ namespace Nz
return sizeof(sockaddr_in);
}
case NetProtocol_IPv6:
case NetProtocol::IPv6:
{
sockaddr_in6* socketAddress = reinterpret_cast<sockaddr_in6*>(buffer);
@@ -217,7 +218,7 @@ namespace Nz
}
default:
NazaraInternalError("Unhandled ip protocol (0x" + NumberToString(ipAddress.GetProtocol(), 16) + ')');
NazaraInternalError("Unhandled ip protocol (0x" + NumberToString(UnderlyingCast(ipAddress.GetProtocol()), 16) + ')');
break;
}
}
@@ -231,13 +232,13 @@ namespace Nz
switch (family)
{
case PF_INET:
return NetProtocol_IPv4;
return NetProtocol::IPv4;
case PF_INET6:
return NetProtocol_IPv6;
return NetProtocol::IPv6;
default:
return NetProtocol_Unknown;
return NetProtocol::Unknown;
}
}
@@ -246,16 +247,16 @@ namespace Nz
switch (socketType)
{
case SOCK_STREAM:
return SocketType_TCP;
return SocketType::TCP;
case SOCK_DGRAM:
return SocketType_UDP;
return SocketType::UDP;
case SOCK_RAW:
return SocketType_Raw;
return SocketType::Raw;
default:
return SocketType_Unknown;
return SocketType::Unknown;
}
}
@@ -265,35 +266,35 @@ namespace Nz
switch (error)
{
case 0:
return ResolveError_NoError;
return ResolveError::NoError;
// Engine error
case EAI_BADFLAGS:
case EAI_SYSTEM:
return ResolveError_Internal;
return ResolveError::Internal;
case EAI_FAMILY:
case EAI_SERVICE:
case EAI_SOCKTYPE:
return ResolveError_ProtocolNotSupported;
return ResolveError::ProtocolNotSupported;
case EAI_NONAME:
return ResolveError_NotFound;
return ResolveError::NotFound;
case EAI_FAIL:
return ResolveError_NonRecoverable;
return ResolveError::NonRecoverable;
case EAI_NODATA:
return ResolveError_NotInitialized;
return ResolveError::NotInitialized;
case EAI_MEMORY:
return ResolveError_ResourceError;
return ResolveError::ResourceError;
case EAI_AGAIN:
return ResolveError_TemporaryFailure;
return ResolveError::TemporaryFailure;
}
NazaraWarning("Unhandled EAI error: " + Error::GetLastSystemError(error) + " (" + NumberToString(error) + ") as " + gai_strerror(error));
return ResolveError_Unknown;
return ResolveError::Unknown;
}
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Posix/SocketImpl.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StackArray.hpp>
#include <Nazara/Core/StringExt.hpp>
@@ -41,7 +42,7 @@ namespace Nz
*address = IpAddressImpl::FromSockAddr(reinterpret_cast<const sockaddr*>(&nameBuffer));
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
}
else
{
@@ -65,19 +66,19 @@ namespace Nz
if (error)
*error = TranslateErrnoToSocketError(GetLastErrorCode());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return SocketState_Bound;
return SocketState::Bound;
}
SocketHandle SocketImpl::Create(NetProtocol protocol, SocketType type, SocketError* error)
{
NazaraAssert(protocol != NetProtocol_Any, "Any protocol is not supported for socket creation");
NazaraAssert(type <= SocketType_Max, "Type has value out of enum");
NazaraAssert(protocol != NetProtocol::Any, "Any protocol is not supported for socket creation");
NazaraAssert(type <= SocketType::Max, "Type has value out of enum");
SocketHandle handle = socket(TranslateNetProtocolToAF(protocol), TranslateSocketTypeToSock(type), 0);
if (handle == InvalidHandle && error != nullptr)
@@ -98,7 +99,7 @@ namespace Nz
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
if (GetLastError(handle, nullptr) < 0)
if (GetLastError(handle, nullptr) != SocketError::Internal)
NazaraWarning("Failed to clear socket error code: " + Error::GetLastSystemError(GetLastErrorCode()));
}
@@ -111,7 +112,7 @@ namespace Nz
int bufferLength = IpAddressImpl::ToSockAddr(address, nameBuffer.data());
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
// Clear socket error status
ClearErrorCode(handle);
@@ -123,24 +124,24 @@ namespace Nz
{
case EALREADY:
case EINPROGRESS:
return SocketState_Connecting;
return SocketState::Connecting;
case EISCONN:
return SocketState_Connected;
return SocketState::Connected;
}
if (error)
{
if (errorCode == EADDRNOTAVAIL)
*error = SocketError_ConnectionRefused; //< ConnectionRefused seems more legit than AddressNotAvailable in connect case
*error = SocketError::ConnectionRefused; //< ConnectionRefused seems more legit than AddressNotAvailable in connect case
else
*error = TranslateErrnoToSocketError(errorCode);
}
return SocketState_NotConnected;
return SocketState::NotConnected;
}
return SocketState_Connected;
return SocketState::Connected;
}
bool SocketImpl::Initialize()
@@ -152,7 +153,7 @@ namespace Nz
{
int code = GetLastErrorCode(handle, error);
if (code < 0)
return SocketError_Internal;
return SocketError::Internal;
return TranslateErrnoToSocketError(code);
}
@@ -176,7 +177,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -194,7 +195,7 @@ namespace Nz
if (error)
*error = TranslateErrnoToSocketError(GetLastErrorCode());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (listen(handle, queueSize) == SOCKET_ERROR)
@@ -202,13 +203,13 @@ namespace Nz
if (error)
*error = TranslateErrnoToSocketError(GetLastErrorCode());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return SocketState_Bound;
return SocketState::Bound;
}
std::size_t SocketImpl::QueryAvailableBytes(SocketHandle handle, SocketError* error)
@@ -225,7 +226,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return availableBytes;
}
@@ -244,7 +245,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -263,7 +264,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -286,7 +287,7 @@ namespace Nz
#endif
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -305,7 +306,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -324,7 +325,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -347,7 +348,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return IpAddressImpl::FromSockAddr(reinterpret_cast<sockaddr*>(nameBuffer.data()));
}
@@ -366,7 +367,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -386,7 +387,7 @@ namespace Nz
{
int errorCode = GetLastErrorCode();
if (errorCode == EINVAL)
*error = SocketError_NoError;
*error = SocketError::NoError;
else
*error = TranslateErrnoToSocketError(errorCode);
}
@@ -395,7 +396,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return IpAddressImpl::FromSockAddr(reinterpret_cast<sockaddr*>(nameBuffer.data()));
}
@@ -432,7 +433,7 @@ namespace Nz
if (error)
*error = TranslateErrnoToSocketError(GetLastErrorCode());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
else if (ret > 0)
{
@@ -441,14 +442,14 @@ namespace Nz
if (error)
*error = GetLastError(handle);
return SocketState_NotConnected;
return SocketState::NotConnected;
}
else if (descriptor.revents & POLLOUT)
return SocketState_Connected;
return SocketState::Connected;
else
{
NazaraWarning("Socket " + std::to_string(handle) + " was returned by poll without POLLOUT nor error events (events: 0x" + NumberToString(descriptor.revents, 16) + ')');
return SocketState_NotConnected;
return SocketState::NotConnected;
}
}
else
@@ -457,12 +458,12 @@ namespace Nz
if (error)
{
if (msTimeout > 0)
*error = SocketError_TimedOut;
*error = SocketError::TimedOut;
else
*error = SocketError_NoError;
*error = SocketError::NoError;
}
return SocketState_Connecting;
return SocketState::Connecting;
}
}
@@ -499,7 +500,7 @@ namespace Nz
else if (byteRead == 0)
{
if (error)
*error = SocketError_ConnectionClosed;
*error = SocketError::ConnectionClosed;
return false; //< Connection has been closed
}
@@ -508,7 +509,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -554,7 +555,7 @@ namespace Nz
else if (byteRead == 0)
{
if (error)
*error = SocketError_ConnectionClosed;
*error = SocketError::ConnectionClosed;
return false; //< Connection closed
}
@@ -568,7 +569,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -634,7 +635,7 @@ namespace Nz
else if (byteRead == 0)
{
if (error)
*error = SocketError_ConnectionClosed;
*error = SocketError::ConnectionClosed;
return false; //< Connection closed
}
@@ -645,7 +646,7 @@ namespace Nz
if (msgHdr.msg_flags & MSG_TRUNC)
{
if (error)
*error = SocketError_DatagramSize;
*error = SocketError::DatagramSize;
return false;
}
@@ -658,7 +659,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -695,7 +696,7 @@ namespace Nz
*sent = byteSent;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -753,7 +754,7 @@ namespace Nz
*sent = static_cast<int>(byteSent);
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -793,7 +794,7 @@ namespace Nz
*sent = byteSent;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -812,7 +813,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -831,7 +832,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -850,7 +851,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -888,7 +889,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -907,7 +908,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
#if not defined(MSG_NOSIGNAL) // -> https://github.com/intel/parameter-framework/pull/133/files
//There is no MSG_NOSIGNAL on macos
@@ -938,7 +939,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -957,7 +958,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -967,7 +968,7 @@ namespace Nz
switch (error)
{
case 0:
return SocketError_NoError;
return SocketError::NoError;
// Engine error
case EACCES:
@@ -981,83 +982,83 @@ namespace Nz
case EISCONN:
case EWOULDBLOCK:
NazaraWarning("Internal error occurred: " + Error::GetLastSystemError(error) + " (" + NumberToString(error)+')');
return SocketError_Internal;
return SocketError::Internal;
case EADDRNOTAVAIL:
case EADDRINUSE:
return SocketError_AddressNotAvailable;
return SocketError::AddressNotAvailable;
case EAFNOSUPPORT:
case EPFNOSUPPORT:
case EOPNOTSUPP:
case EPROTONOSUPPORT:
case ESOCKTNOSUPPORT:
return SocketError_NotSupported;
return SocketError::NotSupported;
case ECONNREFUSED:
return SocketError_ConnectionRefused;
return SocketError::ConnectionRefused;
case EINTR:
return SocketError_Interrupted;
return SocketError::Interrupted;
case EMSGSIZE:
return SocketError_DatagramSize;
return SocketError::DatagramSize;
case EMFILE:
case ENOBUFS:
case ENOMEM:
return SocketError_ResourceError;
return SocketError::ResourceError;
case ENOTCONN:
case ESHUTDOWN:
return SocketError_ConnectionClosed;
return SocketError::ConnectionClosed;
case EHOSTUNREACH:
return SocketError_UnreachableHost;
return SocketError::UnreachableHost;
case ENETDOWN:
case ENETUNREACH:
return SocketError_NetworkError;
return SocketError::NetworkError;
case ENODATA:
return SocketError_NotInitialized;
return SocketError::NotInitialized;
case ETIMEDOUT:
return SocketError_TimedOut;
return SocketError::TimedOut;
}
NazaraWarning("Unhandled POSIX error: " + Error::GetLastSystemError(error) + " (" + NumberToString(error) + ')');
return SocketError_Unknown;
return SocketError::Unknown;
}
int SocketImpl::TranslateNetProtocolToAF(NetProtocol protocol)
{
NazaraAssert(protocol <= NetProtocol_Max, "Protocol has value out of enum");
NazaraAssert(protocol <= NetProtocol::Max, "Protocol has value out of enum");
static int addressFamily[] = {
AF_UNSPEC, //< NetProtocol_Any
AF_INET, //< NetProtocol_IPv4
AF_INET6, //< NetProtocol_IPv6
-1 //< NetProtocol_Unknown
AF_UNSPEC, //< NetProtocol::Any
AF_INET, //< NetProtocol::IPv4
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) == NetProtocolCount, "Address family array is incomplete");
return addressFamily[protocol];
return addressFamily[UnderlyingCast(protocol)];
}
int SocketImpl::TranslateSocketTypeToSock(SocketType type)
{
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[] = {
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM, //< SocketType_UDP
-1 //< SocketType_Unknown
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");
static_assert(sizeof(socketType) / sizeof(int) == SocketTypeCount, "Socket type array is incomplete");
return socketType[type];
return socketType[UnderlyingCast(type)];
}
void SocketImpl::Uninitialize()

View File

@@ -42,10 +42,10 @@ namespace Nz
0
};
if (eventFlags & SocketPollEvent_Read)
if (eventFlags & SocketPollEvent::Read)
entry.events |= POLLRDNORM;
if (eventFlags & SocketPollEvent_Write)
if (eventFlags & SocketPollEvent::Write)
entry.events |= POLLWRNORM;
m_allSockets[socket] = m_sockets.size();

View File

@@ -1,640 +0,0 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/RUdpConnection.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
/*!
* \ingroup network
* \class Nz::RUdpConnection
* \brief Network class that represents a reliable UDP connection
*/
/*!
* \brief Constructs a RUdpConnection object by default
*/
RUdpConnection::RUdpConnection() :
m_peerIterator(0),
m_forceAckSendTime(10'000), //< 10ms
m_pingInterval(1'000'000), //< 1s
m_protocol(0x4E4E6574), //< "NNet"
m_timeBeforePing(500'000), //< 0.5s
m_timeBeforeTimeOut(10'000'000), //< 10s
m_currentTime(0),
m_isSimulationEnabled(false),
m_shouldAcceptConnections(true)
{
}
/*!
* \brief Connects to the IpAddress
* \return true
*
* \param remoteAddress Address to connect to
*
* \remark Produces a NazaraAssert if socket is not bound
* \remark Produces a NazaraAssert if remote is invalid
* \remark Produces a NazaraAssert if port is not specified
*/
bool RUdpConnection::Connect(const IpAddress& remoteAddress)
{
NazaraAssert(m_socket.GetState() == SocketState_Bound, "Socket must be bound first");
NazaraAssert(remoteAddress.IsValid(), "Invalid remote address");
NazaraAssert(remoteAddress.GetPort() != 0, "Remote address has no port");
PeerData& client = RegisterPeer(remoteAddress, PeerState_Connecting);
client.stateData1 = s_randomGenerator();
NetPacket connectionRequestPacket(NetCode_RequestConnection);
connectionRequestPacket << client.stateData1;
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Reliable, connectionRequestPacket);
return true;
}
/*!
* \brief Connects to the hostname
* \return true If successful
*
* \param hostName Hostname of the remote
* \param protocol Net protocol to use
* \param service Specify the protocol used
* \param error Optional argument to get the 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())
{
m_lastError = SocketError_ResolveError;
return false;
}
IpAddress hostnameAddress;
for (const HostnameInfo& result : results)
{
if (!result.address)
continue;
if (result.socketType != SocketType_UDP)
continue;
hostnameAddress = result.address;
break; //< Take first valid address
}
return Connect(hostnameAddress);
}
/*!
* \brief Listens to a socket
* \return true If successfully bound
*
* \param remoteAddress Address to listen to
*/
bool RUdpConnection::Listen(const IpAddress& address)
{
if (!InitSocket(address.GetProtocol()))
return false;
return m_socket.Bind(address) == SocketState_Bound;
}
/*!
* \brief Polls the message
* \return true If there is a message
*
* \param message Message to poll
*
* \remark Produces a NazaraAssert if message is invalid
*/
bool RUdpConnection::PollMessage(RUdpMessage* message)
{
NazaraAssert(message, "Invalid message");
if (m_receivedMessages.empty())
return false;
*message = std::move(m_receivedMessages.front());
m_receivedMessages.pop();
return true;
}
/*!
* \brief Sends the packet to a peer
* \return true If peer exists (false may result from disconnected client)
*
* \param peerIp IpAddress of the peer
* \param priority Priority of the packet
* \param reliability Policy of reliability of the packet
* \param packet Packet to send
*/
bool RUdpConnection::Send(const IpAddress& peerIp, PacketPriority priority, PacketReliability reliability, const NetPacket& packet)
{
auto it = m_peerByIP.find(peerIp);
if (it == m_peerByIP.end())
return false; /// Silently fail (probably a disconnected client)
EnqueuePacket(m_peers[it->second], priority, reliability, packet);
return true;
}
/*!
* \brief Updates the reliable connection
*/
void RUdpConnection::Update()
{
m_currentTime = m_clock.GetMicroseconds();
NetPacket receivedPacket;
IpAddress senderIp;
while (m_socket.ReceivePacket(&receivedPacket, &senderIp))
OnPacketReceived(senderIp, std::move(receivedPacket));
//for (unsigned int i = m_activeClients.FindFirst(); i != m_activeClients.npos; i = m_activeClients.FindNext(i))
//{
// PeerData& clientData = m_peers[i];
CallOnExit resetIterator([this] () { m_peerIterator = m_peers.size(); });
for (m_peerIterator = 0; m_peerIterator < m_peers.size(); ++m_peerIterator)
{
PeerData& peer = m_peers[m_peerIterator];
UInt32 timeSinceLastPacket = static_cast<UInt32>(m_currentTime - peer.lastPacketTime);
if (timeSinceLastPacket > m_timeBeforeTimeOut)
{
DisconnectPeer(peer.index);
continue;
}
else if (timeSinceLastPacket > m_timeBeforePing)
{
if (m_currentTime - peer.lastPingTime > m_pingInterval)
{
NetPacket pingPacket(NetCode_Ping);
EnqueuePacket(peer, PacketPriority_Low, PacketReliability_Unreliable, pingPacket);
}
}
if (peer.state == PeerState_WillAck && m_currentTime - peer.stateData1 > m_forceAckSendTime)
{
NetPacket acknowledgePacket(NetCode_Acknowledge);
EnqueuePacket(peer, PacketPriority_Low, PacketReliability_Reliable, acknowledgePacket);
}
for (unsigned int priority = PacketPriority_Highest; priority <= PacketPriority_Lowest; ++priority)
{
std::vector<PendingPacket>& pendingPackets = peer.pendingPackets[priority];
for (PendingPacket& packetData : pendingPackets)
SendPacket(peer, std::move(packetData));
pendingPackets.clear();
}
auto it = peer.pendingAckQueue.begin();
while (it != peer.pendingAckQueue.end())
{
if (m_currentTime - it->timeSent > 2 * peer.roundTripTime)
{
OnPacketLost(peer, std::move(*it));
it = peer.pendingAckQueue.erase(it);
}
else
++it;
}
}
//m_activeClients.Reset();
}
/*!
* \brief Disconnects a peer
*
* \param peerIndex Index of the peer
*
* \remark Produces a NazaraNotice
*/
void RUdpConnection::DisconnectPeer(std::size_t peerIndex)
{
PeerData& peer = m_peers[peerIndex];
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": " + peer.address.ToString() + " has been disconnected due to time-out");
OnPeerDisconnected(this, peer.address);
// Remove from IP lookup table
m_peerByIP.erase(peer.address);
// Can we safely "remove" this slot?
if (m_peerIterator >= m_peers.size() - 1 || peerIndex > m_peerIterator)
{
// Yes we can
PeerData& newSlot = m_peers[peerIndex];
newSlot = std::move(m_peers.back());
newSlot.index = peerIndex; //< Update the moved slot index before resizing (in case it's the last one)
}
else
{
// Nope, let's be tricky
PeerData& current = m_peers[m_peerIterator];
PeerData& newSlot = m_peers[peerIndex];
newSlot = std::move(current);
newSlot.index = peerIndex; //< Update the moved slot index
current = std::move(m_peers.back());
current.index = m_peerIterator; //< Update the moved slot index
--m_peerIterator;
}
// Pop the last entry (from where we moved our slot)
m_peers.pop_back();
}
/*!
* \brief Enqueues a packet in the sending list
*
* \param peer Data relative to the peer
* \param priority Priority of the packet
* \param reliability Policy of reliability of the packet
* \param packet Packet to send
*/
void RUdpConnection::EnqueuePacket(PeerData& peer, PacketPriority priority, PacketReliability reliability, const NetPacket& packet)
{
UInt16 protocolBegin = static_cast<UInt16>(m_protocol & 0xFFFF);
UInt16 protocolEnd = static_cast<UInt16>((m_protocol & 0xFFFF0000) >> 16);
NetPacket data(packet.GetNetCode(), MessageHeader + packet.GetDataSize() + MessageFooter);
data << protocolBegin;
data.GetStream()->SetCursorPos(NetPacket::HeaderSize + MessageHeader);
data.Write(packet.GetConstData() + NetPacket::HeaderSize, packet.GetDataSize());
data << protocolEnd;
EnqueuePacketInternal(peer, priority, reliability, std::move(data));
}
/*!
* \brief Enqueues internally a packet in the sending list
*
* \param peer Data relative to the peer
* \param priority Priority of the packet
* \param reliability Policy of reliability of the packet
* \param packet Packet to send
*/
void RUdpConnection::EnqueuePacketInternal(PeerData& peer, PacketPriority priority, PacketReliability reliability, NetPacket&& data)
{
PendingPacket pendingPacket;
pendingPacket.data = std::move(data);
pendingPacket.priority = priority;
pendingPacket.reliability = reliability;
peer.pendingPackets[priority].emplace_back(std::move(pendingPacket));
m_activeClients.UnboundedSet(peer.index);
}
/*!
* \brief Inits the internal socket
* \return true If successful
*
* \param protocol Net protocol to use
*/
bool RUdpConnection::InitSocket(NetProtocol protocol)
{
CallOnExit updateLastError([this]
{
m_lastError = m_socket.GetLastError();
});
if (!m_socket.Create(protocol))
return false;
m_socket.EnableBlocking(false);
return true;
}
/*!
* \brief Processes the acks
*
* \param peer Data relative to the peer
* \param lastAck Last index of the ack
* \param ackBits Bits for acking
*/
void RUdpConnection::ProcessAcks(PeerData& peer, SequenceIndex lastAck, UInt32 ackBits)
{
auto it = peer.pendingAckQueue.begin();
while (it != peer.pendingAckQueue.end())
{
bool acked = false;
if (lastAck == it->sequenceId)
acked = true;
else if (!IsAckMoreRecent(it->sequenceId, lastAck))
{
unsigned int difference = ComputeSequenceDifference(lastAck, it->sequenceId);
if (difference <= 32)
acked = (ackBits >> (difference - 1)) & 1;
}
if (acked)
{
it = peer.pendingAckQueue.erase(it);
}
else
++it;
}
}
/*!
* \brief Registers a peer
* \return Data relative to the peer
*
* \param address Address of the peer
* \param state Status of the peer
*/
RUdpConnection::PeerData& RUdpConnection::RegisterPeer(const IpAddress& address, PeerState state)
{
PeerData data;
data.address = address;
data.localSequence = 0;
data.remoteSequence = 0;
data.index = m_peers.size();
data.lastPacketTime = m_currentTime;
data.lastPingTime = m_currentTime;
data.roundTripTime = 1'000'000; ///< Okay that's quite a lot
data.state = state;
m_activeClients.UnboundedSet(data.index);
m_peerByIP[address] = data.index;
m_peers.emplace_back(std::move(data));
return m_peers.back();
}
/*!
* \brief Operation to do when client requests a connection
*
* \param address Address of the peer
* \param sequenceId Sequence index for the ack
* \param token Token for connection
*/
void RUdpConnection::OnClientRequestingConnection(const IpAddress& address, SequenceIndex sequenceId, UInt64 token)
{
// Call hook to check if client should be accepted or not
OnPeerConnection(this, address);
PeerData& client = RegisterPeer(address, PeerState_Aknowledged);
client.remoteSequence = sequenceId;
/// Acknowledge connection
NetPacket connectionAcceptedPacket(NetCode_AcknowledgeConnection);
//connectionAcceptedPacket << address;
connectionAcceptedPacket << ~token;
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Reliable, connectionAcceptedPacket);
}
/*!
* \brief Operation to do when a packet is lost
*
* \param peer Data relative to the peer
* \param packet Pending packet
*/
void RUdpConnection::OnPacketLost(PeerData& peer, PendingAckPacket&& packet)
{
//NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Lost packet " + NumberToString(packet.sequenceId));
if (IsReliable(packet.reliability))
EnqueuePacketInternal(peer, packet.priority, packet.reliability, std::move(packet.data));
}
/*!
* \brief Operation to do when receiving a packet
*
* \param peerIndex Index of the peer
*
* \remark Produces a NazaraNotice
*/
void RUdpConnection::OnPacketReceived(const IpAddress& peerIp, NetPacket&& packet)
{
UInt16 protocolBegin;
UInt16 protocolEnd;
SequenceIndex sequenceId;
SequenceIndex lastAck;
UInt32 ackBits;
packet.GetStream()->SetCursorPos(packet.GetSize() - MessageFooter);
packet >> protocolEnd;
packet.GetStream()->SetCursorPos(NetPacket::HeaderSize);
packet >> protocolBegin;
UInt32 protocolId = static_cast<UInt32>(protocolEnd) << 16 | protocolBegin;
if (protocolId != m_protocol)
return; ///< Ignore
packet >> sequenceId >> lastAck >> ackBits;
auto it = m_peerByIP.find(peerIp);
if (it == m_peerByIP.end())
{
switch (packet.GetNetCode())
{
case NetCode_RequestConnection:
{
UInt64 token;
packet >> token;
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_RequestConnection from " + peerIp.ToString() + ": " + NumberToString(token));
if (!m_shouldAcceptConnections)
return; //< Ignore
OnClientRequestingConnection(peerIp, sequenceId, token);
break;
}
default:
return; //< Ignore
}
}
else
{
PeerData& peer = m_peers[it->second];
peer.lastPacketTime = m_currentTime;
if (peer.receivedQueue.find(sequenceId) != peer.receivedQueue.end())
return; //< Ignore
if (m_isSimulationEnabled && m_packetLossProbability(s_randomGenerator))
{
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Lost packet " + NumberToString(sequenceId) + " from " + peerIp.ToString() + " for simulation purpose");
return;
}
///< Receiving a packet from an acknowledged client means the connection works in both ways
if (peer.state == PeerState_Aknowledged && packet.GetNetCode() != NetCode_RequestConnection)
{
peer.state = PeerState_Connected;
OnPeerAcknowledged(this, peerIp);
}
if (IsAckMoreRecent(sequenceId, peer.remoteSequence))
peer.remoteSequence = sequenceId;
ProcessAcks(peer, lastAck, ackBits);
peer.receivedQueue.insert(sequenceId);
switch (packet.GetNetCode())
{
case NetCode_Acknowledge:
return; //< Do not switch to will ack mode (to prevent infinite replies, just let's ping/pong do that)
case NetCode_AcknowledgeConnection:
{
if (peer.state == PeerState_Connected)
break;
IpAddress externalAddress;
UInt64 token;
packet /*>> externalAddress*/ >> token;
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_AcknowledgeConnection from " + peerIp.ToString() + ": " + NumberToString(token));
if (token == ~peer.stateData1)
{
peer.state = PeerState_Connected;
OnConnectedToPeer(this);
}
else
{
NazaraNotice("Received wrong token (" + NumberToString(token) + " instead of " + NumberToString(~peer.stateData1) + ") from client " + peer.address.ToString());
return; //< Ignore
}
break;
}
case NetCode_RequestConnection:
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_RequestConnection from " + peerIp.ToString());
return; //< Ignore
case NetCode_Ping:
{
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_Ping from " + peerIp.ToString());
NetPacket pongPacket(NetCode_Pong);
EnqueuePacket(peer, PacketPriority_Low, PacketReliability_Unreliable, pongPacket);
break;
}
case NetCode_Pong:
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received NetCode_Pong from " + peerIp.ToString());
break;
default:
{
NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Received 0x" + NumberToString(packet.GetNetCode(), 16) + " from " + peerIp.ToString());
RUdpMessage receivedMessage;
receivedMessage.from = peerIp;
receivedMessage.data = std::move(packet);
m_receivedMessages.emplace(std::move(receivedMessage));
break;
}
}
if (!HasPendingPackets(peer))
{
peer.state = PeerState_WillAck;
peer.stateData1 = m_currentTime;
}
}
}
/*!
* \brief Sends a packet to a peer
*
* \param peer Data relative to the peer
* \param packet Pending packet
*/
void RUdpConnection::SendPacket(PeerData& peer, PendingPacket&& packet)
{
if (peer.state == PeerState_WillAck)
peer.state = PeerState_Connected;
SequenceIndex remoteSequence = peer.remoteSequence;
UInt32 previousAcks = 0;
for (SequenceIndex ack : peer.receivedQueue)
{
if (ack == remoteSequence)
continue;
unsigned int difference = ComputeSequenceDifference(remoteSequence, ack);
if (difference <= 32U)
previousAcks |= (1U << (difference - 1));
}
SequenceIndex sequenceId = ++peer.localSequence;
packet.data.GetStream()->SetCursorPos(NetPacket::HeaderSize + sizeof(UInt16)); ///< Protocol begin has already been filled
packet.data << sequenceId;
packet.data << remoteSequence;
packet.data << previousAcks;
m_socket.SendPacket(peer.address, packet.data);
PendingAckPacket pendingAckPacket;
pendingAckPacket.data = std::move(packet.data);
pendingAckPacket.priority = packet.priority;
pendingAckPacket.reliability = packet.reliability;
pendingAckPacket.sequenceId = sequenceId;
pendingAckPacket.timeSent = m_currentTime;
peer.pendingAckQueue.emplace_back(std::move(pendingAckPacket));
}
/*!
* \brief Initializes the RUdpConnection class
* \return true
*/
bool RUdpConnection::Initialize()
{
std::random_device device;
s_randomGenerator.seed(device());
return true;
}
/*!
* \brief Uninitializes the RUdpConnection class
*/
void RUdpConnection::Uninitialize()
{
}
std::mt19937_64 RUdpConnection::s_randomGenerator;
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/SocketPoller.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Network/Algorithm.hpp>
@@ -24,7 +25,7 @@ namespace Nz
/*!
* \ingroup network
* \class Nz::SocketPoller
* \brief Network class allowing an application to wait on multiples sockets for them to become active (readable)
* \brief Network class allowing an application to wait on multiples sockets for them to become active (readable/writeable)
*/
/*!
@@ -194,10 +195,10 @@ namespace Nz
if (error)
*error = waitError;
if (waitError != SocketError_NoError)
if (waitError != SocketError::NoError)
{
if (waitError != SocketError_Interrupted) //< Do not log interrupted error
NazaraError("SocketPoller encountered an error (code: 0x" + NumberToString(waitError, 16) + "): " + ErrorToString(waitError));
if (waitError != SocketError::Interrupted) //< Do not log interrupted error
NazaraError("SocketPoller encountered an error (code: 0x" + NumberToString(UnderlyingCast(waitError), 16) + "): " + ErrorToString(waitError));
return 0;
}

View File

@@ -56,7 +56,7 @@ namespace Nz
}
SocketState state = SocketImpl::Connect(m_handle, remoteAddress, &m_lastError);
m_peerAddress = (state != SocketState_NotConnected) ? remoteAddress : IpAddress::Invalid;
m_peerAddress = (state != SocketState::NotConnected) ? remoteAddress : IpAddress::Invalid;
UpdateState(state);
return state;
@@ -75,13 +75,13 @@ namespace Nz
SocketState TcpClient::Connect(const std::string& hostName, NetProtocol protocol, const std::string& service, ResolveError* error)
{
UpdateState(SocketState_Resolving);
UpdateState(SocketState::Resolving);
std::vector<HostnameInfo> results = IpAddress::ResolveHostname(protocol, hostName, service, error);
if (results.empty())
{
m_lastError = SocketError_ResolveError;
m_lastError = SocketError::ResolveError;
UpdateState(SocketState_NotConnected);
UpdateState(SocketState::NotConnected);
return m_state;
}
@@ -91,7 +91,7 @@ namespace Nz
if (!result.address)
continue;
if (result.socketType != SocketType_TCP)
if (result.socketType != SocketType::TCP)
continue;
hostnameAddress = result.address;
@@ -176,7 +176,7 @@ namespace Nz
/*!
* \brief Polls the connection status of the currently connecting socket
* \return New socket state, which maybe unchanged (if connecting is still pending), SocketState_Connected if connection is successful or SocketState_NotConnected if connection failed
* \return New socket state, which maybe unchanged (if connecting is still pending), SocketState::Connected if connection is successful or SocketState::NotConnected if connection failed
*
* This functions checks if the pending connection has either succeeded, failed or is still processing at the time of the call.
*
@@ -188,16 +188,16 @@ namespace Nz
{
switch (m_state)
{
case SocketState_Connecting:
case SocketState::Connecting:
{
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
SocketState newState = SocketImpl::PollConnection(m_handle, m_peerAddress, waitDuration, &m_lastError);
// Prevent valid peer address in non-connected state
if (newState == SocketState_NotConnected)
if (newState == SocketState::NotConnected)
{
m_openMode = OpenMode_NotOpen;
m_openMode = OpenMode::NotOpen;
m_peerAddress = IpAddress::Invalid;
}
@@ -205,16 +205,16 @@ namespace Nz
return newState;
}
case SocketState_Connected:
case SocketState_NotConnected:
case SocketState::Connected:
case SocketState::NotConnected:
return m_state;
case SocketState_Bound:
case SocketState_Resolving:
case SocketState::Bound:
case SocketState::Resolving:
break;
}
NazaraInternalError("Unexpected socket state (0x" + NumberToString(m_state, 16) + ')');
NazaraInternalError("Unexpected socket state (0x" + NumberToString(UnderlyingCast(m_state), 16) + ')');
return m_state;
}
@@ -240,9 +240,9 @@ namespace Nz
{
switch (m_lastError)
{
case SocketError_ConnectionClosed:
case SocketError_ConnectionRefused:
UpdateState(SocketState_NotConnected);
case SocketError::ConnectionClosed:
case SocketError::ConnectionRefused:
UpdateState(SocketState::NotConnected);
break;
default:
@@ -255,7 +255,7 @@ namespace Nz
if (received)
*received = read;
UpdateState(SocketState_Connected);
UpdateState(SocketState::Connected);
return true;
}
@@ -292,7 +292,7 @@ namespace Nz
UInt32 size;
if (!NetPacket::DecodeHeader(m_pendingPacket.data.GetConstBuffer(), &size, &m_pendingPacket.netcode))
{
m_lastError = SocketError_Packet;
m_lastError = SocketError::Packet;
NazaraWarning("Invalid header data");
return false;
}
@@ -379,9 +379,9 @@ namespace Nz
{
switch (m_lastError)
{
case SocketError_ConnectionClosed:
case SocketError_ConnectionRefused:
UpdateState(SocketState_NotConnected);
case SocketError::ConnectionClosed:
case SocketError::ConnectionRefused:
UpdateState(SocketState::NotConnected);
break;
default:
@@ -394,7 +394,7 @@ namespace Nz
totalByteSent += sentSize;
}
UpdateState(SocketState_Connected);
UpdateState(SocketState::Connected);
return true;
}
@@ -415,9 +415,9 @@ namespace Nz
{
switch (m_lastError)
{
case SocketError_ConnectionClosed:
case SocketError_ConnectionRefused:
UpdateState(SocketState_NotConnected);
case SocketError::ConnectionClosed:
case SocketError::ConnectionRefused:
UpdateState(SocketState::NotConnected);
break;
default:
@@ -433,7 +433,7 @@ namespace Nz
if (sent)
*sent = byteSent;
UpdateState(SocketState_Connected);
UpdateState(SocketState::Connected);
return true;
}
@@ -452,7 +452,7 @@ namespace Nz
const UInt8* ptr = static_cast<const UInt8*>(packet.OnSend(&size));
if (!ptr)
{
m_lastError = SocketError_Packet;
m_lastError = SocketError::Packet;
NazaraError("Failed to prepare packet");
return false;
}
@@ -491,20 +491,20 @@ namespace Nz
{
switch (m_state)
{
case SocketState_Connecting:
case SocketState::Connecting:
{
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
SocketState newState = SocketImpl::PollConnection(m_handle, m_peerAddress, (msTimeout > 0) ? msTimeout : std::numeric_limits<UInt64>::max(), &m_lastError);
// If connection is still pending after waiting, cancel it
if (newState == SocketState_Connecting)
newState = SocketState_NotConnected;
if (newState == SocketState::Connecting)
newState = SocketState::NotConnected;
// Prevent valid stats in non-connected state
if (newState == SocketState_NotConnected)
if (newState == SocketState::NotConnected)
{
m_openMode = OpenMode_NotOpen;
m_openMode = OpenMode::NotOpen;
m_peerAddress = IpAddress::Invalid;
}
@@ -512,16 +512,16 @@ namespace Nz
return newState;
}
case SocketState_Connected:
case SocketState_NotConnected:
case SocketState::Connected:
case SocketState::NotConnected:
return m_state;
case SocketState_Bound:
case SocketState_Resolving:
case SocketState::Bound:
case SocketState::Resolving:
break;
}
NazaraInternalError("Unhandled socket state (0x" + NumberToString(m_state, 16) + ')');
NazaraInternalError("Unhandled socket state (0x" + NumberToString(UnderlyingCast(m_state), 16) + ')');
return m_state;
}
@@ -541,7 +541,7 @@ namespace Nz
{
AbstractSocket::OnClose();
m_openMode = OpenMode_NotOpen;
m_openMode = OpenMode::NotOpen;
m_peerAddress = IpAddress::Invalid;
}
@@ -558,10 +558,10 @@ namespace Nz
SocketError errorCode;
if (!SocketImpl::SetNoDelay(m_handle, m_isLowDelayEnabled, &errorCode))
NazaraWarning("Failed to set socket no delay mode (0x" + NumberToString(errorCode, 16) + ')');
NazaraWarning("Failed to set socket no delay mode (0x" + NumberToString(UnderlyingCast(errorCode), 16) + ')');
if (!SocketImpl::SetKeepAlive(m_handle, m_isKeepAliveEnabled, m_keepAliveTime, m_keepAliveInterval, &errorCode))
NazaraWarning("Failed to set socket keep alive mode (0x" + NumberToString(errorCode, 16) + ')');
NazaraWarning("Failed to set socket keep alive mode (0x" + NumberToString(UnderlyingCast(errorCode), 16) + ')');
m_peerAddress = IpAddress::Invalid;
m_openMode = OpenMode_ReadWrite;
@@ -610,7 +610,7 @@ namespace Nz
Open(handle);
m_peerAddress = peerAddress;
m_openMode = OpenMode_ReadWrite;
UpdateState(SocketState_Connected);
UpdateState(SocketState::Connected);
}
/*!

View File

@@ -66,7 +66,7 @@ namespace Nz
Open(address.GetProtocol());
SocketState state = SocketImpl::Listen(m_handle, address, queueSize, &m_lastError);
if (state == SocketState_Bound)
if (state == SocketState::Bound)
m_boundAddress = SocketImpl::QuerySocketAddress(m_handle);
UpdateState(state);

View File

@@ -41,7 +41,7 @@ namespace Nz
NazaraAssert(address.IsValid(), "Invalid address");
SocketState state = SocketImpl::Bind(m_handle, address, &m_lastError);
if (state == SocketState_Bound)
if (state == SocketState::Bound)
m_boundAddress = SocketImpl::QuerySocketAddress(m_handle);
UpdateState(state);
@@ -104,8 +104,8 @@ namespace Nz
{
switch (m_lastError)
{
case SocketError_ConnectionClosed:
m_lastError = SocketError_NoError;
case SocketError::ConnectionClosed:
m_lastError = SocketError::NoError;
read = 0;
break;
@@ -140,8 +140,8 @@ namespace Nz
{
switch (m_lastError)
{
case SocketError_ConnectionClosed:
m_lastError = SocketError_NoError;
case SocketError::ConnectionClosed:
m_lastError = SocketError::NoError;
read = 0;
break;
@@ -166,14 +166,13 @@ namespace Nz
* \remark Produces a NazaraAssert if packet is invalid
* \remark Produces a NazaraWarning if packet's header is invalid
*/
bool UdpSocket::ReceivePacket(NetPacket* packet, IpAddress* from)
{
NazaraAssert(packet, "Invalid packet");
// I'm not sure what's the best between having a 65k bytes buffer ready for any datagram size
// or querying the next datagram size every time, for now I'll leave it as is
packet->Reset(NetCode_Invalid, std::numeric_limits<UInt16>::max());
packet->Reset(0, std::numeric_limits<UInt16>::max());
packet->Resize(std::numeric_limits<UInt16>::max());
std::size_t received;
@@ -187,14 +186,14 @@ namespace Nz
Nz::UInt32 packetSize;
if (!NetPacket::DecodeHeader(packet->GetConstData(), &packetSize, &netCode))
{
m_lastError = SocketError_Packet;
m_lastError = SocketError::Packet;
NazaraWarning("Invalid header data");
return false;
}
if (packetSize != received)
{
m_lastError = SocketError_Packet;
m_lastError = SocketError::Packet;
NazaraWarning("Invalid packet size (packet size is " + NumberToString(packetSize) + " bytes, received " + NumberToString(received) + " bytes)");
return false;
}
@@ -275,7 +274,7 @@ namespace Nz
const UInt8* ptr = static_cast<const UInt8*>(packet.OnSend(&size));
if (!ptr)
{
m_lastError = SocketError_Packet;
m_lastError = SocketError::Packet;
NazaraError("Failed to prepare packet");
return false;
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Win32/IpAddressImpl.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
@@ -187,7 +188,7 @@ namespace Nz
}
if (error)
*error = ResolveError_NoError;
*error = ResolveError::NoError;
return true;
}
@@ -228,7 +229,7 @@ namespace Nz
}
if (error)
*error = ResolveError_NoError;
*error = ResolveError::NoError;
return results;
}
@@ -239,7 +240,7 @@ namespace Nz
{
switch (ipAddress.GetProtocol())
{
case NetProtocol_IPv4:
case NetProtocol::IPv4:
{
sockaddr_in* socketAddress = reinterpret_cast<sockaddr_in*>(buffer);
@@ -251,7 +252,7 @@ namespace Nz
return sizeof(sockaddr_in);
}
case NetProtocol_IPv6:
case NetProtocol::IPv6:
{
sockaddr_in6* socketAddress = reinterpret_cast<sockaddr_in6*>(buffer);
@@ -271,7 +272,7 @@ namespace Nz
}
default:
NazaraInternalError("Unhandled ip protocol (0x" + NumberToString(ipAddress.GetProtocol()) + ')');
NazaraInternalError("Unhandled ip protocol (0x" + NumberToString(UnderlyingCast(ipAddress.GetProtocol())) + ')');
break;
}
}
@@ -285,13 +286,13 @@ namespace Nz
switch (family)
{
case PF_INET:
return NetProtocol_IPv4;
return NetProtocol::IPv4;
case PF_INET6:
return NetProtocol_IPv6;
return NetProtocol::IPv6;
default:
return NetProtocol_Unknown;
return NetProtocol::Unknown;
}
}
@@ -300,16 +301,16 @@ namespace Nz
switch (socketType)
{
case SOCK_STREAM:
return SocketType_TCP;
return SocketType::TCP;
case SOCK_DGRAM:
return SocketType_UDP;
return SocketType::UDP;
case SOCK_RAW:
return SocketType_Raw;
return SocketType::Raw;
default:
return SocketType_Unknown;
return SocketType::Unknown;
}
}
@@ -318,35 +319,35 @@ namespace Nz
switch (error)
{
case 0:
return ResolveError_NoError;
return ResolveError::NoError;
// Engine error
case WSAEFAULT:
case WSAEINVAL:
return ResolveError_Internal;
return ResolveError::Internal;
case WSAEAFNOSUPPORT:
case WSAESOCKTNOSUPPORT:
case WSASERVICE_NOT_FOUND:
return ResolveError_ProtocolNotSupported;
return ResolveError::ProtocolNotSupported;
case WSAHOST_NOT_FOUND:
return ResolveError_NotFound;
return ResolveError::NotFound;
case WSANO_RECOVERY:
return ResolveError_NonRecoverable;
return ResolveError::NonRecoverable;
case WSANOTINITIALISED:
return ResolveError_NotInitialized;
return ResolveError::NotInitialized;
case WSA_NOT_ENOUGH_MEMORY:
return ResolveError_ResourceError;
return ResolveError::ResourceError;
case WSATRY_AGAIN:
return ResolveError_TemporaryFailure;
return ResolveError::TemporaryFailure;
}
NazaraWarning("Unhandled WinSock error: " + Error::GetLastSystemError(error) + " (" + NumberToString(error) + ')');
return ResolveError_Unknown;
return ResolveError::Unknown;
}
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Win32/SocketImpl.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StackArray.hpp>
@@ -52,7 +53,7 @@ namespace Nz
*address = IpAddressImpl::FromSockAddr(reinterpret_cast<const sockaddr*>(&nameBuffer));
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
}
else
{
@@ -76,19 +77,19 @@ namespace Nz
if (error)
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return SocketState_Bound;
return SocketState::Bound;
}
SocketHandle SocketImpl::Create(NetProtocol protocol, SocketType type, SocketError* error)
{
NazaraAssert(protocol != NetProtocol_Any, "Any protocol is not supported for socket creation");
NazaraAssert(type <= SocketType_Max, "Type has value out of enum");
NazaraAssert(protocol != NetProtocol::Any, "Any protocol is not supported for socket creation");
NazaraAssert(type <= SocketType::Max, "Type has value out of enum");
SocketHandle handle = socket(TranslateNetProtocolToAF(protocol), TranslateSocketTypeToSock(type), 0);
if (handle == InvalidHandle && error != nullptr)
@@ -109,7 +110,7 @@ namespace Nz
{
NazaraAssert(handle != InvalidHandle, "Invalid handle");
if (GetLastError(handle, nullptr) < 0)
if (GetLastError(handle, nullptr) != SocketError::Internal)
NazaraWarning("Failed to clear socket error code: " + Error::GetLastSystemError(WSAGetLastError()));
}
@@ -122,7 +123,7 @@ namespace Nz
int bufferLength = IpAddressImpl::ToSockAddr(address, nameBuffer.data());
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
// Clear socket error status
ClearErrorCode(handle);
@@ -135,24 +136,24 @@ namespace Nz
case WSAEALREADY:
case WSAEINVAL: //< In case of connect, WSAEINVAL may be returned instead of WSAEALREADY
case WSAEWOULDBLOCK:
return SocketState_Connecting;
return SocketState::Connecting;
case WSAEISCONN:
return SocketState_Connected;
return SocketState::Connected;
}
if (error)
{
if (errorCode == WSAEADDRNOTAVAIL)
*error = SocketError_ConnectionRefused; //< ConnectionRefused seems more legit than AddressNotAvailable in connect case
*error = SocketError::ConnectionRefused; //< ConnectionRefused seems more legit than AddressNotAvailable in connect case
else
*error = TranslateWSAErrorToSocketError(errorCode);
}
return SocketState_NotConnected;
return SocketState::NotConnected;
}
return SocketState_Connected;
return SocketState::Connected;
}
bool SocketImpl::Initialize()
@@ -172,7 +173,7 @@ namespace Nz
{
int code = GetLastErrorCode(handle, error);
if (code < 0)
return SocketError_Internal;
return SocketError::Internal;
return TranslateWSAErrorToSocketError(code);
}
@@ -196,7 +197,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -214,7 +215,7 @@ namespace Nz
if (error)
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (listen(handle, queueSize) == SOCKET_ERROR)
@@ -222,13 +223,13 @@ namespace Nz
if (error)
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return SocketState_Bound;
return SocketState::Bound;
}
std::size_t SocketImpl::QueryAvailableBytes(SocketHandle handle, SocketError* error)
@@ -245,7 +246,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return availableBytes;
}
@@ -264,7 +265,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code == TRUE;
}
@@ -283,7 +284,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code == TRUE;
}
@@ -302,7 +303,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -321,7 +322,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code == TRUE;
}
@@ -340,7 +341,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -363,7 +364,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return IpAddressImpl::FromSockAddr(reinterpret_cast<sockaddr*>(nameBuffer.data()));
}
@@ -383,7 +384,7 @@ namespace Nz
{
int errorCode = WSAGetLastError();
if (errorCode == WSAEINVAL)
*error = SocketError_NoError;
*error = SocketError::NoError;
else
*error = TranslateWSAErrorToSocketError(errorCode);
}
@@ -392,7 +393,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return IpAddressImpl::FromSockAddr(reinterpret_cast<sockaddr*>(nameBuffer.data()));
}
@@ -411,7 +412,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return code;
}
@@ -436,7 +437,7 @@ namespace Nz
assert(result >= 0);
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return static_cast<unsigned int>(result);
#else
@@ -445,7 +446,7 @@ namespace Nz
NazaraUnused(timeout);
if (error)
*error = SocketError_NotSupported;
*error = SocketError::NotSupported;
return 0;
#endif
@@ -465,7 +466,7 @@ namespace Nz
if (error)
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
return SocketState_NotConnected;
return SocketState::NotConnected;
}
else if (ret > 0)
{
@@ -474,14 +475,14 @@ namespace Nz
if (error)
*error = GetLastError(handle);
return SocketState_NotConnected;
return SocketState::NotConnected;
}
else if (descriptor.revents & POLLWRNORM)
return SocketState_Connected;
return SocketState::Connected;
else
{
NazaraWarning("Socket " + std::to_string(handle) + " was returned by poll without POLLOUT nor error events (events: 0x" + NumberToString(descriptor.revents, 16) + ')');
return SocketState_NotConnected;
return SocketState::NotConnected;
}
}
else
@@ -490,12 +491,12 @@ namespace Nz
if (error)
{
if (msTimeout > 0)
*error = SocketError_TimedOut;
*error = SocketError::TimedOut;
else
*error = SocketError_NoError;
*error = SocketError::NoError;
}
return SocketState_Connecting;
return SocketState::Connecting;
}
}
@@ -529,7 +530,7 @@ namespace Nz
else if (byteRead == 0)
{
if (error)
*error = SocketError_ConnectionClosed;
*error = SocketError::ConnectionClosed;
return false; //< Connection has been closed
}
@@ -538,7 +539,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -581,7 +582,7 @@ namespace Nz
else if (byteRead == 0)
{
if (error)
*error = SocketError_ConnectionClosed;
*error = SocketError::ConnectionClosed;
return false; //< Connection closed
}
@@ -595,7 +596,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -650,7 +651,7 @@ namespace Nz
if (flags & MSG_PARTIAL)
{
if (error)
*error = SocketError_DatagramSize;
*error = SocketError::DatagramSize;
return false;
}
@@ -662,7 +663,7 @@ namespace Nz
*read = byteRead;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -698,7 +699,7 @@ namespace Nz
*sent = byteSent;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -744,7 +745,7 @@ namespace Nz
*sent = static_cast<int>(byteSent);
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -783,7 +784,7 @@ namespace Nz
*sent = byteSent;
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -802,7 +803,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -821,7 +822,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -841,12 +842,12 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
#else
if (error)
*error = SocketError_NotSupported;
*error = SocketError::NotSupported;
return false;
#endif
@@ -871,7 +872,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -890,7 +891,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -909,7 +910,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -928,7 +929,7 @@ namespace Nz
}
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
return true;
}
@@ -938,7 +939,7 @@ namespace Nz
switch (error)
{
case 0:
return SocketError_NoError;
return SocketError::NoError;
// Engine error
case WSAEACCES:
@@ -953,82 +954,82 @@ namespace Nz
case WSAEISCONN:
case WSAEWOULDBLOCK:
NazaraWarning("Internal error occurred: " + Error::GetLastSystemError(error) + " (" + NumberToString(error) + ')');
return SocketError_Internal;
return SocketError::Internal;
case WSAEADDRNOTAVAIL:
case WSAEADDRINUSE:
return SocketError_AddressNotAvailable;
return SocketError::AddressNotAvailable;
case WSAEAFNOSUPPORT:
case WSAEPFNOSUPPORT:
case WSAEOPNOTSUPP:
case WSAEPROTONOSUPPORT:
case WSAESOCKTNOSUPPORT:
return SocketError_NotSupported;
return SocketError::NotSupported;
case WSAECONNREFUSED:
return SocketError_ConnectionRefused;
return SocketError::ConnectionRefused;
case WSAECONNABORTED:
case WSAECONNRESET:
case WSAENOTCONN:
case WSAESHUTDOWN:
return SocketError_ConnectionClosed;
return SocketError::ConnectionClosed;
case WSAEMSGSIZE:
return SocketError_DatagramSize;
return SocketError::DatagramSize;
case WSAEMFILE:
case WSAENOBUFS:
case WSA_NOT_ENOUGH_MEMORY:
return SocketError_ResourceError;
return SocketError::ResourceError;
case WSAEHOSTUNREACH:
return SocketError_UnreachableHost;
return SocketError::UnreachableHost;
case WSAENETDOWN:
case WSAENETUNREACH:
return SocketError_NetworkError;
return SocketError::NetworkError;
case WSANOTINITIALISED:
return SocketError_NotInitialized;
return SocketError::NotInitialized;
case WSAETIMEDOUT:
return SocketError_TimedOut;
return SocketError::TimedOut;
}
NazaraWarning("Unhandled WinSock error: " + Error::GetLastSystemError(error) + " (" + NumberToString(error) + ')');
return SocketError_Unknown;
return SocketError::Unknown;
}
int SocketImpl::TranslateNetProtocolToAF(NetProtocol protocol)
{
NazaraAssert(protocol <= NetProtocol_Max, "Protocol has value out of enum");
NazaraAssert(protocol <= NetProtocol::Max, "Protocol has value out of enum");
static int addressFamily[] = {
AF_UNSPEC, //< NetProtocol_Any
AF_INET, //< NetProtocol_IPv4
AF_INET6, //< NetProtocol_IPv6
-1 //< NetProtocol_Unknown
AF_UNSPEC, //< NetProtocol::Any
AF_INET, //< NetProtocol::IPv4
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) == NetProtocolCount, "Address family array is incomplete");
return addressFamily[protocol];
return addressFamily[UnderlyingCast(protocol)];
}
int SocketImpl::TranslateSocketTypeToSock(SocketType type)
{
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[] = {
SOCK_RAW, //< SocketType_Raw
SOCK_STREAM, //< SocketType_TCP
SOCK_DGRAM, //< SocketType_UDP
-1 //< SocketType_Unknown
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");
static_assert(sizeof(socketType) / sizeof(int) == SocketTypeCount, "Socket type array is incomplete");
return socketType[type];
return socketType[UnderlyingCast(type)];
}
void SocketImpl::Uninitialize()

View File

@@ -74,10 +74,10 @@ namespace Nz
0
};
if (eventFlags & SocketPollEvent_Read)
if (eventFlags & SocketPollEvent::Read)
entry.events |= POLLRDNORM;
if (eventFlags & SocketPollEvent_Write)
if (eventFlags & SocketPollEvent::Write)
entry.events |= POLLWRNORM;
m_allSockets[socket] = m_sockets.size();
@@ -85,7 +85,7 @@ namespace Nz
#else
for (std::size_t i = 0; i < 2; ++i)
{
if ((eventFlags & ((i == 0) ? SocketPollEvent_Read : SocketPollEvent_Write)) == 0)
if ((eventFlags & ((i == 0) ? SocketPollEvent::Read : SocketPollEvent::Write)) == 0)
continue;
fd_set& targetSet = (i == 0) ? m_readSockets : m_writeSockets;
@@ -202,7 +202,7 @@ namespace Nz
activeSockets = static_cast<unsigned int>(selectValue);
if (error)
*error = SocketError_NoError;
*error = SocketError::NoError;
#endif
return activeSockets;