From 80d0db546daef88c0bba9cc335af3a2e41606aee Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 15 Aug 2023 18:35:07 +0200 Subject: [PATCH] cleanup transfers if peer disconnects --- src/ngcft1.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/ngcft1.hpp | 1 + src/sha1_ngcft1.cpp | 3 ++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/ngcft1.cpp b/src/ngcft1.cpp index 32aff77..7516628 100644 --- a/src/ngcft1.cpp +++ b/src/ngcft1.cpp @@ -332,6 +332,8 @@ NGCFT1::NGCFT1( _neep.subscribe(this, NGCEXT_Event::FT1_DATA); _neep.subscribe(this, NGCEXT_Event::FT1_DATA_ACK); _neep.subscribe(this, NGCEXT_Event::FT1_MESSAGE); + + _tep.subscribe(this, Tox_Event::TOX_EVENT_GROUP_PEER_EXIT); } void NGCFT1::iterate(float time_delta) { @@ -645,3 +647,60 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_message& e) { ); } +bool NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) { + const auto group_number = tox_event_group_peer_exit_get_group_number(e); + const auto peer_number = tox_event_group_peer_exit_get_peer_id(e); + + // peer disconnected, end all transfers + + if (!groups.count(group_number)) { + return false; + } + + auto& group = groups.at(group_number); + + if (!group.peers.count(peer_number)) { + return false; + } + + auto& peer = group.peers.at(peer_number); + + for (size_t i = 0; i < peer.send_transfers.size(); i++) { + auto& it_opt = peer.send_transfers.at(i); + if (!it_opt.has_value()) { + continue; + } + + std::cout << "NGCFT1: sending " << int(i) << " canceled bc peer offline\n"; + dispatch( + NGCFT1_Event::send_done, + Events::NGCFT1_send_done{ + group_number, peer_number, + static_cast(i), + } + ); + + it_opt.reset(); + } + + for (size_t i = 0; i < peer.recv_transfers.size(); i++) { + auto& it_opt = peer.recv_transfers.at(i); + if (!it_opt.has_value()) { + continue; + } + + std::cout << "NGCFT1: receiving " << int(i) << " canceled bc peer offline\n"; + dispatch( + NGCFT1_Event::recv_done, + Events::NGCFT1_recv_done{ + group_number, peer_number, + static_cast(i), + } + ); + + it_opt.reset(); + } + + return false; +} + diff --git a/src/ngcft1.hpp b/src/ngcft1.hpp index 1cc6cd6..5a2bba6 100644 --- a/src/ngcft1.hpp +++ b/src/ngcft1.hpp @@ -244,6 +244,7 @@ class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProvider bool onEvent(const Events::NGCEXT_ft1_message&) override; protected: + bool onToxEvent(const Tox_Event_Group_Peer_Exit* e) override; //bool onToxEvent(const Tox_Event_Group_Custom_Packet* e) override; //bool onToxEvent(const Tox_Event_Group_Custom_Private_Packet* e) override; }; diff --git a/src/sha1_ngcft1.cpp b/src/sha1_ngcft1.cpp index d4f1082..48fa5a7 100644 --- a/src/sha1_ngcft1.cpp +++ b/src/sha1_ngcft1.cpp @@ -867,6 +867,8 @@ bool SHA1_NGCFT1::sendFilePath(const Contact3 c, std::string_view file_name, std ce.emplace(0u); } + ce.remove(); + // we dont want the info anymore ce.remove(); if (auto it = std::find(_queue_content_want_info.begin(), _queue_content_want_info.end(), ce); it != _queue_content_want_info.end()) { @@ -917,7 +919,6 @@ bool SHA1_NGCFT1::sendFilePath(const Contact3 c, std::string_view file_name, std return true; } - const auto msg_e = reg_ptr->create(); reg_ptr->emplace(msg_e, c); reg_ptr->emplace(msg_e, c_self);