Merge pull request #7360 from Icinga/feature/std-shared_ptr

Replace some classic shared pointers with intrusive ones
This commit is contained in:
Michael Friedrich 2019-10-21 17:02:28 +02:00 committed by GitHub
commit fbb402b944
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 186 additions and 79 deletions

View File

@ -60,6 +60,7 @@ set(base_SOURCES
scriptglobal.cpp scriptglobal.hpp scriptglobal.cpp scriptglobal.hpp
scriptutils.cpp scriptutils.hpp scriptutils.cpp scriptutils.hpp
serializer.cpp serializer.hpp serializer.cpp serializer.hpp
shared.hpp
singleton.hpp singleton.hpp
socket.cpp socket.hpp socket.cpp socket.hpp
stacktrace.cpp stacktrace.hpp stacktrace.cpp stacktrace.hpp

View File

@ -126,7 +126,7 @@ size_t NetString::WriteStringToStream(const Stream::Ptr& stream, const String& s
* @exception invalid_argument The input stream is invalid. * @exception invalid_argument The input stream is invalid.
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c * @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
*/ */
String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream, String NetString::ReadStringFromStream(const Shared<AsioTlsStream>::Ptr& stream,
ssize_t maxMessageLength) ssize_t maxMessageLength)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
@ -205,7 +205,7 @@ String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& str
* @exception invalid_argument The input stream is invalid. * @exception invalid_argument The input stream is invalid.
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c * @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
*/ */
String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream, String NetString::ReadStringFromStream(const Shared<AsioTlsStream>::Ptr& stream,
boost::asio::yield_context yc, ssize_t maxMessageLength) boost::asio::yield_context yc, ssize_t maxMessageLength)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
@ -284,7 +284,7 @@ String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& str
* *
* @return The amount of bytes written. * @return The amount of bytes written.
*/ */
size_t NetString::WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& str) size_t NetString::WriteStringToStream(const Shared<AsioTlsStream>::Ptr& stream, const String& str)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
@ -307,7 +307,7 @@ size_t NetString::WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stre
* *
* @return The amount of bytes written. * @return The amount of bytes written.
*/ */
size_t NetString::WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& str, boost::asio::yield_context yc) size_t NetString::WriteStringToStream(const Shared<AsioTlsStream>::Ptr& stream, const String& str, boost::asio::yield_context yc)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;

View File

@ -26,12 +26,12 @@ class NetString
public: public:
static StreamReadStatus ReadStringFromStream(const Stream::Ptr& stream, String *message, StreamReadContext& context, static StreamReadStatus ReadStringFromStream(const Stream::Ptr& stream, String *message, StreamReadContext& context,
bool may_wait = false, ssize_t maxMessageLength = -1); bool may_wait = false, ssize_t maxMessageLength = -1);
static String ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream, ssize_t maxMessageLength = -1); static String ReadStringFromStream(const Shared<AsioTlsStream>::Ptr& stream, ssize_t maxMessageLength = -1);
static String ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream, static String ReadStringFromStream(const Shared<AsioTlsStream>::Ptr& stream,
boost::asio::yield_context yc, ssize_t maxMessageLength = -1); boost::asio::yield_context yc, ssize_t maxMessageLength = -1);
static size_t WriteStringToStream(const Stream::Ptr& stream, const String& message); static size_t WriteStringToStream(const Stream::Ptr& stream, const String& message);
static size_t WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& message); static size_t WriteStringToStream(const Shared<AsioTlsStream>::Ptr& stream, const String& message);
static size_t WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& message, boost::asio::yield_context yc); static size_t WriteStringToStream(const Shared<AsioTlsStream>::Ptr& stream, const String& message, boost::asio::yield_context yc);
static void WriteStringToStream(std::ostream& stream, const String& message); static void WriteStringToStream(std::ostream& stream, const String& message);
private: private:

101
lib/base/shared.hpp Normal file
View File

