diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-07-16 15:57:27 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-07-16 15:57:27 +0200 |
commit | de99fe82c3e7738c05799c93192d879f9d56187e (patch) | |
tree | cf338c039193c84c13c3fd5fe42e0c38a0aae5a0 | |
parent | 8393d8ca08beb7d4aef39e7292b7e1b31c54e29d (diff) | |
download | ODR-AudioEnc-de99fe82c3e7738c05799c93192d879f9d56187e.tar.gz ODR-AudioEnc-de99fe82c3e7738c05799c93192d879f9d56187e.tar.bz2 ODR-AudioEnc-de99fe82c3e7738c05799c93192d879f9d56187e.zip |
Update Socket.h and .cpp
-rw-r--r-- | contrib/Socket.cpp | 21 | ||||
-rw-r--r-- | contrib/Socket.h | 2 |
2 files changed, 12 insertions, 11 deletions
diff --git a/contrib/Socket.cpp b/contrib/Socket.cpp index cd70a8e..c87606a 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) +void TCPSocket::connect(const std::string& hostname, int port, bool nonblock) { if (m_sock != INVALID_SOCKET) { throw std::logic_error("You may only connect an invalid TCPSocket"); @@ -415,10 +415,16 @@ void TCPSocket::connect(const std::string& hostname, int port) 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; } @@ -693,13 +699,8 @@ ssize_t TCPClient::recv(void *buffer, size_t length, int flags, int timeout_ms) void TCPClient::reconnect() { - 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); + const bool nonblock = true; + m_sock.connect(m_hostname, m_port, nonblock); } TCPConnection::TCPConnection(TCPSocket&& sock) : diff --git a/contrib/Socket.h b/contrib/Socket.h index 8bb7fe1..c3c37e1 100644 --- a/contrib/Socket.h +++ b/contrib/Socket.h @@ -162,7 +162,7 @@ class TCPSocket { TCPSocket& operator=(TCPSocket&& other); bool valid(void) const; - void connect(const std::string& hostname, int port); + void connect(const std::string& hostname, int port, bool nonblock = false); void listen(int port, const std::string& name); void close(void); |