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 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/TcpSocket.cpp') 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; -- cgit v1.2.3 From f16f9c0634693ce0a53bb269aa2d36402e51f92f Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 10 Oct 2016 11:19:15 +0200 Subject: Replace select() by poll() for TcpSocket accept --- src/TcpSocket.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'src/TcpSocket.cpp') diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index 6e7c31d..433e5c1 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -32,6 +32,7 @@ #include #include #include +#include using namespace std; @@ -168,17 +169,11 @@ TcpSocket TcpSocket::accept() boost::optional TcpSocket::accept(int timeout_ms) { - fd_set rfds; - struct timeval tv; - int retval; + struct pollfd fds[1]; + fds[0].fd = m_sock; + fds[0].events = POLLIN | POLLOUT; - 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); + int retval = poll(fds, 1, timeout_ms); if (retval == -1) { stringstream ss; -- cgit v1.2.3