From e2e1a0f374aeffc933d0e5d295607197d6b74a4a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 10 Oct 2016 10:10:24 +0200 Subject: Fix dabOutputTCP shutdown using accept() with timeout --- src/TcpSocket.cpp | 28 ++++++++++++++++++++++++++++ src/TcpSocket.h | 3 +++ src/dabOutput/dabOutputTcp.cpp | 7 ++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index 13efece..6e7c31d 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -166,6 +166,34 @@ TcpSocket TcpSocket::accept() } } +boost::optional TcpSocket::accept(int timeout_ms) +{ + fd_set rfds; + struct timeval tv; + int retval; + + FD_ZERO(&rfds); + FD_SET(m_sock, &rfds); + + tv.tv_sec = 0; + tv.tv_usec = 1000ul * timeout_ms; + + retval = select(1, &rfds, NULL, NULL, &tv); + + if (retval == -1) { + stringstream ss; + ss << "TCP Socket accept error: " << strerror(errno); + throw std::runtime_error(ss.str()); + } + else if (retval) { + return accept(); + } + else { + return boost::none; + } +} + + InetAddress TcpSocket::getOwnAddress() const { return m_own_address; diff --git a/src/TcpSocket.h b/src/TcpSocket.h index f1354a7..5a4a808 100644 --- a/src/TcpSocket.h +++ b/src/TcpSocket.h @@ -46,6 +46,8 @@ #include #include +#include + /** * This class represents a TCP socket. */ @@ -84,6 +86,7 @@ class TcpSocket void listen(void); TcpSocket accept(void); + boost::optional accept(int timeout_ms); /** Retrieve address this socket is bound to */ InetAddress getOwnAddress() const; diff --git a/src/dabOutput/dabOutputTcp.cpp b/src/dabOutput/dabOutputTcp.cpp index 2c5a067..343ba0f 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -128,9 +128,14 @@ class TCPDataDispatcher void process(long) { m_listener_socket.listen(); + const int timeout_ms = 1000; + while (m_running) { // Add a new TCPConnection to the list, constructing it from the client socket - m_connections.emplace(m_connections.begin(), m_listener_socket.accept()); + auto optional_sock = m_listener_socket.accept(timeout_ms); + if (optional_sock) { + m_connections.emplace(m_connections.begin(), std::move(*optional_sock)); + } } } -- cgit v1.2.3