This commit is contained in:
2025-09-20 23:30:18 -05:00
parent b339ee435c
commit b0e74a30a2

View File

@@ -107,20 +107,21 @@ void run_with_deadline(net::io_context &io_ctx, op_t &&operation,
std::string_view function_name) { std::string_view function_name) {
deadline = std::max(deadline, std::chrono::milliseconds{250}); deadline = std::max(deadline, std::chrono::milliseconds{250});
boost::system::error_code err{};
bool done = false; bool done = false;
bool timed_out = false; bool timed_out = false;
net::steady_timer timer{io_ctx}; net::steady_timer timer{io_ctx};
timer.expires_after(deadline); timer.expires_after(deadline);
timer.async_wait([&](const boost::system::error_code &err_) { timer.async_wait(
if (not err_ && not done) { [&cancel_op, &done, &timed_out](const boost::system::error_code &err_) {
timed_out = true; if (not err_ && not done) {
std::forward<cancel_t>(cancel_op)(); timed_out = true;
} cancel_op();
}); }
});
std::forward<op_t>(operation)([&](const boost::system::error_code &err_) { boost::system::error_code err{};
operation([&done, &err](const boost::system::error_code &err_) {
err = err_; err = err_;
done = true; done = true;
}); });
@@ -150,21 +151,22 @@ void connect_with_deadline(net::io_context &io_ctx,
run_with_deadline( run_with_deadline(
io_ctx, io_ctx,
[&](auto &&handler) { [&sock, &endpoints](auto &&handler) {
net::async_connect(sock, endpoints, net::async_connect(sock, endpoints,
[handler = std::forward<decltype(handler)>(handler)]( [handler](auto &&err, auto &&) { handler(err); });
const boost::system::error_code &err,
const auto &) { handler(err); });
}, },
[&]() { sock.cancel(); }, deadline, "connect", "connect", function_name); [&sock]() { sock.cancel(); }, deadline, "connect", "connect",
function_name);
} }
void read_exact_with_deadline(net::io_context &io_ctx, void read_exact_with_deadline(net::io_context &io_ctx,
boost::asio::ip::tcp::socket &sock, auto buf, boost::asio::ip::tcp::socket &sock,
boost::asio::mutable_buffer buf,
std::chrono::milliseconds deadline) { std::chrono::milliseconds deadline) {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
auto *base = static_cast<std::uint8_t *>(const_cast<void *>(buf.data())); auto *base = static_cast<std::uint8_t *>(buf.data());
std::size_t total = buf.size(); std::size_t total = buf.size();
std::size_t offset = 0U; std::size_t offset = 0U;
@@ -176,13 +178,13 @@ void read_exact_with_deadline(net::io_context &io_ctx,
[&](auto &&handler) { [&](auto &&handler) {
sock.async_read_some( sock.async_read_some(
net::buffer(base + offset, total - offset), net::buffer(base + offset, total - offset),
[&, handler = std::forward<decltype(handler)>(handler)]( [&bytes_read, handler](auto &&err, auto &&count) {
const boost::system::error_code &err, std::size_t count) {
bytes_read = count; bytes_read = count;
handler(err); handler(err);
}); });
}, },
[&]() { sock.cancel(); }, deadline, "response", "read", function_name); [&sock]() { sock.cancel(); }, deadline, "response", "read",
function_name);
if (bytes_read == 0U) { if (bytes_read == 0U) {
throw std::runtime_error("0 bytes read"); throw std::runtime_error("0 bytes read");
@@ -193,11 +195,12 @@ void read_exact_with_deadline(net::io_context &io_ctx,
} }
void write_all_with_deadline(net::io_context &io_ctx, void write_all_with_deadline(net::io_context &io_ctx,
boost::asio::ip::tcp::socket &sock, auto buf, boost::asio::ip::tcp::socket &sock,
boost::asio::mutable_buffer buf,
std::chrono::milliseconds deadline) { std::chrono::milliseconds deadline) {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
auto *base = static_cast<std::uint8_t *>(const_cast<void *>(buf.data())); auto *base = static_cast<std::uint8_t *>(buf.data());
std::size_t total = buf.size(); std::size_t total = buf.size();
std::size_t offset = 0U; std::size_t offset = 0U;
@@ -209,13 +212,13 @@ void write_all_with_deadline(net::io_context &io_ctx,
[&](auto &&handler) { [&](auto &&handler) {
sock.async_write_some( sock.async_write_some(
net::buffer(base + offset, total - offset), net::buffer(base + offset, total - offset),
[&, handler = std::forward<decltype(handler)>(handler)]( [&bytes_written, handler](auto &&err, auto &&count) {
const boost::system::error_code &err, std::size_t count) {
bytes_written = count; bytes_written = count;
handler(err); handler(err);
}); });
}, },
[&]() { sock.cancel(); }, deadline, "request", "write", function_name); [&sock]() { sock.cancel(); }, deadline, "request", "write",
function_name);
if (bytes_written == 0U) { if (bytes_written == 0U) {
throw std::runtime_error("0 bytes written"); throw std::runtime_error("0 bytes written");