14#ifndef NETWORK_CORE_OS_ABSTRACTION_H
15#define NETWORK_CORE_OS_ABSTRACTION_H
47typedef unsigned long in_addr_t;
50#if defined(__MINGW32__) && !defined(AI_ADDRCONFIG)
51# define AI_ADDRCONFIG 0x00000400
54#if !(defined(__MINGW32__) || defined(__CYGWIN__))
56 typedef SSIZE_T ssize_t;
57 typedef int socklen_t;
58# define IPPROTO_IPV6 41
64# if defined(OPENBSD) || defined(__NetBSD__)
65# define AI_ADDRCONFIG 0
68# define INVALID_SOCKET -1
69# define closesocket close
75# include <sys/ioctl.h>
76# include <sys/socket.h>
77# include <netinet/in.h>
78# include <netinet/tcp.h>
79# include <arpa/inet.h>
82# if !defined(INADDR_NONE)
83# define INADDR_NONE 0xffffffff
86# if defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
87 typedef uint32_t in_addr_t;
94# if defined(__EMSCRIPTEN__)
97# define AI_ADDRCONFIG 0
101# define FD_SETSIZE 64
105# if defined(__HAIKU__)
107# define FD_SETSIZE 512
124inline socklen_t FixAddrLenForEmscripten(
struct sockaddr_storage &address)
126 switch (address.ss_family) {
127 case AF_INET6:
return sizeof(
struct sockaddr_in6);
128 case AF_INET:
return sizeof(
struct sockaddr_in);
129 default: NOT_REACHED();
141static_assert(
sizeof(in_addr) == 4);
142static_assert(
sizeof(in6_addr) == 16);
147 ssize_t operator()(std::span<const uint8_t> buffer)
149 return send(this->sock,
reinterpret_cast<const char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0);
156 ssize_t operator()(std::span<uint8_t> buffer)
158 return recv(this->sock,
reinterpret_cast<char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0);
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
std::string_view AsString() const
Get the string representation of the error message.
bool WouldBlock() const
Check whether this error describes that the operation would block.
bool IsConnectInProgress() const
Check whether this error describes a connect is in progress.
int error
The underlying error number from errno or WSAGetLastError.
bool HasError() const
Check whether an error was actually set.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
std::string message
The string representation of the error (set on first call to AsString).
static NetworkError GetLast()
Get the last network error.
bool SetReusePort(SOCKET d)
Try to set the socket to reuse ports.
bool SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
NetworkError GetSocketError(SOCKET d)
Get the error from a socket, if any.
IPv4 addresses should be 4 bytes.