14 #ifndef NETWORK_CORE_OS_ABSTRACTION_H
15 #define NETWORK_CORE_OS_ABSTRACTION_H
47 typedef 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;
91 # include <sys/time.h>
94 # if defined(__EMSCRIPTEN__)
97 # define AI_ADDRCONFIG 0
101 # define FD_SETSIZE 64
105 # if defined(__HAIKU__)
107 # define FD_SETSIZE 512
112 #ifdef __EMSCRIPTEN__
124 inline 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();
141 static_assert(
sizeof(in_addr) == 4);
142 static_assert(
sizeof(in6_addr) == 16);
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
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).
const std::string & AsString() const
Get the string representation of the error message.
NetworkError(int error)
Construct the network error with the given error code.
static NetworkError GetLast()
Get the last network error.
bool SetNonBlocking([[maybe_unused]] SOCKET d)
Try to set the socket into non-blocking mode.
bool SetNoDelay([[maybe_unused]] SOCKET d)
Try to set the socket to not delay sending.
bool SetReusePort(SOCKET d)
Try to set the socket to reuse ports.
NetworkError GetSocketError(SOCKET d)
Get the error from a socket, if any.