@ -0,0 +1,101 @@
/* Icinga 2 | (c) 2019 Icinga GmbH | GPLv2+ */
#ifndef SHARED_H
#define SHARED_H
#include "base/atomic.hpp"
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <cstdint>
#include <utility>
namespace icinga
{
template<class T>
class Shared;
template<class T>
inline void intrusive_ptr_add_ref(Shared<T> *object)
{
object->m_References.fetch_add(1);
}
template<class T>
inline void intrusive_ptr_release(Shared<T> *object)
{
if (object->m_References.fetch_sub(1) == 1u) {
delete object;
}
}
/**
* Seemless wrapper for any class to create shared pointers of.
* Saves a memory allocation compared to std::shared_ptr.
*
* @ingroup base
*/
template<class T>
class Shared : public T
{
friend void intrusive_ptr_add_ref<>(Shared<T> *object);
friend void intrusive_ptr_release<>(Shared<T> *object);
public:
typedef boost::intrusive_ptr<Shared> Ptr;
/**
* Like std::make_shared, but for this class.
*
* @param args Constructor arguments
*
* @return Ptr
*/
template<class... Args>
static inline
Ptr Make(Args&&... args)
{
return new Shared(std::forward<Args>(args)...);
}
inline Shared(const Shared& origin) : Shared((const T&)origin)
{
}
inline Shared(Shared&& origin) : Shared((T&&)origin)
{
}
template<class... Args>
inline Shared(Args&&... args) : T(std::forward<Args>(args)...), m_References(0)
{
}
inline Shared& operator=(const Shared& rhs)
{
return operator=((const T&)rhs);
}
inline Shared& operator=(Shared&& rhs)
{
return operator=((T&&)rhs);
}
inline Shared& operator=(const T& rhs)
{
T::operator=(rhs);
return *this;
}
inline Shared& operator=(T&& rhs)
{
T::operator=(std::move(rhs));
return *this;
}
private:
Atomic<uint_fast64_t> m_References;
};
}
#endif /* SHARED_H */

View File

@ -4,6 +4,7 @@
#define TLSSTREAM_H #define TLSSTREAM_H
#include "base/i2-base.hpp" #include "base/i2-base.hpp"
#include "base/shared.hpp"
#include "base/socket.hpp" #include "base/socket.hpp"
#include "base/stream.hpp" #include "base/stream.hpp"
#include "base/tlsutility.hpp" #include "base/tlsutility.hpp"
@ -85,7 +86,7 @@ private:
}; };
typedef boost::asio::buffered_stream<boost::asio::ip::tcp::socket> AsioTcpStream; typedef boost::asio::buffered_stream<boost::asio::ip::tcp::socket> AsioTcpStream;
typedef std::pair<std::shared_ptr<AsioTlsStream>, std::shared_ptr<AsioTcpStream>> OptionalTlsStream; typedef std::pair<Shared<AsioTlsStream>::Ptr, Shared<AsioTcpStream>::Ptr> OptionalTlsStream;
} }

View File

