From 92373d34f7d20583837476beef00c685d2a1fb0f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 9 Jul 2024 11:00:59 +0200 Subject: [PATCH] work around missing contact events (better now) fix missing ft event on reset (oops) hard assert sending transfers can not time out higher level --- solanaceae/ngc_ft1/ngcft1.cpp | 22 +++++++++++++-------- solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp | 26 ++++++++++++++++++++++--- solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp | 6 +++++- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/solanaceae/ngc_ft1/ngcft1.cpp b/solanaceae/ngc_ft1/ngcft1.cpp index 67dbebf..65f3e24 100644 --- a/solanaceae/ngc_ft1/ngcft1.cpp +++ b/solanaceae/ngc_ft1/ngcft1.cpp @@ -60,8 +60,14 @@ void NGCFT1::updateSendTransfer(float time_delta, uint32_t group_number, uint32_ }); if (tf.time_since_activity >= sending_give_up_after) { // no ack after 30sec, close ft - // TODO: notify app std::cerr << "NGCFT1 warning: sending ft finishing timed out, deleting\n"; + dispatch( + NGCFT1_Event::send_done, + Events::NGCFT1_send_done{ + group_number, peer_number, + static_cast(idx), + } + ); // clean up cca tf.ssb.for_each(time_delta, [&](uint16_t id, const std::vector& data, float& time_since_activity) { @@ -155,8 +161,8 @@ void NGCFT1::updateSendTransfer(float time_delta, uint32_t group_number, uint32_ break; default: // invalid state, delete std::cerr << "NGCFT1 error: ft in invalid state, deleting\n"; + assert(false && "ft in invalid state"); tf_opt.reset(); - //continue; return; } } @@ -333,7 +339,7 @@ bool NGCFT1::NGC_FT1_send_message_public( bool NGCFT1::onEvent(const Events::NGCEXT_ft1_request& e) { //#if !NDEBUG - std::cout << "NGCFT1: FT1_REQUEST fk:" << e.file_kind << " [" << bin2hex(e.file_id) << "]\n"; + std::cout << "NGCFT1: got FT1_REQUEST fk:" << e.file_kind << " [" << bin2hex(e.file_id) << "]\n"; //#endif // .... just rethrow?? @@ -350,7 +356,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_request& e) { bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init& e) { //#if !NDEBUG - std::cout << "NGCFT1: FT1_INIT fk:" << e.file_kind << " fs:" << e.file_size << " tid:" << int(e.transfer_id) << " [" << bin2hex(e.file_id) << "]\n"; + std::cout << "NGCFT1: got FT1_INIT fk:" << e.file_kind << " fs:" << e.file_size << " tid:" << int(e.transfer_id) << " [" << bin2hex(e.file_id) << "]\n"; //#endif bool accept = false; @@ -394,7 +400,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init& e) { bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) { //#if !NDEBUG - std::cout << "NGCFT1: FT1_INIT_ACK mds:" << e.max_lossy_data_size << "\n"; + std::cout << "NGCFT1: got FT1_INIT_ACK mds:" << e.max_lossy_data_size << "\n"; //#endif // we now should start sending data @@ -445,7 +451,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) { bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data& e) { #if !NDEBUG - //std::cout << "NGCFT1: FT1_DATA\n"; + //std::cout << "NGCFT1: got FT1_DATA\n"; #endif if (e.data.empty()) { @@ -519,7 +525,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data& e) { bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data_ack& e) { #if !NDEBUG - //std::cout << "NGCFT1: FT1_DATA_ACK\n"; + //std::cout << "NGCFT1: got FT1_DATA_ACK\n"; #endif if (!groups.count(e.group_number)) { @@ -572,7 +578,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data_ack& e) { } bool NGCFT1::onEvent(const Events::NGCEXT_ft1_message& e) { - std::cout << "NGCFT1: FT1_MESSAGE mid:" << e.message_id << " fk:" << e.file_kind << " [" << bin2hex(e.file_id) << "]\n"; + std::cout << "NGCFT1: got FT1_MESSAGE mid:" << e.message_id << " fk:" << e.file_kind << " [" << bin2hex(e.file_id) << "]\n"; // .... just rethrow?? // TODO: dont diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp index ecca279..9ab220d 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp @@ -243,6 +243,7 @@ float SHA1_NGCFT1::iterate(float delta) { // TODO: do we really need this if we get events? if (it->second.time_since_activity >= 120.f) { std::cerr << "SHA1_NGCFT1 warning: sending tansfer timed out " << "." << int(it->first) << "\n"; + assert(false); it = peer_it->second.erase(it); } else { it++; @@ -295,8 +296,8 @@ float SHA1_NGCFT1::iterate(float delta) { for (auto it = ftchunk_requested.chunks.begin(); it != ftchunk_requested.chunks.end();) { it->second.timer += delta; - // 15sec, TODO: config - if (it->second.timer >= 15.f) { + // TODO: config + if (it->second.timer >= 60.f) { it = ftchunk_requested.chunks.erase(it); } else { _peer_open_requests[it->second.c] += 1; @@ -705,6 +706,9 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) { [combine_ids(e.group_number, e.peer_number)] [transfer_id] .v = SendingTransfer::Info{content.get().data}; + + const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround } else if (e.file_kind == NGCFT1_file_kind::HASH_SHA1_CHUNK) { if (e.file_id_size != 20) { // error @@ -722,6 +726,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) { { // they advertise interest in the content const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround if (addParticipation(c, o)) { // something happend, update chunk picker assert(static_cast(c)); @@ -780,6 +785,9 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) { ); e.accept = true; + + const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround } else if (e.file_kind == NGCFT1_file_kind::HASH_SHA1_CHUNK) { SHA1Digest sha1_chunk_hash {e.file_id, e.file_id_size}; @@ -792,6 +800,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) { { // they have the content (probably, might be fake, should move this to done) const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround if (addParticipation(c, o)) { // something happend, update chunk picker assert(static_cast(c)); @@ -1126,6 +1135,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_message& e) { uint64_t ts = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround const auto self_c = c.get().self; auto* reg_ptr = _rmm.get(c); @@ -1523,7 +1533,13 @@ bool SHA1_NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) { { // FIXME: this does not work, tcm just delteded the relation ship - auto c = _tcm.getContactGroupPeer(group_number, peer_number); + //auto c = _tcm.getContactGroupPeer(group_number, peer_number); + + const auto c_it = _tox_peer_to_contact.find(combine_ids(group_number, peer_number)); + if (c_it == _tox_peer_to_contact.end()) { + return false; + } + auto c = c_it->second; if (!static_cast(c)) { return false; } @@ -1579,6 +1595,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_ft1_have& e) { const size_t num_total_chunks = o.get().chunks.size(); const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround // we might not know yet if (addParticipation(c, o)) { @@ -1663,6 +1680,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_ft1_bitset& e) { } const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround // we might not know yet if (addParticipation(c, o)) { @@ -1721,6 +1739,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_pc1_announce& e) { return false; } + SHA1Digest hash{e.id.data()+sizeof(file_kind), 20}; // if have use hash(-info) for file, add to participants @@ -1734,6 +1753,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCEXT_pc1_announce& e) { // add to participants const auto c = _tcm.getContactGroupPeer(e.group_number, e.peer_number); + _tox_peer_to_contact[combine_ids(e.group_number, e.peer_number)] = c; // workaround auto o = itc_it->second; if (addParticipation(c, o)) { // something happend, update chunk picker diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp index bd94b9f..de46ee1 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.hpp @@ -74,6 +74,10 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public // makes request rotate around open content std::deque _queue_content_want_info; + // workaround missing contact events + // only used to remove participation on peer exit + entt::dense_map _tox_peer_to_contact; + std::atomic_bool _info_builder_dirty {false}; std::mutex _info_builder_queue_mutex; using InfoBuilderEntry = std::function; @@ -86,7 +90,7 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public public: // TODO: config bool _udp_only {false}; - size_t _max_concurrent_in {4}; + size_t _max_concurrent_in {4}; // info only size_t _max_concurrent_out {3*4}; // HACK: allow ideal number for 4 peers public: