diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/TcpSocket.cpp | 28 | ||||
-rw-r--r-- | src/TcpSocket.h | 3 | ||||
-rw-r--r-- | src/dabOutput/dabOutputTcp.cpp | 7 |
3 files changed, 37 insertions, 1 deletions
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> 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 <iostream> #include <string> +#include <boost/optional.hpp> + /** * This class represents a TCP socket. */ @@ -84,6 +86,7 @@ class TcpSocket void listen(void); TcpSocket accept(void); + boost::optional<TcpSocket> 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)); + } } } |