From 7512c84d0cd7d9b1a3733e84280864f5a989b51e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 30 Oct 2016 12:18:14 +0100 Subject: Catch runtime_error in TCPDataDispatcher thread --- src/dabOutput/dabOutputTcp.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/dabOutput') diff --git a/src/dabOutput/dabOutputTcp.cpp b/src/dabOutput/dabOutputTcp.cpp index 8696bec..6485d15 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -126,17 +126,23 @@ class TCPDataDispatcher private: void process(long) { - m_listener_socket.listen(); + try { + m_listener_socket.listen(); - const int timeout_ms = 1000; + const int timeout_ms = 1000; - while (m_running) { - // Add a new TCPConnection to the list, constructing it from the client socket - auto optional_sock = m_listener_socket.accept(timeout_ms); - if (optional_sock) { - m_connections.emplace(m_connections.begin(), std::move(*optional_sock)); + while (m_running) { + // Add a new TCPConnection to the list, constructing it from the client socket + auto optional_sock = m_listener_socket.accept(timeout_ms); + if (optional_sock) { + m_connections.emplace(m_connections.begin(), std::move(*optional_sock)); + } } } + catch (std::runtime_error& e) { + etiLog.level(error) << "TCPDataDispatcher caught runtime error: " << e.what(); + m_running = false; + } } atomic m_running; -- cgit v1.2.3 From 90d77644cf5a715379019e4f09cb5868a02f3c69 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 30 Oct 2016 12:23:54 +0100 Subject: Avoid manual pointer management of the TCPDataDispatcher --- src/dabOutput/dabOutput.h | 7 +------ src/dabOutput/dabOutputTcp.cpp | 10 +--------- 2 files changed, 2 insertions(+), 15 deletions(-) (limited to 'src/dabOutput') diff --git a/src/dabOutput/dabOutput.h b/src/dabOutput/dabOutput.h index eaa623f..c10d358 100644 --- a/src/dabOutput/dabOutput.h +++ b/src/dabOutput/dabOutput.h @@ -222,11 +222,6 @@ class TCPDataDispatcher; class DabOutputTcp : public DabOutput { public: - DabOutputTcp() {} - DabOutputTcp(const DabOutputTcp& other) = delete; - const DabOutputTcp& operator=(const DabOutputTcp& other) = delete; - ~DabOutputTcp(); - int Open(const char* name); int Write(void* buffer, int size); int Close(); @@ -238,7 +233,7 @@ class DabOutputTcp : public DabOutput private: std::string uri_; - TCPDataDispatcher* dispatcher_; + std::shared_ptr dispatcher_; }; // -------------- Simul ------------------ diff --git a/src/dabOutput/dabOutputTcp.cpp b/src/dabOutput/dabOutputTcp.cpp index 6485d15..94d379e 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -151,14 +151,6 @@ class TCPDataDispatcher std::list m_connections; }; -DabOutputTcp::~DabOutputTcp() -{ - if (dispatcher_) { - delete dispatcher_; - dispatcher_ = nullptr; - } -} - static bool parse_uri(const char *uri, long *port, string& addr) { char* const hostport = strdup(uri); // the uri is actually an tuple host:port @@ -205,7 +197,7 @@ int DabOutputTcp::Open(const char* name) uri_ = name; if (success) { - dispatcher_ = new TCPDataDispatcher(); + dispatcher_ = make_shared(); try { dispatcher_->start(port, address); } -- cgit v1.2.3 From 5c12d5b387e07203be79df7e4d526d124a53ad8a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 5 Nov 2016 15:00:09 +0100 Subject: Do not use boost::optional for TcpSocket Older boost versions require T to be copy-constructable in optional. This is not the case for TcpSocket, which is only move-constructable. Fixes compilation on debian jessie --- src/TcpSocket.cpp | 10 ++++++++-- src/TcpSocket.h | 13 ++++++++++--- src/dabOutput/dabOutputTcp.cpp | 6 +++--- 3 files changed, 21 insertions(+), 8 deletions(-) (limited to 'src/dabOutput') diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index 433e5c1..6791286 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -116,6 +116,11 @@ TcpSocket::~TcpSocket() close(); } +bool TcpSocket::isValid() +{ + return m_sock != INVALID_SOCKET; +} + ssize_t TcpSocket::recv(void* data, size_t size) { ssize_t ret = ::recv(m_sock, (char*)data, size, 0); @@ -167,7 +172,7 @@ TcpSocket TcpSocket::accept() } } -boost::optional TcpSocket::accept(int timeout_ms) +TcpSocket TcpSocket::accept(int timeout_ms) { struct pollfd fds[1]; fds[0].fd = m_sock; @@ -184,7 +189,8 @@ boost::optional TcpSocket::accept(int timeout_ms) return accept(); } else { - return boost::none; + TcpSocket invalidsock(0, ""); + return invalidsock; } } diff --git a/src/TcpSocket.h b/src/TcpSocket.h index 8df913f..660515d 100644 --- a/src/TcpSocket.h +++ b/src/TcpSocket.h @@ -46,7 +46,7 @@ #include #include -#include +#include /** * This class represents a TCP socket. @@ -70,7 +70,9 @@ class TcpSocket TcpSocket(const TcpSocket& other) = delete; TcpSocket& operator=(const TcpSocket& other) = delete; - int close(); + bool isValid(void); + + int close(void); /** Send data over the TCP connection. * @param data The buffer that will be sent. @@ -88,7 +90,12 @@ class TcpSocket void listen(void); TcpSocket accept(void); - boost::optional accept(int timeout_ms); + + /* Returns either valid socket if a connection was + * accepted before the timeout expired, or an invalid + * socket otherwise. + */ + 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 94d379e..975fdff 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -133,9 +133,9 @@ class TCPDataDispatcher while (m_running) { // Add a new TCPConnection to the list, constructing it from the client socket - auto optional_sock = m_listener_socket.accept(timeout_ms); - if (optional_sock) { - m_connections.emplace(m_connections.begin(), std::move(*optional_sock)); + auto sock = m_listener_socket.accept(timeout_ms); + if (sock.isValid()) { + m_connections.emplace(m_connections.begin(), move(sock)); } } } -- cgit v1.2.3 From 596f46de6532834f5143405120a3896fca27bb6d Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 7 Nov 2016 21:21:28 +0100 Subject: Add const for the TCPConnection in remove_if lambda --- src/dabOutput/dabOutputTcp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/dabOutput') diff --git a/src/dabOutput/dabOutputTcp.cpp b/src/dabOutput/dabOutputTcp.cpp index 975fdff..02bc7f0 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -121,7 +121,7 @@ class TCPDataDispatcher connection.queue.push(data); } - m_connections.remove_if([](TCPConnection& conn){ return conn.is_overloaded(); }); + m_connections.remove_if([](const TCPConnection& conn){ return conn.is_overloaded(); }); } private: -- cgit v1.2.3 From 33e51f5996c02c6a6aee27b57d91d90e3f1db5a2 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 7 Nov 2016 21:30:52 +0100 Subject: Print dabOutputTCP connection drop --- src/dabOutput/dabOutputTcp.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/dabOutput') diff --git a/src/dabOutput/dabOutputTcp.cpp b/src/dabOutput/dabOutputTcp.cpp index 02bc7f0..2aab48a 100644 --- a/src/dabOutput/dabOutputTcp.cpp +++ b/src/dabOutput/dabOutputTcp.cpp @@ -95,6 +95,10 @@ class TCPConnection m_running = false; } } + + auto addr = m_sock.getRemoteAddress(); + etiLog.level(debug) << "Dropping TCP Connection from " << + addr.getHostAddress() << ":" << addr.getPort(); } }; -- cgit v1.2.3