19#include "../../stdafx.h"
21#include "../../string_func.h"
22#include "../../3rdparty/fmt/format.h"
25#include "../../safeguards.h"
43 return this->
error == WSAEWOULDBLOCK;
47 return this->
error == EWOULDBLOCK || this->
error == EAGAIN;
58 return this->
error == WSAECONNRESET;
60 return this->
error == ECONNRESET;
71 return this->
error == WSAEWOULDBLOCK;
73 return this->
error == EINPROGRESS;
86 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, this->
error,
87 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer,
static_cast<DWORD
>(std::size(buffer)),
nullptr) == 0) {
88 this->
message.assign(fmt::format(
"Unknown error {}", this->
error));
98 static std::mutex mutex;
99 std::lock_guard<std::mutex> guard(mutex);
112 return this->
error != 0;
137 u_long nonblocking = 1;
138 return ioctlsocket(d, FIONBIO, &nonblocking) == 0;
139#elif defined __EMSCRIPTEN__
143 return ioctl(d, FIONBIO, &nonblocking) == 0;
159 return setsockopt(d, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<const char *
>(&flags),
sizeof(flags)) == 0;
173 return setsockopt(d, SOL_SOCKET, SO_REUSEADDR, (
const char *)&reuse_port,
sizeof(reuse_port)) == 0;
176 return setsockopt(d, SOL_SOCKET, SO_REUSEPORT, &reuse_port,
sizeof(reuse_port)) == 0;
188 socklen_t len =
sizeof(err);
189 if (getsockopt(d, SOL_SOCKET, SO_ERROR, (
char *)&err, &len) != 0)
return NetworkError(-1,
"Could not get error for socket");
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.
Network stuff has many things that needs to be included and/or implemented by default.
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.