10#ifndef NETWORK_CORE_OS_ABSTRACTION_H
11#define NETWORK_CORE_OS_ABSTRACTION_H
43typedef unsigned long in_addr_t;
46#if defined(__MINGW32__) && !defined(AI_ADDRCONFIG)
47# define AI_ADDRCONFIG 0x00000400
50#if !(defined(__MINGW32__) || defined(__CYGWIN__))
52 typedef int socklen_t;
53# define IPPROTO_IPV6 41
59# if defined(OPENBSD) || defined(__NetBSD__)
60# define AI_ADDRCONFIG 0
63# define INVALID_SOCKET -1
64# define closesocket close
70# include <sys/ioctl.h>
71# include <sys/socket.h>
72# include <netinet/in.h>
73# include <netinet/tcp.h>
74# include <arpa/inet.h>
77# if !defined(INADDR_NONE)
78# define INADDR_NONE 0xffffffff
81# if defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 1)
82 typedef uint32_t in_addr_t;
89# if defined(__EMSCRIPTEN__)
92# define AI_ADDRCONFIG 0
100# if defined(__HAIKU__)
102# define FD_SETSIZE 512
119inline socklen_t FixAddrLenForEmscripten(
struct sockaddr_storage &address)
121 switch (address.ss_family) {
122 case AF_INET6:
return sizeof(
struct sockaddr_in6);
123 case AF_INET:
return sizeof(
struct sockaddr_in);
124 default: NOT_REACHED();
136static_assert(
sizeof(in_addr) == 4);
137static_assert(
sizeof(in6_addr) == 16);
150 return send(this->sock,
reinterpret_cast<const char *
>(buffer.data()),
static_cast<int>(buffer.size()), 0);
165 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).
NetworkError(int error, std::string_view message={})
Construct the network error with the given error code.
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.
Helper for Packet::TransferIn that reads data from a socket.
std::ptrdiff_t operator()(std::span< uint8_t > buffer)
Read data from the socket into the buffer.
SOCKET sock
The socket we're receiving data from.
IPv4 addresses should be 4 bytes.
SOCKET sock
The socket we're sending data to.
std::ptrdiff_t operator()(std::span< const uint8_t > buffer)
Write the buffer to the socket.