@ -58,7 +58,7 @@ void InitializeOpenSSL()
l_SSLInitialized = true; l_SSLInitialized = true;
} }
static void SetupSslContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& pubkey, const String& privkey, const String& cakey) static void SetupSslContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& pubkey, const String& privkey, const String& cakey)
{ {
char errbuf[256]; char errbuf[256];
@ -156,13 +156,13 @@ static void SetupSslContext(const std::shared_ptr<boost::asio::ssl::context>& co
* @param cakey CA certificate chain file. * @param cakey CA certificate chain file.
* @returns An SSL context. * @returns An SSL context.
*/ */
std::shared_ptr<boost::asio::ssl::context> MakeAsioSslContext(const String& pubkey, const String& privkey, const String& cakey) Shared<boost::asio::ssl::context>::Ptr MakeAsioSslContext(const String& pubkey, const String& privkey, const String& cakey)
{ {
namespace ssl = boost::asio::ssl; namespace ssl = boost::asio::ssl;
InitializeOpenSSL(); InitializeOpenSSL();
auto context (std::make_shared<ssl::context>(ssl::context::tlsv12)); auto context (Shared<ssl::context>::Make(ssl::context::tlsv12));
SetupSslContext(context, pubkey, privkey, cakey); SetupSslContext(context, pubkey, privkey, cakey);
@ -174,7 +174,7 @@ std::shared_ptr<boost::asio::ssl::context> MakeAsioSslContext(const String& pubk
* @param context The ssl context. * @param context The ssl context.
* @param cipherList The ciper list. * @param cipherList The ciper list.
**/ **/
void SetCipherListToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& cipherList) void SetCipherListToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& cipherList)
{ {
char errbuf[256]; char errbuf[256];
@ -215,7 +215,7 @@ void SetCipherListToSSLContext(const std::shared_ptr<boost::asio::ssl::context>&
* @param context The ssl context. * @param context The ssl context.
* @param tlsProtocolmin The minimum TLS protocol version. * @param tlsProtocolmin The minimum TLS protocol version.
*/ */
void SetTlsProtocolminToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& tlsProtocolmin) void SetTlsProtocolminToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& tlsProtocolmin)
{ {
// tlsProtocolmin has no effect since we enforce TLS 1.2 since 2.11. // tlsProtocolmin has no effect since we enforce TLS 1.2 since 2.11.
/* /*
@ -235,7 +235,7 @@ void SetTlsProtocolminToSSLContext(const std::shared_ptr<boost::asio::ssl::conte
* @param context The SSL context. * @param context The SSL context.
* @param crlPath The path to the CRL file. * @param crlPath The path to the CRL file.
*/ */
void AddCRLToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& crlPath) void AddCRLToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& crlPath)
{ {
char errbuf[256]; char errbuf[256];
X509_STORE *x509_store = SSL_CTX_get_cert_store(context->native_handle()); X509_STORE *x509_store = SSL_CTX_get_cert_store(context->native_handle());

View File

@ -5,6 +5,7 @@
#include "base/i2-base.hpp" #include "base/i2-base.hpp"
#include "base/object.hpp" #include "base/object.hpp"
#include "base/shared.hpp"
#include "base/string.hpp" #include "base/string.hpp"
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/bio.h> #include <openssl/bio.h>
@ -22,10 +23,10 @@ namespace icinga
void InitializeOpenSSL(); void InitializeOpenSSL();
std::shared_ptr<boost::asio::ssl::context> MakeAsioSslContext(const String& pubkey = String(), const String& privkey = String(), const String& cakey = String()); Shared<boost::asio::ssl::context>::Ptr MakeAsioSslContext(const String& pubkey = String(), const String& privkey = String(), const String& cakey = String());
void AddCRLToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& crlPath); void AddCRLToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& crlPath);
void SetCipherListToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& cipherList); void SetCipherListToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& cipherList);
void SetTlsProtocolminToSSLContext(const std::shared_ptr<boost::asio::ssl::context>& context, const String& tlsProtocolmin); void SetTlsProtocolminToSSLContext(const Shared<boost::asio::ssl::context>::Ptr& context, const String& tlsProtocolmin);
String GetCertificateCN(const std::shared_ptr<X509>& certificate); String GetCertificateCN(const std::shared_ptr<X509>& certificate);
std::shared_ptr<X509> GetX509Certificate(const String& pemfile); std::shared_ptr<X509> GetX509Certificate(const String& pemfile);

View File

@ -42,7 +42,7 @@ namespace po = boost::program_options;
static ScriptFrame *l_ScriptFrame; static ScriptFrame *l_ScriptFrame;
static Url::Ptr l_Url; static Url::Ptr l_Url;
static std::shared_ptr<AsioTlsStream> l_TlsStream; static Shared<AsioTlsStream>::Ptr l_TlsStream;
static String l_Session; static String l_Session;
REGISTER_CLICOMMAND("console", ConsoleCommand); REGISTER_CLICOMMAND("console", ConsoleCommand);
@ -522,9 +522,9 @@ incomplete:
* *
* @returns AsioTlsStream pointer for future HTTP connections. * @returns AsioTlsStream pointer for future HTTP connections.
*/ */
std::shared_ptr<AsioTlsStream> ConsoleCommand::Connect() Shared<AsioTlsStream>::Ptr ConsoleCommand::Connect()
{ {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters
@ -537,7 +537,7 @@ std::shared_ptr<AsioTlsStream> ConsoleCommand::Connect()
String host = l_Url->GetHost(); String host = l_Url->GetHost();
String port = l_Url->GetPort(); String port = l_Url->GetPort();
std::shared_ptr<AsioTlsStream> stream = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, host); Shared<AsioTlsStream>::Ptr stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host);
try { try {
icinga::Connect(stream->lowest_layer(), host, port); icinga::Connect(stream->lowest_layer(), host, port);

View File

@ -40,7 +40,7 @@ private:
mutable boost::mutex m_Mutex; mutable boost::mutex m_Mutex;
mutable boost::condition_variable m_CV; mutable boost::condition_variable m_CV;
static std::shared_ptr<AsioTlsStream> Connect(); static Shared<AsioTlsStream>::Ptr Connect();
static Value ExecuteScript(const String& session, const String& command, bool sandboxed); static Value ExecuteScript(const String& session, const String& command, bool sandboxed);
static Array::Ptr AutoCompleteScript(const String& session, const String& command, bool sandboxed); static Array::Ptr AutoCompleteScript(const String& session, const String& command, bool sandboxed);

View File

@ -588,7 +588,7 @@ OptionalTlsStream ElasticsearchWriter::Connect()
bool tls = GetEnableTls(); bool tls = GetEnableTls();
if (tls) { if (tls) {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(GetCertPath(), GetKeyPath(), GetCaPath()); sslContext = MakeAsioSslContext(GetCertPath(), GetKeyPath(), GetCaPath());
@ -598,9 +598,10 @@ OptionalTlsStream ElasticsearchWriter::Connect()
throw; throw;
} }
stream.first = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); stream.first = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, GetHost());
} else { } else {
stream.second = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext()); stream.second = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
} }
try { try {

View File

@ -163,7 +163,7 @@ void GelfWriter::ReconnectInternal()
bool ssl = GetEnableTls(); bool ssl = GetEnableTls();
if (ssl) { if (ssl) {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(GetCertPath(), GetKeyPath(), GetCaPath()); sslContext = MakeAsioSslContext(GetCertPath(), GetKeyPath(), GetCaPath());
@ -173,9 +173,10 @@ void GelfWriter::ReconnectInternal()
throw; throw;
} }
m_Stream.first = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); m_Stream.first = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, GetHost());
} else { } else {
m_Stream.second = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext()); m_Stream.second = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
} }
try { try {

View File

@ -187,7 +187,7 @@ void GraphiteWriter::ReconnectInternal()
Log(LogNotice, "GraphiteWriter") Log(LogNotice, "GraphiteWriter")
<< "Reconnecting to Graphite on host '" << GetHost() << "' port '" << GetPort() << "'."; << "Reconnecting to Graphite on host '" << GetHost() << "' port '" << GetPort() << "'.";
m_Stream = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext()); m_Stream = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
try { try {
icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort()); icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort());

View File

@ -37,7 +37,7 @@ protected:
void Pause() override; void Pause() override;
private: private:
std::shared_ptr<AsioTcpStream> m_Stream; Shared<AsioTcpStream>::Ptr m_Stream;
boost::mutex m_StreamMutex; boost::mutex m_StreamMutex;
WorkQueue m_WorkQueue{10000000, 1}; WorkQueue m_WorkQueue{10000000, 1};

View File

@ -177,7 +177,7 @@ OptionalTlsStream InfluxdbWriter::Connect()
bool ssl = GetSslEnable(); bool ssl = GetSslEnable();
if (ssl) { if (ssl) {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(GetSslCert(), GetSslKey(), GetSslCaCert()); sslContext = MakeAsioSslContext(GetSslCert(), GetSslKey(), GetSslCaCert());
@ -187,9 +187,10 @@ OptionalTlsStream InfluxdbWriter::Connect()
throw; throw;
} }
stream.first = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, GetHost()); stream.first = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, GetHost());
} else { } else {
stream.second = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext()); stream.second = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
} }
try { try {

View File

@ -120,8 +120,7 @@ void OpenTsdbWriter::ReconnectTimerHandler()
* We're using telnet as input method. Future PRs may change this into using the HTTP API. * We're using telnet as input method. Future PRs may change this into using the HTTP API.
* http://opentsdb.net/docs/build/html/user_guide/writing/index.html#telnet * http://opentsdb.net/docs/build/html/user_guide/writing/index.html#telnet
*/ */
m_Stream = Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext());
m_Stream = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext());
try { try {
icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort()); icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort());

