diff options
author | rubidium42 <rubidium@openttd.org> | 2021-04-30 19:21:02 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-05-01 19:36:22 +0200 |
commit | e097c83c83ac3be81041a67f8d641650045502fb (patch) | |
tree | a47af42910ef415f110c9994cc696d22b63b9d2d /src/network/core/os_abstraction.cpp | |
parent | 22720332eb9922e20148c7aae1127f7304f6f7d3 (diff) | |
download | openttd-e097c83c83ac3be81041a67f8d641650045502fb.tar.xz |
Codechange: move some OS abstraction method implementations out of the header
Diffstat (limited to 'src/network/core/os_abstraction.cpp')
-rw-r--r-- | src/network/core/os_abstraction.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/network/core/os_abstraction.cpp b/src/network/core/os_abstraction.cpp index 75f2224eb..b2d3475d0 100644 --- a/src/network/core/os_abstraction.cpp +++ b/src/network/core/os_abstraction.cpp @@ -123,3 +123,52 @@ bool NetworkError::HasError() const return NetworkError(errno); #endif } + + +/** + * Try to set the socket into non-blocking mode. + * @param d The socket to set the non-blocking more for. + * @return True if setting the non-blocking mode succeeded, otherwise false. + */ +bool SetNonBlocking(SOCKET d) +{ +#if defined(_WIN32) + u_long nonblocking = 1; + return ioctlsocket(d, FIONBIO, &nonblocking) == 0; +#elif defined __EMSCRIPTEN__ + return true; +#else + int nonblocking = 1; + return ioctl(d, FIONBIO, &nonblocking) == 0; +#endif +} + +/** + * Try to set the socket to not delay sending. + * @param d The socket to disable the delaying for. + * @return True if disabling the delaying succeeded, otherwise false. + */ +bool SetNoDelay(SOCKET d) +{ +#ifdef __EMSCRIPTEN__ + return true; +#else + int flags = 1; + /* The (const char*) cast is needed for windows */ + return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char *)&flags, sizeof(flags)) == 0; +#endif +} + +/** + * Get the error from a socket, if any. + * @param d The socket to get the error from. + * @return The errno on the socket. + */ +NetworkError GetSocketError(SOCKET d) +{ + int err; + socklen_t len = sizeof(err); + getsockopt(d, SOL_SOCKET, SO_ERROR, (char *)&err, &len); + + return NetworkError(err); +} |