diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/sockets.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/sockets.cpp b/src/sockets.cpp index f2e0d65f6..418b1b334 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -65,6 +65,34 @@ int Socket::GetSock() const return sock; } +/** Mark a socket as blockig + * @return true if the socket is now blocking + */ +bool Socket::SetBlocking() +{ +#ifdef _WIN32 + unsigned long opt = 0; + return !ioctlsocket(this->GetSock(), FIONBIO, &opt); +#else + int flags = fcntl(this->GetSock(), F_GETFL, 0); + return !fcntl(this->GetSock(), F_SETFL, flags & ~O_NONBLOCK); +#endif +} + +/** Mark a socket as non-blocking + * @return true if the socket is now non-blocking + */ +bool Socket::SetNonBlocking() +{ +#ifdef _WIN32 + unsigned long opt = 0; + return !ioctlsocket(this->GetSock(), FIONBIO, &opt); +#else + int flags = fcntl(this->GetSock(), F_GETFL, 0); + return !fcntl(this->GetSock(), F_SETFL, flags | O_NONBLOCK); +#endif +} + /** Check if this socket is IPv6 * @return true or false */ @@ -305,6 +333,8 @@ ClientSocket::ClientSocket(const std::string &nTargetHost, int nPort, const std: throw SocketException("Error connecting to server: " + std::string(strerror(errno))); } } + + this->SetNonBlocking(); } /** Default destructor @@ -375,6 +405,8 @@ ListenSocket::ListenSocket(const std::string &bindip, int port) : Socket(0, (bin { throw SocketException("Unable to listen: " + std::string(strerror(errno))); } + + this->SetNonBlocking(); } /** Destructor |