From 78069a8032d199b5559372a2384668c5d3a9a47f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 16 Jan 2023 15:29:49 +0100 Subject: [PATCH] finish request strategy (for now) --- external/tox_ngc_ft1/tox_ngc_ft1 | 2 +- src/states/send_start_sha1.cpp | 2 +- src/states/sha1.cpp | 20 ++++++++++++++++++-- src/states/sha1.hpp | 7 ++++--- src/tox_client.cpp | 1 + 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/external/tox_ngc_ft1/tox_ngc_ft1 b/external/tox_ngc_ft1/tox_ngc_ft1 index 04befb2..bbe4453 160000 --- a/external/tox_ngc_ft1/tox_ngc_ft1 +++ b/external/tox_ngc_ft1/tox_ngc_ft1 @@ -1 +1 @@ -Subproject commit 04befb21be0327996eb3597cb91d5bdd00066974 +Subproject commit bbe4453833ad561c79a7cae1a5ac5e66d06535f7 diff --git a/src/states/send_start_sha1.cpp b/src/states/send_start_sha1.cpp index b492aa2..c43feb4 100644 --- a/src/states/send_start_sha1.cpp +++ b/src/states/send_start_sha1.cpp @@ -27,7 +27,7 @@ SendStartSHA1::SendStartSHA1(ToxClient& tcl, const CommandLine& cl) : StateI(tcl assert(!_file_map.empty()); // build info - _sha1_info.file_name = std::filesystem::path(cl.send_path).filename(); + _sha1_info.file_name = std::filesystem::path(cl.send_path).filename().string(); _sha1_info.file_size = _file_map.length(); { // build chunks diff --git a/src/states/sha1.cpp b/src/states/sha1.cpp index 6b6d0ed..78d640e 100644 --- a/src/states/sha1.cpp +++ b/src/states/sha1.cpp @@ -82,11 +82,24 @@ bool SHA1::iterate(float delta) { // if we have not heard for 10sec, timeout if (time_since_remove_activity >= 10.f) { std::cerr << "SHA1 receiving chunk tansfer timed out " << std::get<0>(*it) << ":" << std::get<1>(*it) << "." << int(std::get<2>(*it)) << "\n"; + _chunk_want_queue.push_back(std::get<4>(*it)); // put it back it = _transfers_receiving_chunk.erase(it); } else { it++; } } + // sent requests + for (auto it = _chunks_requested.begin(); it != _chunks_requested.end();) { + it->second += delta; + + // if we have not heard for 15sec, timeout + if (it->second >= 15.f) { + _chunk_want_queue.push_back(it->first); // put it back + it = _chunks_requested.erase(it); + } else { + it++; + } + } } // if we have not reached the total cap for transfers @@ -142,7 +155,7 @@ bool SHA1::iterate(float delta) { } } - if (!_have_all && !_chunk_want_queue.empty() && _transfers_receiving_chunk.size() < _max_concurrent_in) { + if (!_have_all && !_chunk_want_queue.empty() && _chunks_requested.size() + _transfers_receiving_chunk.size() < _max_concurrent_in) { // send out request, no burst tho std::vector> target_peers; _tcl.forEachGroup([&target_peers, this](uint32_t group_number) { @@ -162,7 +175,7 @@ bool SHA1::iterate(float delta) { auto [group_number, peer_number] = target_peers.at(target_index); size_t chunk_index = _chunk_want_queue.front(); - _chunks_requested.emplace(chunk_index); + _chunks_requested[chunk_index] = 0.f; _chunk_want_queue.pop_front(); _tcl.sendFT1RequestPrivate(group_number, peer_number, NGC_FT1_file_kind::HASH_SHA1_CHUNK, _sha1_info.chunks[chunk_index].data.data(), 20); @@ -301,6 +314,9 @@ bool SHA1::onFT1ReceiveInitSHA1Chunk(uint32_t group_number, uint32_t peer_number ) ); + // remove form requests + _chunks_requested.erase(chunk_index); + return true; } diff --git a/src/states/sha1.hpp b/src/states/sha1.hpp index 767cf77..af957f0 100644 --- a/src/states/sha1.hpp +++ b/src/states/sha1.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include @@ -66,10 +66,11 @@ struct SHA1 final : public StateI { bool _have_all {false}; size_t _have_count {0}; std::deque _chunk_want_queue; - std::set _chunks_requested; + // chunk_index -> time since request + std::map _chunks_requested; const size_t _max_concurrent_out {4}; - const size_t _max_concurrent_in {4}; + const size_t _max_concurrent_in {16}; std::minstd_rand _rng {1337}; std::uniform_int_distribution _distrib; diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 7894fc2..8387b57 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -253,6 +253,7 @@ void ToxClient::onToxGroupPeerExit(uint32_t group_number, uint32_t peer_id, Tox_ void ToxClient::onToxGroupSelfJoin(uint32_t group_number) { std::cout << "TCL group self join " << group_number << "\n"; // ??? + // can be triggered after other peers allready joined o.o _tox_profile_dirty = true; }