diff options
author | Adam <Adam@anope.org> | 2010-07-24 13:45:54 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2010-07-24 13:45:54 -0400 |
commit | 2328c3e7ec718071a8cafcc396d072f90fb4dbc2 (patch) | |
tree | 7a77b1d8def8a7621c862007001919a05a6ad3ab /src/sockets.cpp | |
parent | b218d52a31442214bf3bfdb5cc171fd5384f37c8 (diff) |
Always use non-blocking sockets
Diffstat (limited to 'src/sockets.cpp')
-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 |