diff options
Diffstat (limited to 'contrib/Socket.cpp')
-rw-r--r-- | contrib/Socket.cpp | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/contrib/Socket.cpp b/contrib/Socket.cpp index c87606a..cd70a8e 100644 --- a/contrib/Socket.cpp +++ b/contrib/Socket.cpp @@ -381,7 +381,7 @@ bool TCPSocket::valid() const return m_sock != -1; } -void TCPSocket::connect(const std::string& hostname, int port, bool nonblock) +void TCPSocket::connect(const std::string& hostname, int port) { if (m_sock != INVALID_SOCKET) { throw std::logic_error("You may only connect an invalid TCPSocket"); @@ -415,16 +415,10 @@ void TCPSocket::connect(const std::string& hostname, int port, bool nonblock) if (sfd == -1) continue; - if (nonblock) { - int flags = fcntl(sfd, F_GETFL); - if (fcntl(sfd, F_SETFL, flags | O_NONBLOCK) == -1) { - std::string errstr(strerror(errno)); - throw std::runtime_error("TCP: Could not set O_NONBLOCK: " + errstr); - } - } - int ret = ::connect(sfd, rp->ai_addr, rp->ai_addrlen); if (ret != -1 or (ret == -1 and errno == EINPROGRESS)) { + // As the TCPClient could set the socket to nonblocking, we + // must handle EINPROGRESS here m_sock = sfd; break; } @@ -699,8 +693,13 @@ ssize_t TCPClient::recv(void *buffer, size_t length, int flags, int timeout_ms) void TCPClient::reconnect() { - const bool nonblock = true; - m_sock.connect(m_hostname, m_port, nonblock); + int flags = fcntl(m_sock.m_sock, F_GETFL); + if (fcntl(m_sock.m_sock, F_SETFL, flags | O_NONBLOCK) == -1) { + std::string errstr(strerror(errno)); + throw std::runtime_error("TCP: Could not set O_NONBLOCK: " + errstr); + } + + m_sock.connect(m_hostname, m_port); } TCPConnection::TCPConnection(TCPSocket&& sock) : |