...
This commit is contained in:
parent
986758430e
commit
e730b85714
@ -15,8 +15,10 @@
|
|||||||
#include <asio/basic_datagram_socket.hpp>
|
#include <asio/basic_datagram_socket.hpp>
|
||||||
#include <asio/basic_seq_packet_socket.hpp>
|
#include <asio/basic_seq_packet_socket.hpp>
|
||||||
#include <asio/basic_stream_socket.hpp>
|
#include <asio/basic_stream_socket.hpp>
|
||||||
|
#include <asio/compose.hpp>
|
||||||
#include <asio/experimental/awaitable_operators.hpp>
|
#include <asio/experimental/awaitable_operators.hpp>
|
||||||
#include <asio/read_until.hpp>
|
#include <asio/read_until.hpp>
|
||||||
|
#include <asio/steady_timer.hpp>>
|
||||||
#include <asio/streambuf.hpp>
|
#include <asio/streambuf.hpp>
|
||||||
#include <asio/write.hpp>
|
#include <asio/write.hpp>
|
||||||
|
|
||||||
@ -37,18 +39,42 @@ public:
|
|||||||
virtual ~AdcNetServiceASIOStream() = default;
|
virtual ~AdcNetServiceASIOStream() = default;
|
||||||
|
|
||||||
|
|
||||||
template <typename TimeoutT, asio::completion_token_for<void(std::error_code)> CompletionTokenT>
|
template <typename TimeoutT, asio::completion_token_for<void(std::error_code, size_t)> CompletionTokenT>
|
||||||
auto asynSend(const NetMessageT& msg, const TimeoutT& timeout, CompletionTokenT&& token)
|
auto asynSend(const NetMessageT& msg, const TimeoutT& timeout, CompletionTokenT&& token)
|
||||||
{
|
{
|
||||||
using namespace asio::experimental::awaitable_operators;
|
// using namespace asio::experimental::awaitable_operators;
|
||||||
|
|
||||||
auto deadline = std::chrono::steady_clock::now() + timeout;
|
// auto deadline = std::chrono::steady_clock::now() + timeout;
|
||||||
|
|
||||||
std::error_code ec;
|
std::unique_ptr<asio::steady_timer> timer(_socket.get_executor());
|
||||||
|
|
||||||
co_await (asyncSendImpl(msg) && watchdog(deadline, ec));
|
timer->expires_after(timeout);
|
||||||
|
timer->async_wait([this, timer = std::move(timer)](const std::error_code& ec) {
|
||||||
|
if (!ec) {
|
||||||
|
_socket.cancel(std::make_error_code(std::errc::timed_out));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
std::forward<CompletionTokenT>(token)(ec);
|
|
||||||
|
if constexpr (std::derived_from<socket_t, asio::basic_stream_socket<typename socket_t::protocol_type>>) {
|
||||||
|
return asio::async_write(_socket, createConstBufferSequence(msg), std::forward<CompletionTokenT>(token));
|
||||||
|
} else if constexpr (std::derived_from<socket_t,
|
||||||
|
asio::basic_datagram_socket<typename socket_t::protocol_type>>) {
|
||||||
|
return _socket.async_send(createConstBufferSequence(msg), std::forward<CompletionTokenT>(token));
|
||||||
|
} else if constexpr (std::derived_from<socket_t,
|
||||||
|
asio::basic_seq_packet_socket<typename socket_t::protocol_type>>) {
|
||||||
|
return _socket.async_send(createConstBufferSequence(msg), std::forward<CompletionTokenT>(token));
|
||||||
|
} else {
|
||||||
|
static_assert(false, "UNKNOWN ASIO-LIBRARY SOCKET TYPE!!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// std::error_code ec;
|
||||||
|
|
||||||
|
// co_await (asyncSendImpl(msg) && watchdog(deadline, ec));
|
||||||
|
|
||||||
|
// std::forward<CompletionTokenT>(token)(ec);
|
||||||
|
// co_return asio::async_compose<CompletionTokenT, void(std::error_code)>(
|
||||||
|
// [ec](auto& self, const std::error_code& = {}) { self.complete(ec); }, token, _socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -63,6 +89,16 @@ protected:
|
|||||||
|
|
||||||
asio::streambuf _streamBuffer;
|
asio::streambuf _streamBuffer;
|
||||||
|
|
||||||
|
std::vector<asio::const_buffer> createConstBufferSequence(const NetMessageT& msg)
|
||||||
|
{
|
||||||
|
std::vector<asio::const_buffer> buff;
|
||||||
|
for (const auto& el : msg.template bytesView<std::vector<std::string_view>>()) {
|
||||||
|
buff.emplace_back(asio::const_buffer(el));
|
||||||
|
}
|
||||||
|
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
|
||||||
asio::awaitable<void> asyncSendImpl(const NetMessageT& msg)
|
asio::awaitable<void> asyncSendImpl(const NetMessageT& msg)
|
||||||
{
|
{
|
||||||
// for (const auto& buff : msg.bytesView()) {
|
// for (const auto& buff : msg.bytesView()) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user