Split IP stuff into separate source and WIP implementation of name resolution.
This commit is contained in:
@@ -1,23 +1,11 @@
|
||||
|
||||
#include "./socket.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "./detail/net_common.hpp"
|
||||
#include "../detect.hpp"
|
||||
#include "../util/string.hpp"
|
||||
#include "../util/variant.hpp"
|
||||
|
||||
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
#include <WinSock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include "../util/winundef.hpp"
|
||||
#endif
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
namespace
|
||||
@@ -103,7 +91,19 @@ bool osSetSocketNonBlocking(int socket, bool blocking)
|
||||
using in_addr_t = ULONG;
|
||||
|
||||
const char SOCKOPT_ONE = 1;
|
||||
thread_local int numSocketsOpen = 0;
|
||||
thread_local bool gWsaInited = false;
|
||||
|
||||
class WSAGuard
|
||||
{
|
||||
public:
|
||||
~WSAGuard() noexcept
|
||||
{
|
||||
if (gWsaInited)
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
}
|
||||
} thread_local [[maybe_unused]] gWsaGuard;
|
||||
|
||||
long osRecv(SOCKET socket, std::span<std::uint8_t> buffer, int flags)
|
||||
{
|
||||
@@ -117,35 +117,16 @@ long osSend(SOCKET socket, std::span<const std::uint8_t> buffer, int flags)
|
||||
|
||||
SOCKET osCreateSocket(int addressFamily, int type, int protocol)
|
||||
{
|
||||
if (numSocketsOpen == 0)
|
||||
if (!detail::initWSA())
|
||||
{
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
||||
{
|
||||
return INVALID_SOCKET_HANDLE;
|
||||
}
|
||||
return INVALID_SOCKET_HANDLE;
|
||||
}
|
||||
SOCKET result = socket(addressFamily, type, protocol);
|
||||
if (result != INVALID_SOCKET_HANDLE)
|
||||
{
|
||||
++numSocketsOpen;
|
||||
}
|
||||
return result;
|
||||
return socket(addressFamily, type, protocol);
|
||||
}
|
||||
|
||||
int osCloseSocket(SOCKET socket)
|
||||
{
|
||||
const int result = closesocket(socket);
|
||||
if (result == 0)
|
||||
{
|
||||
MIJIN_ASSERT(numSocketsOpen > 0, "Inbalanced calls to osOpenSocket and osCloseSocket!");
|
||||
--numSocketsOpen;
|
||||
if (numSocketsOpen == 0)
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return closesocket(socket);
|
||||
}
|
||||
|
||||
bool osIsSocketValid(SOCKET socket)
|
||||
@@ -161,6 +142,45 @@ bool osSetSocketNonBlocking(SOCKET socket, bool blocking)
|
||||
#endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
#if MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
bool initWSA() noexcept
|
||||
{
|
||||
if (gWsaInited)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
gWsaInited = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
StreamError translateWSAError() noexcept
|
||||
{
|
||||
// TODO
|
||||
return StreamError::UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
StreamError translateWinError(DWORD error) noexcept
|
||||
{
|
||||
// TODO
|
||||
(void) error;
|
||||
return StreamError::UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
StreamError translateWinError() noexcept
|
||||
{
|
||||
return translateWinError(GetLastError());
|
||||
}
|
||||
#endif // MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
|
||||
}// namespace impl
|
||||
|
||||
Optional<IPv4Address> IPv4Address::fromString(std::string_view stringView) noexcept
|
||||
{
|
||||
std::vector<std::string_view> parts = split(stringView, ".", {.limitParts = 4});
|
||||
@@ -280,11 +300,11 @@ mijin::Task<StreamError> TCPStream::c_readRaw(std::span<std::uint8_t> buffer, co
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
else if (bytesRead == 0)
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
co_return StreamError::CONNECTION_CLOSED;
|
||||
}
|
||||
else if (errno != EAGAIN)
|
||||
if (errno != EAGAIN)
|
||||
{
|
||||
co_return translateErrno();
|
||||
}
|
||||
@@ -292,7 +312,7 @@ mijin::Task<StreamError> TCPStream::c_readRaw(std::span<std::uint8_t> buffer, co
|
||||
}
|
||||
}
|
||||
|
||||
mijin::Task<StreamError> TCPStream::c_writeRaw(std::span<const std::uint8_t> buffer)
|
||||
Task<StreamError> TCPStream::c_writeRaw(std::span<const std::uint8_t> buffer)
|
||||
{
|
||||
MIJIN_ASSERT(isOpen(), "Socket is not open.");
|
||||
|
||||
@@ -318,7 +338,7 @@ mijin::Task<StreamError> TCPStream::c_writeRaw(std::span<const std::uint8_t> buf
|
||||
{
|
||||
co_return translateErrno();
|
||||
}
|
||||
co_await mijin::c_suspend();
|
||||
co_await c_suspend();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +358,7 @@ std::size_t TCPStream::tell()
|
||||
return 0;
|
||||
}
|
||||
|
||||
StreamError TCPStream::seek(std::intptr_t /* pos */, mijin::SeekMode /* seekMode */)
|
||||
StreamError TCPStream::seek(std::intptr_t /* pos */, SeekMode /* seekMode */)
|
||||
{
|
||||
return StreamError::NOT_SUPPORTED;
|
||||
}
|
||||
@@ -396,7 +416,7 @@ StreamError TCPStream::open(ip_address_t address, std::uint16_t port) noexcept
|
||||
{
|
||||
sockaddr_in6 connectAddress =
|
||||
{
|
||||
.sin6_family = AF_INET,
|
||||
.sin6_family = AF_INET6,
|
||||
.sin6_port = htons(port),
|
||||
.sin6_addr = std::bit_cast<in6_addr>(address6)
|
||||
};
|
||||
@@ -458,7 +478,7 @@ StreamError TCPServerSocket::setup(ip_address_t address, std::uint16_t port) noe
|
||||
{
|
||||
sockaddr_in6 bindAddress =
|
||||
{
|
||||
.sin6_family = AF_INET,
|
||||
.sin6_family = AF_INET6,
|
||||
.sin6_port = htons(port),
|
||||
.sin6_addr = std::bit_cast<in6_addr>(address6)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user