View File

@ -32,7 +32,7 @@ protected:
void Pause() override; void Pause() override;
private: private:
std::shared_ptr<AsioTcpStream> m_Stream; Shared<AsioTcpStream>::Ptr m_Stream;
Timer::Ptr m_ReconnectTimer; Timer::Ptr m_ReconnectTimer;

View File

@ -178,7 +178,7 @@ void ApiListener::UpdateSSLContext()
{ {
namespace ssl = boost::asio::ssl; namespace ssl = boost::asio::ssl;
std::shared_ptr<ssl::context> context; Shared<ssl::context>::Ptr context;
try { try {
context = MakeAsioSslContext(GetDefaultCertPath(), GetDefaultKeyPath(), GetDefaultCaPath()); context = MakeAsioSslContext(GetDefaultCertPath(), GetDefaultKeyPath(), GetDefaultCaPath());
@ -364,7 +364,7 @@ bool ApiListener::AddListener(const String& node, const String& service)
} }
auto& io (IoEngine::Get().GetIoContext()); auto& io (IoEngine::Get().GetIoContext());
auto acceptor (std::make_shared<tcp::acceptor>(io)); auto acceptor (Shared<tcp::acceptor>::Make(io));
try { try {
tcp::resolver resolver (io); tcp::resolver resolver (io);
@ -423,7 +423,7 @@ bool ApiListener::AddListener(const String& node, const String& service)
return true; return true;
} }
void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const std::shared_ptr<boost::asio::ip::tcp::acceptor>& server, const std::shared_ptr<boost::asio::ssl::context>& sslContext) void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const Shared<boost::asio::ip::tcp::acceptor>::Ptr& server, const Shared<boost::asio::ssl::context>::Ptr& sslContext)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
@ -431,7 +431,7 @@ void ApiListener::ListenerCoroutineProc(boost::asio::yield_context yc, const std
for (;;) { for (;;) {
try { try {
auto sslConn (std::make_shared<AsioTlsStream>(io, *sslContext)); auto sslConn (Shared<AsioTlsStream>::Make(io, *sslContext));
server->async_accept(sslConn->lowest_layer(), yc); server->async_accept(sslConn->lowest_layer(), yc);
@ -470,7 +470,7 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint)
<< "Reconnecting to endpoint '" << endpoint->GetName() << "' via host '" << host << "' and port '" << port << "'"; << "Reconnecting to endpoint '" << endpoint->GetName() << "' via host '" << host << "' and port '" << port << "'";
try { try {
auto sslConn (std::make_shared<AsioTlsStream>(io, *sslContext, endpoint->GetName())); auto sslConn (Shared<AsioTlsStream>::Make(io, *sslContext, endpoint->GetName()));
Connect(sslConn->lowest_layer(), host, port, yc); Connect(sslConn->lowest_layer(), host, port, yc);
@ -488,7 +488,7 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint)
}); });
} }
void ApiListener::NewClientHandler(boost::asio::yield_context yc, const std::shared_ptr<AsioTlsStream>& client, const String& hostname, ConnectionRole role) void ApiListener::NewClientHandler(boost::asio::yield_context yc, const Shared<AsioTlsStream>::Ptr& client, const String& hostname, ConnectionRole role)
{ {
try { try {
NewClientHandlerInternal(yc, client, hostname, role); NewClientHandlerInternal(yc, client, hostname, role);
@ -506,7 +506,7 @@ void ApiListener::NewClientHandler(boost::asio::yield_context yc, const std::sha
* *
* @param client The new client. * @param client The new client.
*/ */
void ApiListener::NewClientHandlerInternal(boost::asio::yield_context yc, const std::shared_ptr<AsioTlsStream>& client, const String& hostname, ConnectionRole role) void ApiListener::NewClientHandlerInternal(boost::asio::yield_context yc, const Shared<AsioTlsStream>::Ptr& client, const String& hostname, ConnectionRole role)
{ {
namespace asio = boost::asio; namespace asio = boost::asio;
namespace ssl = asio::ssl; namespace ssl = asio::ssl;

View File

@ -10,6 +10,7 @@
#include "remote/messageorigin.hpp" #include "remote/messageorigin.hpp"
#include "base/configobject.hpp" #include "base/configobject.hpp"
#include "base/process.hpp" #include "base/process.hpp"
#include "base/shared.hpp"
#include "base/timer.hpp" #include "base/timer.hpp"
#include "base/workqueue.hpp" #include "base/workqueue.hpp"
#include "base/tcpsocket.hpp" #include "base/tcpsocket.hpp"
@ -124,7 +125,7 @@ protected:
void ValidateTlsHandshakeTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) override; void ValidateTlsHandshakeTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) override;
private: private:
std::shared_ptr<boost::asio::ssl::context> m_SSLContext; Shared<boost::asio::ssl::context>::Ptr m_SSLContext;
mutable boost::mutex m_AnonymousClientsLock; mutable boost::mutex m_AnonymousClientsLock;
mutable boost::mutex m_HttpClientsLock; mutable boost::mutex m_HttpClientsLock;
@ -150,9 +151,9 @@ private:
bool AddListener(const String& node, const String& service); bool AddListener(const String& node, const String& service);
void AddConnection(const Endpoint::Ptr& endpoint); void AddConnection(const Endpoint::Ptr& endpoint);
void NewClientHandler(boost::asio::yield_context yc, const std::shared_ptr<AsioTlsStream>& client, const String& hostname, ConnectionRole role); void NewClientHandler(boost::asio::yield_context yc, const Shared<AsioTlsStream>::Ptr& client, const String& hostname, ConnectionRole role);
void NewClientHandlerInternal(boost::asio::yield_context yc, const std::shared_ptr<AsioTlsStream>& client, const String& hostname, ConnectionRole role); void NewClientHandlerInternal(boost::asio::yield_context yc, const Shared<AsioTlsStream>::Ptr& client, const String& hostname, ConnectionRole role);
void ListenerCoroutineProc(boost::asio::yield_context yc, const std::shared_ptr<boost::asio::ip::tcp::acceptor>& server, const std::shared_ptr<boost::asio::ssl::context>& sslContext); void ListenerCoroutineProc(boost::asio::yield_context yc, const Shared<boost::asio::ip::tcp::acceptor>::Ptr& server, const Shared<boost::asio::ssl::context>::Ptr& sslContext);
WorkQueue m_RelayQueue; WorkQueue m_RelayQueue;
WorkQueue m_SyncQueue{0, 4}; WorkQueue m_SyncQueue{0, 4};

View File

@ -34,12 +34,12 @@ using namespace icinga;
auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion()); auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion());
HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream) HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream)
: HttpServerConnection(identity, authenticated, stream, IoEngine::Get().GetIoContext()) : HttpServerConnection(identity, authenticated, stream, IoEngine::Get().GetIoContext())
{ {
} }
HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, boost::asio::io_context& io) HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io)
: m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(io), m_ShuttingDown(false), m_HasStartedStreaming(false), : m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(io), m_ShuttingDown(false), m_HasStartedStreaming(false),
m_CheckLivenessTimer(io) m_CheckLivenessTimer(io)
{ {

View File

@ -25,7 +25,7 @@ class HttpServerConnection final : public Object
public: public:
DECLARE_PTR_TYPEDEFS(HttpServerConnection); DECLARE_PTR_TYPEDEFS(HttpServerConnection);
HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream); HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream);
void Start(); void Start();
void Disconnect(); void Disconnect();
@ -35,7 +35,7 @@ public:
private: private:
ApiUser::Ptr m_ApiUser; ApiUser::Ptr m_ApiUser;
std::shared_ptr<AsioTlsStream> m_Stream; Shared<AsioTlsStream>::Ptr m_Stream;
double m_Seen; double m_Seen;
String m_PeerAddress; String m_PeerAddress;
boost::asio::io_context::strand m_IoStrand; boost::asio::io_context::strand m_IoStrand;
@ -43,7 +43,7 @@ private:
bool m_HasStartedStreaming; bool m_HasStartedStreaming;
boost::asio::deadline_timer m_CheckLivenessTimer; boost::asio::deadline_timer m_CheckLivenessTimer;
HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, boost::asio::io_context& io); HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io);
void ProcessMessages(boost::asio::yield_context yc); void ProcessMessages(boost::asio::yield_context yc);
void CheckLiveness(boost::asio::yield_context yc); void CheckLiveness(boost::asio::yield_context yc);

