This commit is contained in:
Timur A. Fatkhullin 2024-09-24 21:51:05 +03:00
parent 02dee70db9
commit 2cf0b1f94c
3 changed files with 60 additions and 32 deletions

View File

@ -65,22 +65,23 @@ struct AdcStopSeqSessionProto {
template <std::input_iterator IT>
std::pair<IT, bool> search(IT begin, IT end)
auto search(IT begin, IT end)
{
auto res = std::make_pair(begin, false);
std::tuple<IT, IT, bool> res{begin, end, false};
if (begin == end) {
return res;
}
res.first = std::search(begin, end, STOP_SEQ.begin(), STOP_SEQ.end());
if (res.first != end) {
std::advance(res.first, STOP_SEQ_SIZE); // move iterator to the one-past-the-end position
res.second = true;
auto it = std::search(begin, end, STOP_SEQ.begin(), STOP_SEQ.end());
if (it != end) {
// std::advance(it, STOP_SEQ_SIZE); // move iterator to the one-past-the-end position
std::get<1>(res) = it + STOP_SEQ_SIZE;
std::get<2>(res) = true;
} else {
// may be only a part of valid byte sequence was received,
// so start next matching from previous begin-iterator
res.first = begin;
std::get<1>(res) = begin;
}
return res;

View File

@ -364,9 +364,9 @@ public:
// 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 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>();
@ -432,7 +432,9 @@ public:
}
// here, the iterators were computed in MatchCondition called by asio::async_read_until function!!!
std::string_view net_pack{std::get<0>(*s_res), std::get<1>(*s_res)};
// std::string_view net_pack{std::get<0>(*s_res), std::get<1>(*s_res)};
size_t N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
std::span net_pack{&*std::get<0>(*s_res), N};
std::ranges::copy(this->fromProto(net_pack), std::back_inserter(msg));
_streamBuffer.consume(net_pack.size());
@ -442,14 +444,20 @@ public:
// auto end_it = (const char*)asio_streambuff_iter_t::end(_streamBuffer.data());
// auto begin_it = asio_streambuff_iter_t::begin(_streamBuffer.data());
// auto end_it = asio_streambuff_iter_t::end(_streamBuffer.data());
auto begin_it =
static_cast<std::ranges::iterator_t<std::string_view>>(_streamBuffer.data().data());
auto end_it = begin_it + _streamBuffer.data().size();
// auto begin_it =
// static_cast<std::ranges::iterator_t<std::string_view>>(_streamBuffer.data().data());
// auto end_it = begin_it + _streamBuffer.data().size();
auto begin_it = asio::buffers_begin(_streamBuffer.data());
auto end_it = asio::buffers_end(_streamBuffer.data());
*s_res = this->search(std::span(begin_it, end_it));
// *s_res = this->search(std::span(begin_it, end_it));
*s_res = this->search(begin_it, end_it);
if (std::get<2>(*s_res)) {
net_pack = std::string_view{std::get<0>(*s_res), std::get<1>(*s_res)};
// net_pack = std::string_view{std::get<0>(*s_res), std::get<1>(*s_res)};
N = std::distance(std::get<0>(*s_res), std::get<1>(*s_res));
net_pack = std::span{&*std::get<0>(*s_res), N};
_receiveQueue.emplace();
std::ranges::copy(this->fromProto(net_pack), std::back_inserter(_receiveQueue.back()));
@ -563,12 +571,13 @@ protected:
template <typename T>
auto MatchCondition(asio_streambuff_iter_t begin, asio_streambuff_iter_t end, T& s_res)
{
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);
}
// 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));

View File

@ -4,6 +4,21 @@
#include "../net/adc_netproto.h"
#include "../net/asio/adc_netservice_asio.h"
template <typename T>
void receive(T& srv)
{
srv.asyncReceive(
[&srv](std::error_code ec, std::string msg) {
if (!ec) {
std::cout << "Received: [" << msg << "]\n";
receive(srv);
} else {
std::cout << "Received: " << ec.message() << "\n";
}
},
std::chrono::minutes(1));
}
int main()
{
asio::ip::tcp::endpoint ept_s(asio::ip::tcp::v4(), 9999);
@ -17,10 +32,10 @@ int main()
adc::impl::AdcNetServiceASIOBase<asio::ip::tcp, adc::AdcStopSeqSessionProto<>> srv(ctx);
adc::impl::AdcNetServiceASIOBase<asio::ip::tcp, adc::AdcStopSeqSessionProto<>>::asio_async_ctx_t srv_ctx;
srv_ctx.accept_comp_token = [](std::error_code ec) {
// adc::impl::AdcNetServiceASIOBase<asio::ip::tcp, adc::AdcStopSeqSessionProto<>>::asio_async_ctx_t srv_ctx;
// srv_ctx.accept_comp_token = [](std::error_code ec) {
};
// };
// srv.asyncAccept(ept_s, srv_ctx, std::chrono::seconds(120));
// srv.asyncConnect(ept_c, [](std::error_code ec) {
@ -38,13 +53,16 @@ int main()
if (!ec) {
std::cout << "New connection\n";
srv.asyncReceive(
[](std::error_code ec, std::string msg) {
if (!ec) {
std::cout << "Received: [" << msg << "]\n";
}
},
std::chrono::minutes(3));
// srv.asyncReceive(
// [](std::error_code ec, std::string msg) {
// if (!ec) {
// std::cout << "Received: [" << msg << "]\n";
// } else {
// std::cout << "Received: " << ec.message() << "\n";
// }
// },
// std::chrono::minutes(1));
receive(srv);
} else {
std::cout << "ACCEPT ERR: " << ec.message() << "\n";
}