aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/Socket.cpp
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-09-05 11:07:37 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-09-05 11:07:37 +0200
commit579c88d151d55ea9bf302fe7006cbee8c8fa1d4e (patch)
treea4498cf68ffdad6887653ce0b1d520c99c7916b5 /contrib/Socket.cpp
parent29d602422cea4e9f2a59b8cefdb17b3862642e01 (diff)
downloadODR-AudioEnc-579c88d151d55ea9bf302fe7006cbee8c8fa1d4e.tar.gz
ODR-AudioEnc-579c88d151d55ea9bf302fe7006cbee8c8fa1d4e.tar.bz2
ODR-AudioEnc-579c88d151d55ea9bf302fe7006cbee8c8fa1d4e.zip
Pull lib/Socket from mmbtools common 162c68c
Diffstat (limited to 'contrib/Socket.cpp')
-rw-r--r--contrib/Socket.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/contrib/Socket.cpp b/contrib/Socket.cpp
index cd70a8e..0c3cbb4 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,21 @@ void TCPSocket::connect(const std::string& hostname, int port)
if (sfd == -1)
continue;
+ if (nonblock) {
+ int flags = fcntl(sfd, F_GETFL);
+ if (flags == -1) {
+ std::string errstr(strerror(errno));
+ throw std::runtime_error("TCP: Could not get socket flags: " + errstr);
+ }
+
+ 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;
}
@@ -673,9 +684,6 @@ ssize_t TCPClient::recv(void *buffer, size_t length, int flags, int timeout_ms)
if (ret == 0) {
m_sock.close();
-
- TCPSocket newsock;
- m_sock = std::move(newsock);
reconnect();
}
@@ -693,13 +701,9 @@ 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);
+ TCPSocket newsock;
+ m_sock = std::move(newsock);
+ m_sock.connect(m_hostname, m_port, true);
}
TCPConnection::TCPConnection(TCPSocket&& sock) :