mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-02 13:37:47 +00:00
Added thirdparty: boost library
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/connection.hpp>
|
||||
|
||||
namespace boost::redis {
|
||||
|
||||
connection::connection(
|
||||
executor_type ex,
|
||||
asio::ssl::context::method method,
|
||||
std::size_t max_read_size)
|
||||
: impl_{ex, method, max_read_size}
|
||||
{ }
|
||||
|
||||
connection::connection(
|
||||
asio::io_context& ioc,
|
||||
asio::ssl::context::method method,
|
||||
std::size_t max_read_size)
|
||||
: impl_{ioc.get_executor(), method, max_read_size}
|
||||
{ }
|
||||
|
||||
void
|
||||
connection::async_run_impl(
|
||||
config const& cfg,
|
||||
logger l,
|
||||
asio::any_completion_handler<void(boost::system::error_code)> token)
|
||||
{
|
||||
impl_.async_run(cfg, l, std::move(token));
|
||||
}
|
||||
|
||||
void connection::cancel(operation op)
|
||||
{
|
||||
impl_.cancel(op);
|
||||
}
|
||||
|
||||
} // namespace boost::redis
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/error.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
namespace boost::redis {
|
||||
namespace detail {
|
||||
|
||||
struct error_category_impl : system::error_category {
|
||||
|
||||
virtual ~error_category_impl() = default;
|
||||
|
||||
auto name() const noexcept -> char const* override
|
||||
{
|
||||
return "boost.redis";
|
||||
}
|
||||
|
||||
auto message(int ev) const -> std::string override
|
||||
{
|
||||
switch(static_cast<error>(ev)) {
|
||||
case error::invalid_data_type: return "Invalid resp3 type.";
|
||||
case error::not_a_number: return "Can't convert string to number (maybe forgot to upgrade to RESP3?).";
|
||||
case error::exceeeds_max_nested_depth: return "Exceeds the maximum number of nested responses.";
|
||||
case error::unexpected_bool_value: return "Unexpected bool value.";
|
||||
case error::empty_field: return "Expected field value is empty.";
|
||||
case error::expects_resp3_simple_type: return "Expects a resp3 simple type.";
|
||||
case error::expects_resp3_aggregate: return "Expects resp3 aggregate.";
|
||||
case error::expects_resp3_map: return "Expects resp3 map.";
|
||||
case error::expects_resp3_set: return "Expects resp3 set.";
|
||||
case error::nested_aggregate_not_supported: return "Nested aggregate not_supported.";
|
||||
case error::resp3_simple_error: return "Got RESP3 simple-error.";
|
||||
case error::resp3_blob_error: return "Got RESP3 blob-error.";
|
||||
case error::incompatible_size: return "Aggregate container has incompatible size.";
|
||||
case error::not_a_double: return "Not a double.";
|
||||
case error::resp3_null: return "Got RESP3 null.";
|
||||
case error::not_connected: return "Not connected.";
|
||||
case error::resolve_timeout: return "Resolve timeout.";
|
||||
case error::connect_timeout: return "Connect timeout.";
|
||||
case error::pong_timeout: return "Pong timeout.";
|
||||
case error::ssl_handshake_timeout: return "SSL handshake timeout.";
|
||||
case error::sync_receive_push_failed: return "Can't receive server push synchronously without blocking.";
|
||||
case error::incompatible_node_depth: return "Incompatible node depth.";
|
||||
default: BOOST_ASSERT(false); return "Boost.Redis error.";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
auto category() -> system::error_category const&
|
||||
{
|
||||
static error_category_impl instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
auto make_error_code(error e) -> system::error_code
|
||||
{
|
||||
return system::error_code{static_cast<int>(e), detail::category()};
|
||||
}
|
||||
|
||||
} // boost::redis::detail
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/ignore.hpp>
|
||||
|
||||
namespace boost::redis
|
||||
{
|
||||
ignore_t ignore;
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/logger.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
namespace boost::redis
|
||||
{
|
||||
|
||||
void logger::write_prefix()
|
||||
{
|
||||
if (!std::empty(prefix_))
|
||||
std::clog << prefix_;
|
||||
}
|
||||
|
||||
void logger::on_resolve(system::error_code const& ec, asio::ip::tcp::resolver::results_type const& res)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "run-all-op: resolve addresses ";
|
||||
|
||||
if (ec) {
|
||||
std::clog << ec.message() << std::endl;
|
||||
} else {
|
||||
auto begin = std::cbegin(res);
|
||||
auto end = std::cend(res);
|
||||
|
||||
if (begin == end)
|
||||
return;
|
||||
|
||||
std::clog << begin->endpoint();
|
||||
for (auto iter = std::next(begin); iter != end; ++iter)
|
||||
std::clog << ", " << iter->endpoint();
|
||||
}
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void logger::on_connect(system::error_code const& ec, asio::ip::tcp::endpoint const& ep)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "run-all-op: connected to endpoint ";
|
||||
|
||||
if (ec)
|
||||
std::clog << ec.message() << std::endl;
|
||||
else
|
||||
std::clog << ep;
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void logger::on_ssl_handshake(system::error_code const& ec)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "Runner: SSL handshake " << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
void logger::on_connection_lost(system::error_code const& ec)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
if (ec)
|
||||
std::clog << "Connection lost: " << ec.message();
|
||||
else
|
||||
std::clog << "Connection lost.";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void
|
||||
logger::on_write(
|
||||
system::error_code const& ec,
|
||||
std::string const& payload)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
if (ec)
|
||||
std::clog << "writer-op: " << ec.message();
|
||||
else
|
||||
std::clog << "writer-op: " << std::size(payload) << " bytes written.";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void logger::on_read(system::error_code const& ec, std::size_t n)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
if (ec)
|
||||
std::clog << "reader-op: " << ec.message();
|
||||
else
|
||||
std::clog << "reader-op: " << n << " bytes read.";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void logger::on_run(system::error_code const& reader_ec, system::error_code const& writer_ec)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "run-op: "
|
||||
<< reader_ec.message() << " (reader), "
|
||||
<< writer_ec.message() << " (writer)";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void
|
||||
logger::on_hello(
|
||||
system::error_code const& ec,
|
||||
generic_response const& resp)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
if (ec) {
|
||||
std::clog << "hello-op: " << ec.message();
|
||||
if (resp.has_error())
|
||||
std::clog << " (" << resp.error().diagnostic << ")";
|
||||
} else {
|
||||
std::clog << "hello-op: Success";
|
||||
}
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void
|
||||
logger::on_runner(
|
||||
system::error_code const& run_all_ec,
|
||||
system::error_code const& health_check_ec,
|
||||
system::error_code const& hello_ec)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "runner-op: "
|
||||
<< run_all_ec.message() << " (async_run_all), "
|
||||
<< health_check_ec.message() << " (async_health_check) "
|
||||
<< hello_ec.message() << " (async_hello).";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void
|
||||
logger::on_check_health(
|
||||
system::error_code const& ping_ec,
|
||||
system::error_code const& timeout_ec)
|
||||
{
|
||||
if (level_ < level::info)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << "check-health-op: "
|
||||
<< ping_ec.message() << " (async_ping), "
|
||||
<< timeout_ec.message() << " (async_check_timeout).";
|
||||
|
||||
std::clog << std::endl;
|
||||
}
|
||||
|
||||
void logger::trace(std::string_view reason)
|
||||
{
|
||||
if (level_ < level::debug)
|
||||
return;
|
||||
|
||||
write_prefix();
|
||||
|
||||
std::clog << reason << std::endl;
|
||||
}
|
||||
|
||||
} // boost::redis
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/request.hpp>
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace boost::redis::detail {
|
||||
|
||||
auto has_response(std::string_view cmd) -> bool
|
||||
{
|
||||
if (cmd == "SUBSCRIBE") return true;
|
||||
if (cmd == "PSUBSCRIBE") return true;
|
||||
if (cmd == "UNSUBSCRIBE") return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // boost:redis::detail
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
|
||||
*
|
||||
* Distributed under the Boost Software License, Version 1.0. (See
|
||||
* accompanying file LICENSE.txt)
|
||||
*/
|
||||
|
||||
#include <boost/redis/response.hpp>
|
||||
#include <boost/redis/error.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
namespace boost::redis
|
||||
{
|
||||
|
||||
void consume_one(generic_response& r, system::error_code& ec)
|
||||
{
|
||||
if (r.has_error())
|
||||
return; // Nothing to consume.
|
||||
|
||||
if (std::empty(r.value()))
|
||||
return; // Nothing to consume.
|
||||
|
||||
auto const depth = r.value().front().depth;
|
||||
|
||||
// To simplify we will refuse to consume any data-type that is not
|
||||
// a root node. I think there is no use for that and it is complex
|
||||
// since it requires updating parent nodes.
|
||||
if (depth != 0) {
|
||||
ec = error::incompatible_node_depth;
|
||||
return;
|
||||
}
|
||||
|
||||
auto f = [depth](auto const& e)
|
||||
{ return e.depth == depth; };
|
||||
|
||||
auto match = std::find_if(std::next(std::cbegin(r.value())), std::cend(r.value()), f);
|
||||
|
||||
r.value().erase(std::cbegin(r.value()), match);
|
||||
}
|
||||
|
||||
void consume_one(generic_response& r)
|
||||
{
|
||||
system::error_code ec;
|
||||
consume_one(r, ec);
|
||||
if (ec)
|
||||
throw system::system_error(ec);
|
||||
}
|
||||
|
||||
} // boost::redis::resp3
|
||||
Reference in New Issue
Block a user