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