View File

@ -52,7 +52,7 @@ static bool GetDebugJsonRpcCached()
* *
* @return The amount of bytes sent. * @return The amount of bytes sent.
*/ */
size_t JsonRpc::SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message) size_t JsonRpc::SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message)
{ {
String json = JsonEncode(message); String json = JsonEncode(message);
@ -71,7 +71,7 @@ size_t JsonRpc::SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const
* *
* @return The amount of bytes sent. * @return The amount of bytes sent.
*/ */
size_t JsonRpc::SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc) size_t JsonRpc::SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc)
{ {
return JsonRpc::SendRawMessage(stream, JsonEncode(message), yc); return JsonRpc::SendRawMessage(stream, JsonEncode(message), yc);
} }
@ -85,7 +85,7 @@ size_t JsonRpc::SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const
* *
* @return bytes sent * @return bytes sent
*/ */
size_t JsonRpc::SendRawMessage(const std::shared_ptr<AsioTlsStream>& stream, const String& json, boost::asio::yield_context yc) size_t JsonRpc::SendRawMessage(const Shared<AsioTlsStream>::Ptr& stream, const String& json, boost::asio::yield_context yc)
{ {
#ifdef I2_DEBUG #ifdef I2_DEBUG
if (GetDebugJsonRpcCached()) if (GetDebugJsonRpcCached())
@ -104,7 +104,7 @@ size_t JsonRpc::SendRawMessage(const std::shared_ptr<AsioTlsStream>& stream, con
* @return A JSON string * @return A JSON string
*/ */
String JsonRpc::ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, ssize_t maxMessageLength) String JsonRpc::ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, ssize_t maxMessageLength)
{ {
String jsonString = NetString::ReadStringFromStream(stream, maxMessageLength); String jsonString = NetString::ReadStringFromStream(stream, maxMessageLength);
@ -125,7 +125,7 @@ String JsonRpc::ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, ssize_
* *
* @return A JSON string * @return A JSON string
*/ */
String JsonRpc::ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, boost::asio::yield_context yc, ssize_t maxMessageLength) String JsonRpc::ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, boost::asio::yield_context yc, ssize_t maxMessageLength)
{ {
String jsonString = NetString::ReadStringFromStream(stream, yc, maxMessageLength); String jsonString = NetString::ReadStringFromStream(stream, yc, maxMessageLength);

View File

@ -21,12 +21,12 @@ namespace icinga
class JsonRpc class JsonRpc
{ {
public: public:
static size_t SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message); static size_t SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message);
static size_t SendMessage(const std::shared_ptr<AsioTlsStream>& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc); static size_t SendMessage(const Shared<AsioTlsStream>::Ptr& stream, const Dictionary::Ptr& message, boost::asio::yield_context yc);
static size_t SendRawMessage(const std::shared_ptr<AsioTlsStream>& stream, const String& json, boost::asio::yield_context yc); static size_t SendRawMessage(const Shared<AsioTlsStream>::Ptr& stream, const String& json, boost::asio::yield_context yc);
static String ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, ssize_t maxMessageLength = -1); static String ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, ssize_t maxMessageLength = -1);
static String ReadMessage(const std::shared_ptr<AsioTlsStream>& stream, boost::asio::yield_context yc, ssize_t maxMessageLength = -1); static String ReadMessage(const Shared<AsioTlsStream>::Ptr& stream, boost::asio::yield_context yc, ssize_t maxMessageLength = -1);
static Dictionary::Ptr DecodeMessage(const String& message); static Dictionary::Ptr DecodeMessage(const String& message);

View File

@ -30,13 +30,13 @@ REGISTER_APIFUNCTION(SetLogPosition, log, &SetLogPositionHandler);
static RingBuffer l_TaskStats (15 * 60); static RingBuffer l_TaskStats (15 * 60);
JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated, JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated,
const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role) const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role)
: JsonRpcConnection(identity, authenticated, stream, role, IoEngine::Get().GetIoContext()) : JsonRpcConnection(identity, authenticated, stream, role, IoEngine::Get().GetIoContext())
{ {
} }
JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated, JsonRpcConnection::JsonRpcConnection(const String& identity, bool authenticated,
const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role, boost::asio::io_context& io) const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io)
: m_Identity(identity), m_Authenticated(authenticated), m_Stream(stream), m_Role(role), : 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_Timestamp(Utility::GetTime()), m_Seen(Utility::GetTime()), m_NextHeartbeat(0), m_IoStrand(io),
m_OutgoingMessagesQueued(io), m_WriterDone(io), m_ShuttingDown(false), m_OutgoingMessagesQueued(io), m_WriterDone(io), m_ShuttingDown(false),
@ -158,7 +158,7 @@ Endpoint::Ptr JsonRpcConnection::GetEndpoint() const
return m_Endpoint; return m_Endpoint;
} }
std::shared_ptr<AsioTlsStream> JsonRpcConnection::GetStream() const Shared<AsioTlsStream>::Ptr JsonRpcConnection::GetStream() const
{ {
return m_Stream; return m_Stream;
} }

