Files

138 lines
3.9 KiB
C++
Raw Permalink Normal View History

2024-11-13 08:50:16 +01:00
//
2025-02-14 13:42:23 +01:00
// Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
2024-11-13 08:50:16 +01:00
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2025-02-14 13:42:23 +01:00
2025-01-13 16:11:41 +01:00
#ifndef BOOST_MQTT5_TEST_AUTOCONNECT_STREAM_HPP
#define BOOST_MQTT5_TEST_AUTOCONNECT_STREAM_HPP
#include <boost/mqtt5/detail/async_mutex.hpp>
#include <boost/mqtt5/detail/async_traits.hpp>
#include <boost/mqtt5/detail/log_invoke.hpp>
#include <boost/mqtt5/impl/endpoints.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/system/error_code.hpp>
2024-11-13 08:50:16 +01:00
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
2025-01-13 16:11:41 +01:00
namespace boost::mqtt5::test {
2024-11-13 08:50:16 +01:00
namespace asio = boost::asio;
using error_code = boost::system::error_code;
template <
2025-01-13 16:11:41 +01:00
typename StreamType,
typename StreamContext = std::monostate,
typename LoggerType = boost::mqtt5::noop_logger
2024-11-13 08:50:16 +01:00
>
class test_autoconnect_stream {
public:
2025-01-13 16:11:41 +01:00
using stream_type = StreamType;
using stream_ptr = std::shared_ptr<stream_type>;
using stream_context_type = StreamContext;
using executor_type = typename stream_type::executor_type;
using logger_type = LoggerType;
2024-11-13 08:50:16 +01:00
private:
2025-02-13 13:20:55 +01:00
using async_mutex = boost::mqtt5::detail::async_mutex;
using endpoints = boost::mqtt5::detail::endpoints<logger_type>;
using log_invoke = boost::mqtt5::detail::log_invoke<logger_type>;
2025-01-13 16:11:41 +01:00
executor_type _stream_executor;
2025-02-13 13:20:55 +01:00
async_mutex _conn_mtx;
2025-01-13 16:11:41 +01:00
asio::steady_timer _connect_timer;
2025-02-13 13:20:55 +01:00
endpoints _endpoints;
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
stream_ptr _stream_ptr;
stream_context_type& _stream_context;
2024-11-13 08:50:16 +01:00
2025-02-13 13:20:55 +01:00
log_invoke _log;
2024-11-27 12:52:01 +01:00
2025-01-13 16:11:41 +01:00
template <typename Stream>
friend class boost::mqtt5::detail::reconnect_op;
2024-11-13 08:50:16 +01:00
public:
2025-01-13 16:11:41 +01:00
test_autoconnect_stream(
const executor_type& ex,
stream_context_type& context,
2025-02-13 13:20:55 +01:00
log_invoke& log
2025-01-13 16:11:41 +01:00
) :
_stream_executor(ex),
_conn_mtx(_stream_executor),
_connect_timer(_stream_executor),
_endpoints(_stream_executor, _connect_timer, log),
_stream_context(context),
_log(log)
{
replace_next_layer(construct_next_layer());
open_lowest_layer(_stream_ptr, asio::ip::tcp::v4());
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
test_autoconnect_stream(const test_autoconnect_stream&) = delete;
test_autoconnect_stream& operator=(const test_autoconnect_stream&) = delete;
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
stream_ptr stream_pointer() const {
return _stream_ptr;
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
bool is_open() const noexcept {
2025-02-13 13:20:55 +01:00
return boost::mqtt5::detail::lowest_layer(*_stream_ptr).is_open();
2025-01-13 16:11:41 +01:00
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
void brokers(std::string hosts, uint16_t default_port) {
_endpoints.brokers(std::move(hosts), default_port);
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
static void open_lowest_layer(const stream_ptr& sptr, asio::ip::tcp protocol) {
error_code ec;
2025-02-13 13:20:55 +01:00
auto& layer = boost::mqtt5::detail::lowest_layer(*sptr);
2025-01-13 16:11:41 +01:00
layer.open(protocol, ec);
layer.set_option(asio::socket_base::reuse_address(true), ec);
layer.set_option(asio::ip::tcp::no_delay(true), ec);
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
void close() {
error_code ec;
2025-02-13 13:20:55 +01:00
boost::mqtt5::detail::lowest_layer(*_stream_ptr).close(ec);
2025-01-13 16:11:41 +01:00
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
stream_ptr construct_next_layer() const {
stream_ptr sptr;
2025-02-13 13:20:55 +01:00
if constexpr (boost::mqtt5::detail::has_tls_context<StreamContext>)
2025-01-13 16:11:41 +01:00
sptr = std::make_shared<stream_type>(
_stream_executor, _stream_context.tls_context()
);
else
sptr = std::make_shared<stream_type>(_stream_executor);
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
return sptr;
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
stream_ptr construct_and_open_next_layer(asio::ip::tcp protocol) const {
auto sptr = construct_next_layer();
open_lowest_layer(sptr, protocol);
return sptr;
}
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
void replace_next_layer(stream_ptr sptr) {
if (_stream_ptr)
close();
std::exchange(_stream_ptr, std::move(sptr));
}
2024-11-27 12:52:01 +01:00
private:
2025-02-13 13:20:55 +01:00
log_invoke& log() {
2025-01-13 16:11:41 +01:00
return _log;
}
2024-11-13 08:50:16 +01:00
};
2025-01-13 16:11:41 +01:00
} // end namespace boost::mqtt5::test
2024-11-13 08:50:16 +01:00
2025-01-13 16:11:41 +01:00
#endif // !BOOST_MQTT5_TEST_AUTOCONNECT_STREAM_HPP