From cfb7c7cd760b0427af1663d5ea9efc154e79748f Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sat, 5 Nov 2022 15:19:04 -0400 Subject: [PATCH] Fixed CharonClient::Connect issues. Implemented 1 min timeout for async connect calls --- .../Specter/Physics/nscldaq/CharonClient.cpp | 31 +++++++++++++++++-- .../Specter/Physics/nscldaq/CharonClient.h | 2 ++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Specter/src/Specter/Physics/nscldaq/CharonClient.cpp b/Specter/src/Specter/Physics/nscldaq/CharonClient.cpp index b75efd6..1291780 100644 --- a/Specter/src/Specter/Physics/nscldaq/CharonClient.cpp +++ b/Specter/src/Specter/Physics/nscldaq/CharonClient.cpp @@ -3,7 +3,7 @@ namespace Specter { CharonClient::CharonClient(const std::string& hostname, const std::string& port) : - m_socket(m_context) + m_socket(m_context), m_deadline(m_context) { Connect(hostname, port); } @@ -29,18 +29,27 @@ namespace Specter { { asio::ip::tcp::resolver resolver(m_context); auto end_points = resolver.resolve(hostname, port); + + m_deadline.expires_after(std::chrono::seconds(60)); asio::async_connect(m_socket, end_points, - [this](std::error_code ec, asio::ip::tcp::endpoint endpoint) + [this, hostname, port](std::error_code ec, asio::ip::tcp::endpoint endpoint) { if (!ec) { + SPEC_INFO("Connected CharonClient to {0}:{1}", endpoint.address(), endpoint.port()); + //Turn off our deadline timer + m_deadline.cancel(); ReadHeader(); } + else + { + SPEC_WARN("Unable to connect to CharonClient {0}:{1}" , hostname, port); + } } ); + m_deadline.async_wait(std::bind(&CharonClient::HandleTimeout, this, std::placeholders::_1)); m_ioThread = std::thread([this]() { m_context.run(); }); - SPEC_WARN("Connected CharonClient to {0}:{1}", hostname, port); } catch (asio::system_error& e) { @@ -93,4 +102,20 @@ namespace Specter { } ); } + + void CharonClient::HandleTimeout(const asio::error_code& ec) + { + //If we stop the timer, don't do anything + if(ec == asio::error::operation_aborted) + { + return; + } + //Check to make sure that deadline wasn't asychronously moved + if(m_deadline.expiry() <= asio::steady_timer::clock_type::now()) + { + SPEC_WARN("CharonClient timedout at Connect!"); + m_socket.close(); + m_deadline.expires_at(asio::steady_timer::time_point::max()); + } + } } \ No newline at end of file diff --git a/Specter/src/Specter/Physics/nscldaq/CharonClient.h b/Specter/src/Specter/Physics/nscldaq/CharonClient.h index cc1b3ca..99e9d6b 100644 --- a/Specter/src/Specter/Physics/nscldaq/CharonClient.h +++ b/Specter/src/Specter/Physics/nscldaq/CharonClient.h @@ -29,9 +29,11 @@ namespace Specter { private: void ReadHeader(); void ReadBody(); + void HandleTimeout(const asio::error_code& ec); asio::io_context m_context; asio::ip::tcp::socket m_socket; + asio::steady_timer m_deadline; std::thread m_ioThread; StygianMessage m_tempMessage;