View File

@ -42,7 +42,7 @@ class JsonRpcConnection final : public Object
public: public:
DECLARE_PTR_TYPEDEFS(JsonRpcConnection); DECLARE_PTR_TYPEDEFS(JsonRpcConnection);
JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role); JsonRpcConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role);
void Start(); void Start();
@ -50,7 +50,7 @@ public:
String GetIdentity() const; String GetIdentity() const;
bool IsAuthenticated() const; bool IsAuthenticated() const;
Endpoint::Ptr GetEndpoint() const; Endpoint::Ptr GetEndpoint() const;
std::shared_ptr<AsioTlsStream> GetStream() const; Shared<AsioTlsStream>::Ptr GetStream() const;
ConnectionRole GetRole() const; ConnectionRole GetRole() const;
void Disconnect(); void Disconnect();
@ -68,7 +68,7 @@ private:
String m_Identity; String m_Identity;
bool m_Authenticated; bool m_Authenticated;
Endpoint::Ptr m_Endpoint; Endpoint::Ptr m_Endpoint;
std::shared_ptr<AsioTlsStream> m_Stream; Shared<AsioTlsStream>::Ptr m_Stream;
ConnectionRole m_Role; ConnectionRole m_Role;
double m_Timestamp; double m_Timestamp;
double m_Seen; double m_Seen;
@ -80,7 +80,7 @@ private:
bool m_ShuttingDown; bool m_ShuttingDown;
boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer; boost::asio::deadline_timer m_CheckLivenessTimer, m_HeartbeatTimer;
JsonRpcConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream, ConnectionRole role, boost::asio::io_context& io); JsonRpcConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, ConnectionRole role, boost::asio::io_context& io);
void HandleIncomingMessages(boost::asio::yield_context yc); void HandleIncomingMessages(boost::asio::yield_context yc);
void WriteOutgoingMessages(boost::asio::yield_context yc); void WriteOutgoingMessages(boost::asio::yield_context yc);

