From 0f84ce0470014c05d1b5fcc5ca6b958c31a02ef4 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 13 Feb 2020 18:18:58 +0100 Subject: [PATCH] Consider a JsonRpcConnection being seen on a single byte of TLS payload, not only a whole message --- lib/base/tlsstream.hpp | 40 ++++++++++++++++++++++++++++++-- lib/remote/jsonrpcconnection.cpp | 2 ++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/base/tlsstream.hpp b/lib/base/tlsstream.hpp index 70a459114..05b3550d5 100644 --- a/lib/base/tlsstream.hpp +++ b/lib/base/tlsstream.hpp @@ -8,17 +8,53 @@ #include "base/stream.hpp" #include "base/tlsutility.hpp" #include "base/fifo.hpp" +#include "base/utility.hpp" +#include #include #include #include #include #include +#include #include #include namespace icinga { +template +class SeenStream : public ARS +{ +public: + template + SeenStream(Args&&... args) : ARS(std::forward(args)...) + { + m_Seen.store(nullptr); + } + + template + auto async_read_some(Args&&... args) -> decltype(((ARS*)nullptr)->async_read_some(std::forward(args)...)) + { + { + auto seen (m_Seen.load()); + + if (seen) { + *seen = Utility::GetTime(); + } + } + + return ((ARS*)this)->async_read_some(std::forward(args)...); + } + + inline void SetSeen(double* seen) + { + m_Seen.store(seen); + } + +private: + std::atomic m_Seen; +}; + struct UnbufferedAsioTlsStreamParams { boost::asio::io_context& IoContext; @@ -26,14 +62,14 @@ struct UnbufferedAsioTlsStreamParams const String& Hostname; }; -typedef boost::asio::ssl::stream AsioTcpTlsStream; +typedef SeenStream> AsioTcpTlsStream; class UnbufferedAsioTlsStream : public AsioTcpTlsStream { public: inline UnbufferedAsioTlsStream(UnbufferedAsioTlsStreamParams& init) - : stream(init.IoContext, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname) + : AsioTcpTlsStream(init.IoContext, init.SslContext), m_VerifyOK(true), m_Hostname(init.Hostname) { } diff --git a/lib/remote/jsonrpcconnection.cpp b/lib/remote/jsonrpcconnection.cpp index b6d1d41e6..5ee2b645b 100644 --- a/lib/remote/jsonrpcconnection.cpp +++ b/lib/remote/jsonrpcconnection.cpp @@ -60,6 +60,8 @@ void JsonRpcConnection::Start() void JsonRpcConnection::HandleIncomingMessages(boost::asio::yield_context yc) { + m_Stream->next_layer().SetSeen(&m_Seen); + for (;;) { String message;