...
This commit is contained in:
@@ -104,80 +104,33 @@ public:
|
||||
|
||||
thread_local static ::asio::steady_timer timeout_timer(_socket.get_executor());
|
||||
|
||||
if constexpr (std::derived_from<net_connection_t::socket_t,
|
||||
::asio::
|
||||
basic_stream_socket<typename net_connection_t::socket_t::protocol_type,
|
||||
typename net_connection_t::socket_t::executor_type>>) {
|
||||
timeout_timer.expires_after(_sendTimeout);
|
||||
|
||||
const size_t buffs_sz = ::asio::buffer_size(buffs);
|
||||
|
||||
if constexpr (std::derived_from<
|
||||
net_connection_t::socket_t,
|
||||
::asio::basic_stream_socket<typename net_connection_t::socket_t::protocol_type,
|
||||
typename net_connection_t::socket_t::executor_type>>) {
|
||||
// has async_write_some method
|
||||
|
||||
timeout_timer.expires_after(_sendTimeout);
|
||||
|
||||
op_res = ::asio::async_write(_socket, buffs, ::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable);
|
||||
op_res = co_await (::asio::async_write(_socket, buffs, ::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable));
|
||||
} else {
|
||||
const size_t bytes_sz = ::asio::buffer_size(buffs);
|
||||
size_t nbytes, nb;
|
||||
::asio::socket_base::message_flags flags{0};
|
||||
|
||||
auto tm = _sendTimeout;
|
||||
|
||||
auto start_tp = std::chrono::steady_clock::now();
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
do {
|
||||
nb = 0;
|
||||
auto bf = std::span(static_cast<const unsigned char*>(buffs[i].data()), buffs[i].size());
|
||||
|
||||
do {
|
||||
timeout_timer.expires_after(tm);
|
||||
|
||||
op_res = _socket.async_send(::asio::buffer(bf), flags,
|
||||
::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable);
|
||||
if (op_res.index() == 1 || ec) {
|
||||
break;
|
||||
}
|
||||
|
||||
nb += std::get<0>(op_res);
|
||||
if (nb < buffs[i].size()) {
|
||||
bf = std::span(static_cast<const unsigned char*>(buffs[i].data()) + nb, buffs[i].size() - nb);
|
||||
|
||||
tm = _sendTimeout -
|
||||
std::chrono::
|
||||
duration_cast<decltype(_sendTimeout)>(std::chrono::steady_clock::now() - start_tp);
|
||||
|
||||
if (tm.count() == 0) {
|
||||
ec = std::make_error_code(std::errc::timed_out);
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
if (op_res.index() == 1 || ec) {
|
||||
break;
|
||||
}
|
||||
|
||||
++i;
|
||||
|
||||
if (i == buffs.size()) { // should never be!!!
|
||||
ec = std::make_error_code(std::errc::io_error);
|
||||
break;
|
||||
}
|
||||
|
||||
nbytes += nb;
|
||||
|
||||
} while (nbytes < bytes_sz);
|
||||
op_res = co_await (_socket.async_send(buffs, flags, ::asio::redirect_error(::asio::use_awaitable, ec)) ||
|
||||
timeout_timer.async_wait(::asio::use_awaitable));
|
||||
}
|
||||
|
||||
if (op_res.index() == 1) {
|
||||
ec = std::make_error_code(std::errc::timed_out);
|
||||
} else {
|
||||
if (!ec) {
|
||||
// log ...
|
||||
if (std::get<0>(op_res) != buffs_sz) { // !!!! should not be for SEQPACKET and UDP sockets!!!
|
||||
ec = std::make_error_code(std::errc::io_error);
|
||||
} else { // log ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user