This commit is contained in:
2024-09-24 18:09:59 +03:00
parent eb44cd114d
commit 02dee70db9
3 changed files with 102 additions and 39 deletions

View File

@@ -191,7 +191,12 @@ public:
static constexpr std::chrono::duration DEFAULT_RECEIVE_TIMEOUT = std::chrono::seconds(5);
AdcNetServiceASIOBase(asio::io_context& ctx)
: SESSION_PROTOT(), _ioContext(ctx), _receiveStrand(_ioContext), _receiveQueue(), _socket(_ioContext)
: SESSION_PROTOT(),
_ioContext(ctx),
_receiveStrand(_ioContext),
_receiveQueue(),
_acceptor(_ioContext),
_socket(_ioContext)
{
}
@@ -228,26 +233,30 @@ public:
static_assert(false, "INVALID TRANSPORT PROTOCOL TYPE!");
}
auto acc = acceptor_t(_ioContext);
// auto acc = acceptor_t(_ioContext);
auto timer = getDeadlineTimer(acc, timeout);
// auto timer = getDeadlineTimer(acc, timeout);
auto timer = getDeadlineTimer(_acceptor, timeout);
return asio::async_compose<TokenT, void(std::error_code)>(
[acc = std::move(acc), timer = std::move(timer), start = true, &endpoint, this](
auto& self, std::error_code ec = {}) mutable {
// [acc = std::move(acc), timer = std::move(timer), start = true, &endpoint, this](
[timer = std::move(timer), start = true, &endpoint, this](auto& self, std::error_code ec = {}) mutable {
if (!ec) {
if (start) {
start = false;
try {
acc = acceptor_t(_ioContext, endpoint);
// _socket.open(asio::ip::tcp::v4());
// acc = acceptor_t(_ioContext, endpoint);
if (!_acceptor.is_open() || (_acceptor.local_endpoint() != endpoint)) {
_acceptor = acceptor_t(_ioContext, endpoint);
}
} catch (std::system_error err) {
timer->cancel();
self.complete(err.code());
return;
}
return acc.async_accept(_socket, std::move(self));
// return acc.async_accept(_socket, std::move(self));
return _acceptor.async_accept(_socket, std::move(self));
}
}
@@ -354,8 +363,10 @@ public:
std::remove_cvref_t<std::tuple_element_t<1, typename traits::adc_func_traits<TokenT>::args_t>>>;
// auto s_res = std::make_shared<std::invoke_result_t<decltype(this->template search<RMSGT>), RMSGT>>();
auto tp = this->search(std::span<const char>());
auto s_res = std::make_shared<decltype(tp)>();
// auto s_res = std::make_shared<std::tuple<asio_streambuff_iter_t, asio_streambuff_iter_t, bool>>();
auto out_flags = std::make_shared<asio::socket_base::message_flags>();
@@ -390,13 +401,14 @@ public:
// std::pair<IT, bool> res{std::get<1>(*s_res), std::get<2>(*s_res)};
// return res;
// };
auto match_func = [s_res, this](asio_streambuff_iter_t begin, asio_streambuff_iter_t end) {
*s_res = this->search(std::span(&*begin, &*end));
// return std::make_pair(std::get<1>(*s_res), std::get<2>(*s_res));
auto N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
std::pair<asio_streambuff_iter_t, bool> res{begin + N, std::get<2>(*s_res)};
return res;
};
// auto match_func = [s_res, this](asio_streambuff_iter_t begin, asio_streambuff_iter_t end)
// {
// *s_res = this->search(std::span(&*begin, &*end));
// // return std::make_pair(std::get<1>(*s_res), std::get<2>(*s_res));
// auto N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
// std::pair<asio_streambuff_iter_t, bool> res{begin + N, std::get<2>(*s_res)};
// return res;
// };
// return asio::async_read_until(_socket, _streamBuffer, std::move(match_func),
// std::move(self));
@@ -540,7 +552,7 @@ protected:
socket_t _socket;
// acceptor_t _acceptor;
acceptor_t _acceptor;
asio::streambuf _streamBuffer;
@@ -551,10 +563,22 @@ protected:
template <typename T>
auto MatchCondition(asio_streambuff_iter_t begin, asio_streambuff_iter_t end, T& s_res)
{
*s_res = this->search(std::span(&*begin, &*end));
if (begin == end) {
*s_res = this->search(std::span<const char>());
} else {
*s_res = this->search(std::span(&*begin, &*end));
// *s_res = this->search(begin, end);
}
// return std::make_pair(std::get<1>(*s_res), std::get<2>(*s_res));
auto N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
std::pair<asio_streambuff_iter_t, bool> res{begin + N, std::get<2>(*s_res)};
std::pair<asio_streambuff_iter_t, bool> res{end, false};
typename std::iterator_traits<asio_streambuff_iter_t>::difference_type N = 0;
if (std::get<2>(*s_res)) {
N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
res = std::make_pair(begin + N, true);
}
return res;
};