View File

@ -81,7 +81,7 @@ int PkiUtility::SignCsr(const String& csrfile, const String& certfile)
std::shared_ptr<X509> PkiUtility::FetchCert(const String& host, const String& port) std::shared_ptr<X509> PkiUtility::FetchCert(const String& host, const String& port)
{ {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(); sslContext = MakeAsioSslContext();
@ -93,7 +93,7 @@ std::shared_ptr<X509> PkiUtility::FetchCert(const String& host, const String& po
return std::shared_ptr<X509>(); return std::shared_ptr<X509>();
} }
auto stream (std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, host)); auto stream (Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host));
try { try {
Connect(stream->lowest_layer(), host, port); Connect(stream->lowest_layer(), host, port);
@ -149,7 +149,7 @@ int PkiUtility::GenTicket(const String& cn, const String& salt, std::ostream& ti
int PkiUtility::RequestCertificate(const String& host, const String& port, const String& keyfile, int PkiUtility::RequestCertificate(const String& host, const String& port, const String& keyfile,
const String& certfile, const String& cafile, const std::shared_ptr<X509>& trustedCert, const String& ticket) const String& certfile, const String& cafile, const std::shared_ptr<X509>& trustedCert, const String& ticket)
{ {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(certfile, keyfile); sslContext = MakeAsioSslContext(certfile, keyfile);
@ -161,7 +161,7 @@ int PkiUtility::RequestCertificate(const String& host, const String& port, const
return 1; return 1;
} }
auto stream (std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, host)); auto stream (Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host));
try { try {
Connect(stream->lowest_layer(), host, port); Connect(stream->lowest_layer(), host, port);

View File

@ -174,9 +174,9 @@ static int FormatOutput(const Dictionary::Ptr& result)
* *
* @returns AsioTlsStream pointer for future HTTP connections. * @returns AsioTlsStream pointer for future HTTP connections.
*/ */
static std::shared_ptr<AsioTlsStream> Connect(const String& host, const String& port) static Shared<AsioTlsStream>::Ptr Connect(const String& host, const String& port)
{ {
std::shared_ptr<boost::asio::ssl::context> sslContext; Shared<boost::asio::ssl::context>::Ptr sslContext;
try { try {
sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters
@ -186,7 +186,7 @@ static std::shared_ptr<AsioTlsStream> Connect(const String& host, const String&
throw; throw;
} }
std::shared_ptr<AsioTlsStream> stream = std::make_shared<AsioTlsStream>(IoEngine::Get().GetIoContext(), *sslContext, host); Shared<AsioTlsStream>::Ptr stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host);
try { try {
icinga::Connect(stream->lowest_layer(), host, port); icinga::Connect(stream->lowest_layer(), host, port);
@ -338,7 +338,7 @@ static Dictionary::Ptr FetchData(const String& host, const String& port, const S
namespace beast = boost::beast; namespace beast = boost::beast;
namespace http = beast::http; namespace http = beast::http;
std::shared_ptr<AsioTlsStream> tlsStream; Shared<AsioTlsStream>::Ptr tlsStream;
try { try {
tlsStream = Connect(host, port); tlsStream = Connect(host, port);