diff --git a/src/sha1_ngcft1.cpp b/src/sha1_ngcft1.cpp index c1fc814..36ef2f0 100644 --- a/src/sha1_ngcft1.cpp +++ b/src/sha1_ngcft1.cpp @@ -12,6 +12,7 @@ #include "./hash_utils.hpp" #include +#include namespace Components { @@ -27,6 +28,10 @@ namespace Components { } // Components +uint64_t SHA1_NGCFT1::combineIds(const uint32_t group_number, const uint32_t peer_number) { + return (uint64_t(group_number) << 32) | peer_number; +} + SHA1_NGCFT1::SHA1_NGCFT1( Contact3Registry& cr, RegistryMessageModel& rmm, @@ -74,14 +79,19 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) { assert(msg.all_of()); - // assume we have the info, send init + uint8_t transfer_id {0}; _nft.NGC_FT1_send_init_private( e.group_number, e.peer_number, static_cast(e.file_kind), e.file_id, e.file_id_size, msg.get().data.size(), - nullptr + &transfer_id ); + + _sending_transfers + [combineIds(e.group_number, e.peer_number)] + [transfer_id] + .v = SendingTransfer::Info{msg.get().data}; } return false; @@ -101,7 +111,30 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) { } bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) { - return false; + if (!_sending_transfers.count(combineIds(e.group_number, e.peer_number))) { + return false; + } + + auto& peer = _sending_transfers.at(combineIds(e.group_number, e.peer_number)); + + if (!peer.count(e.transfer_id)) { + return false; + } + + auto& transfer = peer.at(e.transfer_id); + if (std::holds_alternative(transfer.v)) { + auto& info_transfer = std::get(transfer.v); + for (size_t i = 0; i < e.data_size && (i + e.data_offset) < info_transfer.info_data.size(); i++) { + e.data[i] = info_transfer.info_data[i + e.data_offset]; + } + + if (e.data_offset + e.data_size >= info_transfer.info_data.size()) { + // was last read (probably TODO: add transfer destruction event) + peer.erase(e.transfer_id); + } + } + + return true; } bool SHA1_NGCFT1::sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) { diff --git a/src/sha1_ngcft1.hpp b/src/sha1_ngcft1.hpp index 0bb3774..3592c94 100644 --- a/src/sha1_ngcft1.hpp +++ b/src/sha1_ngcft1.hpp @@ -9,6 +9,9 @@ #include "./ngcft1.hpp" #include "./ft1_sha1_info.hpp" +#include "entt/container/dense_map.hpp" + +#include class SHA1_NGCFT1 : public RegistryMessageModelEventI, public NGCFT1EventI { Contact3Registry& _cr; @@ -19,6 +22,20 @@ class SHA1_NGCFT1 : public RegistryMessageModelEventI, public NGCFT1EventI { // limit this to each group? entt::dense_map _info_to_message; + struct SendingTransfer { + struct Info { + // copy of info data + // too large? + std::vector info_data; + }; + + std::variant v; + }; + // key is groupid + peerid + entt::dense_map> _sending_transfers; + + static uint64_t combineIds(const uint32_t group_number, const uint32_t peer_number); + public: SHA1_NGCFT1( Contact3Registry& cr,