From 5fa7331cc9f45f7cd02b871f06acbc369335c53e Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 9 Sep 2019 15:11:38 +0200 Subject: [PATCH] Quality: Replace deprecated Boost IO service code https://github.com/boostorg/asio/issues/110 https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/example/cpp03/services/logger_service.hpp --- lib/base/io-engine.cpp | 15 ++++++++------- lib/base/io-engine.hpp | 10 +++++----- lib/base/tcpsocket.hpp | 4 ++-- lib/base/tlsstream.hpp | 10 +++++----- lib/cli/consolecommand.cpp | 2 +- lib/perfdata/elasticsearchwriter.cpp | 4 ++-- lib/perfdata/gelfwriter.cpp | 4 ++-- lib/perfdata/graphitewriter.cpp | 2 +- lib/perfdata/influxdbwriter.cpp | 4 ++-- lib/perfdata/opentsdbwriter.cpp | 4 ++-- lib/remote/apilistener.cpp | 8 ++++---- lib/remote/eventqueue.cpp | 2 +- lib/remote/httpserverconnection.cpp | 6 +++--- lib/remote/httpserverconnection.hpp | 8 ++++---- lib/remote/jsonrpcconnection.cpp | 6 +++--- lib/remote/jsonrpcconnection.hpp | 8 ++++---- lib/remote/pkiutility.cpp | 4 ++-- plugins/check_nscp_api.cpp | 2 +- 18 files changed, 52 insertions(+), 51 deletions(-) diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index e3c36b54b..5dd3ee59c 100644 --- a/lib/base/io-engine.cpp +++ b/lib/base/io-engine.cpp @@ -7,8 +7,9 @@ #include #include #include -#include +#include #include +#include #include #include @@ -78,12 +79,12 @@ IoEngine& IoEngine::Get() return *m_Instance.Get(); } -boost::asio::io_service& IoEngine::GetIoService() +boost::asio::io_context& IoEngine::GetIoContext() { - return m_IoService; + return m_IoContext; } -IoEngine::IoEngine() : m_IoService(), m_KeepAlive(m_IoService), m_Threads(decltype(m_Threads)::size_type(std::thread::hardware_concurrency() * 2u)), m_AlreadyExpiredTimer(m_IoService) +IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(std::thread::hardware_concurrency() * 2u)), m_AlreadyExpiredTimer(m_IoContext) { m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin); m_CpuBoundSemaphore.store(std::thread::hardware_concurrency() * 3u / 2u); @@ -96,7 +97,7 @@ IoEngine::IoEngine() : m_IoService(), m_KeepAlive(m_IoService), m_Threads(declty IoEngine::~IoEngine() { for (auto& thread : m_Threads) { - m_IoService.post([]() { + boost::asio::post(m_IoContext, []() { throw TerminateIoThread(); }); } @@ -110,7 +111,7 @@ void IoEngine::RunEventLoop() { for (;;) { try { - m_IoService.run(); + m_IoContext.run(); break; } catch (const TerminateIoThread&) { @@ -122,7 +123,7 @@ void IoEngine::RunEventLoop() } } -AsioConditionVariable::AsioConditionVariable(boost::asio::io_service& io, bool init) +AsioConditionVariable::AsioConditionVariable(boost::asio::io_context& io, bool init) : m_Timer(io) { m_Timer.expires_at(init ? boost::posix_time::neg_infin : boost::posix_time::pos_infin); diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index e54f414bc..1c6d46045 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include namespace icinga @@ -75,7 +75,7 @@ public: static IoEngine& Get(); - boost::asio::io_service& GetIoService(); + boost::asio::io_context& GetIoContext(); private: IoEngine(); @@ -84,8 +84,8 @@ private: static LazyInit> m_Instance; - boost::asio::io_service m_IoService; - boost::asio::io_service::work m_KeepAlive; + boost::asio::io_context m_IoContext; + boost::asio::executor_work_guard m_KeepAlive; std::vector m_Threads; boost::asio::deadline_timer m_AlreadyExpiredTimer; std::atomic_int_fast32_t m_CpuBoundSemaphore; @@ -103,7 +103,7 @@ class TerminateIoThread : public std::exception class AsioConditionVariable { public: - AsioConditionVariable(boost::asio::io_service& io, bool init = false); + AsioConditionVariable(boost::asio::io_context& io, bool init = false); void Set(); void Clear(); diff --git a/lib/base/tcpsocket.hpp b/lib/base/tcpsocket.hpp index 5c6a9fcb9..e0f502256 100644 --- a/lib/base/tcpsocket.hpp +++ b/lib/base/tcpsocket.hpp @@ -38,7 +38,7 @@ void Connect(Socket& socket, const String& node, const String& service) { using boost::asio::ip::tcp; - tcp::resolver resolver (IoEngine::Get().GetIoService()); + tcp::resolver resolver (IoEngine::Get().GetIoContext()); tcp::resolver::query query (node, service); auto result (resolver.resolve(query)); auto current (result.begin()); @@ -67,7 +67,7 @@ void Connect(Socket& socket, const String& node, const String& service, boost::a { using boost::asio::ip::tcp; - tcp::resolver resolver (IoEngine::Get().GetIoService()); + tcp::resolver resolver (IoEngine::Get().GetIoContext()); tcp::resolver::query query (node, service); auto result (resolver.async_resolve(query, yc)); auto current (result.begin()); diff --git a/lib/base/tlsstream.hpp b/lib/base/tlsstream.hpp index 44fe0b9c5..70a459114 100644 --- a/lib/base/tlsstream.hpp +++ b/lib/base/tlsstream.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -21,7 +21,7 @@ namespace icinga struct UnbufferedAsioTlsStreamParams { - boost::asio::io_service& IoService; + boost::asio::io_context& IoContext; boost::asio::ssl::context& SslContext; const String& Hostname; }; @@ -33,7 +33,7 @@ class UnbufferedAsioTlsStream : public AsioTcpTlsStream public: inline UnbufferedAsioTlsStream(UnbufferedAsioTlsStreamParams& init) - : stream(init.IoService, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname) + : stream(init.IoContext, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname) { } @@ -71,8 +71,8 @@ class AsioTlsStream : public boost::asio::buffered_stream ConsoleCommand::Connect() String host = l_Url->GetHost(); String port = l_Url->GetPort(); - std::shared_ptr stream = std::make_shared(IoEngine::Get().GetIoService(), *sslContext, host); + std::shared_ptr stream = std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, host); try { icinga::Connect(stream->lowest_layer(), host, port); diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index 962148f94..7798ad8dc 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -598,9 +598,9 @@ OptionalTlsStream ElasticsearchWriter::Connect() throw; } - stream.first = std::make_shared(IoEngine::Get().GetIoService(), *sslContext, GetHost()); + stream.first = std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); } else { - stream.second = std::make_shared(IoEngine::Get().GetIoService()); + stream.second = std::make_shared(IoEngine::Get().GetIoContext()); } try { diff --git a/lib/perfdata/gelfwriter.cpp b/lib/perfdata/gelfwriter.cpp index 9846aedc8..c778a3813 100644 --- a/lib/perfdata/gelfwriter.cpp +++ b/lib/perfdata/gelfwriter.cpp @@ -173,9 +173,9 @@ void GelfWriter::ReconnectInternal() throw; } - m_Stream.first = std::make_shared(IoEngine::Get().GetIoService(), *sslContext, GetHost()); + m_Stream.first = std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); } else { - m_Stream.second = std::make_shared(IoEngine::Get().GetIoService()); + m_Stream.second = std::make_shared(IoEngine::Get().GetIoContext()); } try { diff --git a/lib/perfdata/graphitewriter.cpp b/lib/perfdata/graphitewriter.cpp index 9eb2e84a6..642926f66 100644 --- a/lib/perfdata/graphitewriter.cpp +++ b/lib/perfdata/graphitewriter.cpp @@ -187,7 +187,7 @@ void GraphiteWriter::ReconnectInternal() Log(LogNotice, "GraphiteWriter") << "Reconnecting to Graphite on host '" << GetHost() << "' port '" << GetPort() << "'."; - m_Stream = std::make_shared(IoEngine::Get().GetIoService()); + m_Stream = std::make_shared(IoEngine::Get().GetIoContext()); try { icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort()); diff --git a/lib/perfdata/influxdbwriter.cpp b/lib/perfdata/influxdbwriter.cpp index 86d8128de..1e02e5213 100644 --- a/lib/perfdata/influxdbwriter.cpp +++ b/lib/perfdata/influxdbwriter.cpp @@ -187,9 +187,9 @@ OptionalTlsStream InfluxdbWriter::Connect() throw; } - stream.first = std::make_shared(IoEngine::Get().GetIoService(), *sslContext, GetHost()); + stream.first = std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); } else { - stream.second = std::make_shared(IoEngine::Get().GetIoService()); + stream.second = std::make_shared(IoEngine::Get().GetIoContext()); } try { diff --git a/lib/perfdata/opentsdbwriter.cpp b/lib/perfdata/opentsdbwriter.cpp index 0e54b2846..b936b3083 100644 --- a/lib/perfdata/opentsdbwriter.cpp +++ b/lib/perfdata/opentsdbwriter.cpp @@ -121,7 +121,7 @@ void OpenTsdbWriter::ReconnectTimerHandler() * http://opentsdb.net/docs/build/html/user_guide/writing/index.html#telnet */ - m_Stream = std::make_shared(IoEngine::Get().GetIoService()); + m_Stream = std::make_shared(IoEngine::Get().GetIoContext()); try { icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort()); @@ -338,4 +338,4 @@ String OpenTsdbWriter::EscapeMetric(const String& str) boost::replace_all(result, ":", "_"); return result; -} \ No newline at end of file +} diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 5a2ca048c..5d82b6dc8 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -363,7 +363,7 @@ bool ApiListener::AddListener(const String& node, const String& service) return false; } - auto& io (IoEngine::Get().GetIoService()); + auto& io (IoEngine::Get().GetIoContext()); auto acceptor (std::make_shared(io)); try { @@ -427,7 +427,7 @@ void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const std { namespace asio = boost::asio; - auto& io (IoEngine::Get().GetIoService()); + auto& io (IoEngine::Get().GetIoContext()); for (;;) { try { @@ -460,7 +460,7 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint) return; } - auto& io (IoEngine::Get().GetIoService()); + auto& io (IoEngine::Get().GetIoContext()); asio::spawn(io, [this, endpoint, &io, sslContext](asio::yield_context yc) { String host = endpoint->GetHost(); @@ -664,7 +664,7 @@ void ApiListener::NewClientHandlerInternal(boost::asio::yield_context yc, const endpoint->AddClient(aclient); - asio::spawn(IoEngine::Get().GetIoService(), [this, aclient, endpoint, needSync](asio::yield_context yc) { + asio::spawn(IoEngine::Get().GetIoContext(), [this, aclient, endpoint, needSync](asio::yield_context yc) { CpuBoundWork syncClient (yc); SyncClient(aclient, endpoint, needSync); diff --git a/lib/remote/eventqueue.cpp b/lib/remote/eventqueue.cpp index 2a7869d45..5b6219c10 100644 --- a/lib/remote/eventqueue.cpp +++ b/lib/remote/eventqueue.cpp @@ -131,7 +131,7 @@ std::map EventsInbox::m_Filters ({{"", EventsInbox: EventsRouter EventsRouter::m_Instance; EventsInbox::EventsInbox(String filter, const String& filterSource) - : m_Timer(IoEngine::Get().GetIoService()) + : m_Timer(IoEngine::Get().GetIoContext()) { std::unique_lock lock (m_FiltersMutex); m_Filter = m_Filters.find(filter); diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index 524916b4e..556f60857 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include @@ -35,11 +35,11 @@ using namespace icinga; auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion()); HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream) - : HttpServerConnection(identity, authenticated, stream, IoEngine::Get().GetIoService()) + : HttpServerConnection(identity, authenticated, stream, IoEngine::Get().GetIoContext()) { } -HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, boost::asio::io_service& io) +HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, boost::asio::io_context& io) : m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(io), m_ShuttingDown(false), m_HasStartedStreaming(false), m_CheckLivenessTimer(io) { diff --git a/lib/remote/httpserverconnection.hpp b/lib/remote/httpserverconnection.hpp index b697d5381..43851e76e 100644 --- a/lib/remote/httpserverconnection.hpp +++ b/lib/remote/httpserverconnection.hpp @@ -8,8 +8,8 @@ #include "base/tlsstream.hpp" #include #include -#include -#include +#include +#include #include namespace icinga @@ -38,12 +38,12 @@ private: std::shared_ptr m_Stream; double m_Seen; String m_PeerAddress; - boost::asio::io_service::strand m_IoStrand; + boost::asio::io_context::strand m_IoStrand; bool m_ShuttingDown; bool m_HasStartedStreaming; boost::asio::deadline_timer m_CheckLivenessTimer; - HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, boost::asio::io_service& io); + HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, boost::asio::io_context& io); void ProcessMessages(boost::asio::yield_context yc); void CheckLiveness(boost::asio::yield_context yc); diff --git a/lib/remote/jsonrpcconnection.cpp b/lib/remote/jsonrpcconnection.cpp index 5a26812a2..eb9a946a4 100644 --- a/lib/remote/jsonrpcconnection.cpp +++ b/lib/remote/jsonrpcconnection.cpp @@ -16,7 +16,7 @@ #include "base/tlsstream.hpp" #include #include -#include +#include #include #include #include @@ -31,12 +31,12 @@ static RingBuffer l_TaskStats (15 * 60); JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, ConnectionRole role) - : JsonRpcConnection(identity, authenticated, stream, role, IoEngine::Get().GetIoService()) + : JsonRpcConnection(identity, authenticated, stream, role, IoEngine::Get().GetIoContext()) { } JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated, - const std::shared_ptr& stream, ConnectionRole role, boost::asio::io_service& io) + const std::shared_ptr& stream, ConnectionRole role, boost::asio::io_context& io) : m_Identity(identity), m_Authenticated(authenticated), m_Stream(stream), m_Role(role), m_Timestamp(Utility::GetTime()), m_Seen(Utility::GetTime()), m_NextHeartbeat(0), m_IoStrand(io), m_OutgoingMessagesQueued(io), m_WriterDone(io), m_ShuttingDown(false), diff --git a/lib/remote/jsonrpcconnection.hpp b/lib/remote/jsonrpcconnection.hpp index 2b1835308..0fbf6c605 100644 --- a/lib/remote/jsonrpcconnection.hpp +++ b/lib/remote/jsonrpcconnection.hpp @@ -11,8 +11,8 @@ #include "base/workqueue.hpp" #include #include -#include -#include +#include +#include #include namespace icinga @@ -73,14 +73,14 @@ private: double m_Timestamp; double m_Seen; double m_NextHeartbeat; - boost::asio::io_service::strand m_IoStrand; + boost::asio::io_context::strand m_IoStrand; std::vector m_OutgoingMessagesQueue; AsioConditionVariable m_OutgoingMessagesQueued; AsioConditionVariable m_WriterDone; bool m_ShuttingDown; boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer; - JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, ConnectionRole role, boost::asio::io_service& io); + JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr& stream, ConnectionRole role, boost::asio::io_context& io); void HandleIncomingMessages(boost::asio::yield_context yc); void WriteOutgoingMessages(boost::asio::yield_context yc); diff --git a/lib/remote/pkiutility.cpp b/lib/remote/pkiutility.cpp index 3fddd1a67..a95e3554c 100644 --- a/lib/remote/pkiutility.cpp +++ b/lib/remote/pkiutility.cpp @@ -93,7 +93,7 @@ std::shared_ptr PkiUtility::FetchCert(const String& host, const String& po return std::shared_ptr(); } - auto stream (std::make_shared(IoEngine::Get().GetIoService(), *sslContext, host)); + auto stream (std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, host)); try { Connect(stream->lowest_layer(), host, port); @@ -161,7 +161,7 @@ int PkiUtility::RequestCertificate(const String& host, const String& port, const return 1; } - auto stream (std::make_shared(IoEngine::Get().GetIoService(), *sslContext, host)); + auto stream (std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, host)); try { Connect(stream->lowest_layer(), host, port); diff --git a/plugins/check_nscp_api.cpp b/plugins/check_nscp_api.cpp index a9009c47e..1339ccbac 100644 --- a/plugins/check_nscp_api.cpp +++ b/plugins/check_nscp_api.cpp @@ -186,7 +186,7 @@ static std::shared_ptr Connect(const String& host, const String& throw; } - std::shared_ptr stream = std::make_shared(IoEngine::Get().GetIoService(), *sslContext, host); + std::shared_ptr stream = std::make_shared(IoEngine::Get().GetIoContext(), *sslContext, host); try { icinga::Connect(stream->lowest_layer(), host, port);