summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2016-10-10 10:10:24 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2016-10-10 10:10:24 +0200
commite2e1a0f374aeffc933d0e5d295607197d6b74a4a (patch)
tree1878bc61a66d221d3e4395e10e1d72aa487fb36e /src
parenteccfa8ad3774205a929ff70090540d24674618a1 (diff)
downloaddabmux-e2e1a0f374aeffc933d0e5d295607197d6b74a4a.tar.gz
dabmux-e2e1a0f374aeffc933d0e5d295607197d6b74a4a.tar.bz2
dabmux-e2e1a0f374aeffc933d0e5d295607197d6b74a4a.zip
Fix dabOutputTCP shutdown using accept() with timeout
Diffstat (limited to 'src')
-rw-r--r--src/TcpSocket.cpp28
-rw-r--r--src/TcpSocket.h3
-rw-r--r--src/dabOutput/dabOutputTcp.cpp7
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));
+ }
}
}