combined id refactor

This commit is contained in:
Green Sky 2024-07-02 16:09:59 +02:00
parent 92b3d1a5fb
commit 3fd6183c21
No known key found for this signature in database
6 changed files with 63 additions and 50 deletions

View File

@ -51,6 +51,8 @@ add_library(solanaceae_sha1_ngcft1
./solanaceae/ngc_ft1_sha1/hash_utils.hpp ./solanaceae/ngc_ft1_sha1/hash_utils.hpp
./solanaceae/ngc_ft1_sha1/hash_utils.cpp ./solanaceae/ngc_ft1_sha1/hash_utils.cpp
./solanaceae/ngc_ft1_sha1/util.hpp
./solanaceae/ngc_ft1_sha1/ft1_sha1_info.hpp ./solanaceae/ngc_ft1_sha1/ft1_sha1_info.hpp
./solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp ./solanaceae/ngc_ft1_sha1/ft1_sha1_info.cpp

View File

@ -27,22 +27,22 @@ void ReceivingTransfers::tick(float delta) {
} }
} }
ReceivingTransfers::Entry& ReceivingTransfers::emplaceInfo(uint64_t combined_id, uint8_t transfer_id, const Entry::Info& info) { ReceivingTransfers::Entry& ReceivingTransfers::emplaceInfo(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const Entry::Info& info) {
auto& ent = _data[combined_id][transfer_id]; auto& ent = _data[combine_ids(group_number, peer_number)][transfer_id];
ent.v = info; ent.v = info;
return ent; return ent;
} }
ReceivingTransfers::Entry& ReceivingTransfers::emplaceChunk(uint64_t combined_id, uint8_t transfer_id, const Entry::Chunk& chunk) { ReceivingTransfers::Entry& ReceivingTransfers::emplaceChunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const Entry::Chunk& chunk) {
assert(!chunk.chunk_indices.empty()); assert(!chunk.chunk_indices.empty());
assert(!containsPeerChunk(combined_id, chunk.content, chunk.chunk_indices.front())); assert(!containsPeerChunk(group_number, peer_number, chunk.content, chunk.chunk_indices.front()));
auto& ent = _data[combined_id][transfer_id]; auto& ent = _data[combine_ids(group_number, peer_number)][transfer_id];
ent.v = chunk; ent.v = chunk;
return ent; return ent;
} }
bool ReceivingTransfers::containsPeerTransfer(uint64_t combined_id, uint8_t transfer_id) const { bool ReceivingTransfers::containsPeerTransfer(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) const {
auto it = _data.find(combined_id); auto it = _data.find(combine_ids(group_number, peer_number));
if (it == _data.end()) { if (it == _data.end()) {
return false; return false;
} }
@ -73,8 +73,8 @@ bool ReceivingTransfers::containsChunk(ObjectHandle o, size_t chunk_idx) const {
return false; return false;
} }
bool ReceivingTransfers::containsPeerChunk(uint64_t combined_id, ObjectHandle o, size_t chunk_idx) const { bool ReceivingTransfers::containsPeerChunk(uint32_t group_number, uint32_t peer_number, ObjectHandle o, size_t chunk_idx) const {
auto it = _data.find(combined_id); auto it = _data.find(combine_ids(group_number, peer_number));
if (it == _data.end()) { if (it == _data.end()) {
return false; return false;
} }
@ -99,12 +99,12 @@ bool ReceivingTransfers::containsPeerChunk(uint64_t combined_id, ObjectHandle o,
return false; return false;
} }
void ReceivingTransfers::removePeer(uint64_t combined_id) { void ReceivingTransfers::removePeer(uint32_t group_number, uint32_t peer_number) {
_data.erase(combined_id); _data.erase(combine_ids(group_number, peer_number));
} }
void ReceivingTransfers::removePeerTransfer(uint64_t combined_id, uint8_t transfer_id) { void ReceivingTransfers::removePeerTransfer(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) {
auto it = _data.find(combined_id); auto it = _data.find(combine_ids(group_number, peer_number));
if (it == _data.end()) { if (it == _data.end()) {
return; return;
} }
@ -120,8 +120,8 @@ size_t ReceivingTransfers::size(void) const {
return count; return count;
} }
size_t ReceivingTransfers::sizePeer(uint64_t combined_id) const { size_t ReceivingTransfers::sizePeer(uint32_t group_number, uint32_t peer_number) const {
auto it = _data.find(combined_id); auto it = _data.find(combine_ids(group_number, peer_number));
if (it == _data.end()) { if (it == _data.end()) {
return 0; return 0;
} }

View File

@ -4,6 +4,8 @@
#include <entt/container/dense_map.hpp> #include <entt/container/dense_map.hpp>
#include "./util.hpp"
#include <cstdint> #include <cstdint>
#include <variant> #include <variant>
#include <vector> #include <vector>
@ -44,21 +46,21 @@ struct ReceivingTransfers {
void tick(float delta); void tick(float delta);
Entry& emplaceInfo(uint64_t combined_id, uint8_t transfer_id, const Entry::Info& info); Entry& emplaceInfo(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const Entry::Info& info);
Entry& emplaceChunk(uint64_t combined_id, uint8_t transfer_id, const Entry::Chunk& chunk); Entry& emplaceChunk(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const Entry::Chunk& chunk);
bool containsPeer(uint64_t combined_id) const { return _data.count(combined_id); } bool containsPeer(uint32_t group_number, uint32_t peer_number) const { return _data.count(combine_ids(group_number, peer_number)); }
bool containsPeerTransfer(uint64_t combined_id, uint8_t transfer_id) const; bool containsPeerTransfer(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) const;
bool containsChunk(ObjectHandle o, size_t chunk_idx) const; bool containsChunk(ObjectHandle o, size_t chunk_idx) const;
bool containsPeerChunk(uint64_t combined_id, ObjectHandle o, size_t chunk_idx) const; bool containsPeerChunk(uint32_t group_number, uint32_t peer_number, ObjectHandle o, size_t chunk_idx) const;
auto& getPeer(uint64_t combined_id) { return _data.at(combined_id); } auto& getPeer(uint32_t group_number, uint32_t peer_number) { return _data.at(combine_ids(group_number, peer_number)); }
auto& getTransfer(uint64_t combined_id, uint8_t transfer_id) { return getPeer(combined_id).at(transfer_id); } auto& getTransfer(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) { return getPeer(group_number, peer_number).at(transfer_id); }
void removePeer(uint64_t combined_id); void removePeer(uint32_t group_number, uint32_t peer_number);
void removePeerTransfer(uint64_t combined_id, uint8_t transfer_id); void removePeerTransfer(uint32_t group_number, uint32_t peer_number, uint8_t transfer_id);
size_t size(void) const; size_t size(void) const;
size_t sizePeer(uint64_t combined_id) const; size_t sizePeer(uint32_t group_number, uint32_t peer_number) const;
}; };

View File

@ -7,6 +7,8 @@
#include <solanaceae/message3/components.hpp> #include <solanaceae/message3/components.hpp>
#include <solanaceae/tox_messages/components.hpp> #include <solanaceae/tox_messages/components.hpp>
#include "./util.hpp"
#include "./ft1_sha1_info.hpp" #include "./ft1_sha1_info.hpp"
#include "./hash_utils.hpp" #include "./hash_utils.hpp"
@ -53,8 +55,8 @@ void SHA1_NGCFT1::queueUpRequestChunk(uint32_t group_number, uint32_t peer_numbe
} }
// check for running transfer // check for running transfer
if (_sending_transfers.count(combineIds(group_number, peer_number))) { if (_sending_transfers.count(combine_ids(group_number, peer_number))) {
for (const auto& [_, transfer] : _sending_transfers.at(combineIds(group_number, peer_number))) { for (const auto& [_, transfer] : _sending_transfers.at(combine_ids(group_number, peer_number))) {
if (std::holds_alternative<SendingTransfer::Info>(transfer.v)) { if (std::holds_alternative<SendingTransfer::Info>(transfer.v)) {
// ignore info // ignore info
continue; continue;
@ -82,10 +84,6 @@ void SHA1_NGCFT1::queueUpRequestChunk(uint32_t group_number, uint32_t peer_numbe
_queue_requested_chunk.push_back(std::make_tuple(group_number, peer_number, content, hash, 0.f)); _queue_requested_chunk.push_back(std::make_tuple(group_number, peer_number, content, hash, 0.f));
} }
uint64_t SHA1_NGCFT1::combineIds(const uint32_t group_number, const uint32_t peer_number) {
return (uint64_t(group_number) << 32) | peer_number;
}
void SHA1_NGCFT1::updateMessages(ObjectHandle ce) { void SHA1_NGCFT1::updateMessages(ObjectHandle ce) {
assert(ce.all_of<Components::Messages>()); assert(ce.all_of<Components::Messages>());
@ -322,8 +320,8 @@ void SHA1_NGCFT1::iterate(float delta) {
// check if already sending // check if already sending
bool already_sending_to_this_peer = false; bool already_sending_to_this_peer = false;
if (_sending_transfers.count(combineIds(group_number, peer_number))) { if (_sending_transfers.count(combine_ids(group_number, peer_number))) {
for (const auto& [_2, t] : _sending_transfers.at(combineIds(group_number, peer_number))) { for (const auto& [_2, t] : _sending_transfers.at(combine_ids(group_number, peer_number))) {
if (std::holds_alternative<SendingTransfer::Chunk>(t.v)) { if (std::holds_alternative<SendingTransfer::Chunk>(t.v)) {
const auto& v = std::get<SendingTransfer::Chunk>(t.v); const auto& v = std::get<SendingTransfer::Chunk>(t.v);
if (v.content == ce && v.chunk_index == chunk_idx_vec.front()) { if (v.content == ce && v.chunk_index == chunk_idx_vec.front()) {
@ -347,7 +345,7 @@ void SHA1_NGCFT1::iterate(float delta) {
&transfer_id &transfer_id
)) { )) {
_sending_transfers _sending_transfers
[combineIds(group_number, peer_number)] [combine_ids(group_number, peer_number)]
[transfer_id] // TODO: also save index? [transfer_id] // TODO: also save index?
.v = SendingTransfer::Chunk{ce, chunk_idx_vec.front()}; .v = SendingTransfer::Chunk{ce, chunk_idx_vec.front()};
} }
@ -631,7 +629,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) {
); );
_sending_transfers _sending_transfers
[combineIds(e.group_number, e.peer_number)] [combine_ids(e.group_number, e.peer_number)]
[transfer_id] [transfer_id]
.v = SendingTransfer::Info{content.get<Components::FT1InfoSHA1Data>().data}; .v = SendingTransfer::Info{content.get<Components::FT1InfoSHA1Data>().data};
} else if (e.file_kind == NGCFT1_file_kind::HASH_SHA1_CHUNK) { } else if (e.file_kind == NGCFT1_file_kind::HASH_SHA1_CHUNK) {
@ -699,7 +697,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) {
} }
_receiving_transfers.emplaceInfo( _receiving_transfers.emplaceInfo(
combineIds(e.group_number, e.peer_number), e.group_number, e.peer_number,
e.transfer_id, e.transfer_id,
{ce, std::vector<uint8_t>(e.file_size)} {ce, std::vector<uint8_t>(e.file_size)}
); );
@ -743,7 +741,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) {
assert(e.file_size == info.chunkSize(idx_vec.front())); assert(e.file_size == info.chunkSize(idx_vec.front()));
_receiving_transfers.emplaceChunk( _receiving_transfers.emplaceChunk(
combineIds(e.group_number, e.peer_number), e.group_number, e.peer_number,
e.transfer_id, e.transfer_id,
ReceivingTransfers::Entry::Chunk{ce, idx_vec} ReceivingTransfers::Entry::Chunk{ce, idx_vec}
); );
@ -759,11 +757,11 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_init& e) {
} }
bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) { bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) {
if (!_receiving_transfers.containsPeerTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id)) { if (!_receiving_transfers.containsPeerTransfer(e.group_number, e.peer_number, e.transfer_id)) {
return false; return false;
} }
auto& transfer = _receiving_transfers.getTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id); auto& transfer = _receiving_transfers.getTransfer(e.group_number, e.peer_number, e.transfer_id);
transfer.time_since_activity = 0.f; transfer.time_since_activity = 0.f;
if (transfer.isInfo()) { if (transfer.isInfo()) {
@ -794,11 +792,11 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) {
} }
bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) { bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) {
if (!_sending_transfers.count(combineIds(e.group_number, e.peer_number))) { if (!_sending_transfers.count(combine_ids(e.group_number, e.peer_number))) {
return false; return false;
} }
auto& peer = _sending_transfers.at(combineIds(e.group_number, e.peer_number)); auto& peer = _sending_transfers.at(combine_ids(e.group_number, e.peer_number));
if (!peer.count(e.transfer_id)) { if (!peer.count(e.transfer_id)) {
return false; return false;
@ -846,11 +844,11 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) {
} }
bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_done& e) { bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_done& e) {
if (!_receiving_transfers.containsPeerTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id)) { if (!_receiving_transfers.containsPeerTransfer(e.group_number, e.peer_number, e.transfer_id)) {
return false; return false;
} }
auto& transfer = _receiving_transfers.getTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id); auto& transfer = _receiving_transfers.getTransfer(e.group_number, e.peer_number, e.transfer_id);
if (transfer.isInfo()) { if (transfer.isInfo()) {
auto& info = transfer.getInfo(); auto& info = transfer.getInfo();
@ -858,7 +856,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_done& e) {
if (o.any_of<Components::FT1InfoSHA1, Components::FT1InfoSHA1Data>()) { if (o.any_of<Components::FT1InfoSHA1, Components::FT1InfoSHA1Data>()) {
// we already have the info, discard // we already have the info, discard
_receiving_transfers.removePeerTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id); _receiving_transfers.removePeerTransfer(e.group_number, e.peer_number, e.transfer_id);
return true; return true;
} }
@ -869,7 +867,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_done& e) {
if (o.get<Components::FT1InfoSHA1Hash>().hash != hash) { if (o.get<Components::FT1InfoSHA1Hash>().hash != hash) {
std::cerr << "SHA1_NGCFT1 error: got info data mismatching its hash\n"; std::cerr << "SHA1_NGCFT1 error: got info data mismatching its hash\n";
// TODO: requeue info request; eg manipulate o.get<Components::ReRequestInfoTimer>(); // TODO: requeue info request; eg manipulate o.get<Components::ReRequestInfoTimer>();
_receiving_transfers.removePeerTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id); _receiving_transfers.removePeerTransfer(e.group_number, e.peer_number, e.transfer_id);
return true; return true;
} }
@ -997,17 +995,17 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_done& e) {
updateMessages(o); // mostly for received bytes updateMessages(o); // mostly for received bytes
} }
_receiving_transfers.removePeerTransfer(combineIds(e.group_number, e.peer_number), e.transfer_id); _receiving_transfers.removePeerTransfer(e.group_number, e.peer_number, e.transfer_id);
return true; return true;
} }
bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_done& e) { bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_done& e) {
if (!_sending_transfers.count(combineIds(e.group_number, e.peer_number))) { if (!_sending_transfers.count(combine_ids(e.group_number, e.peer_number))) {
return false; return false;
} }
auto& peer_transfers = _sending_transfers.at(combineIds(e.group_number, e.peer_number)); auto& peer_transfers = _sending_transfers.at(combine_ids(e.group_number, e.peer_number));
if (!peer_transfers.count(e.transfer_id)) { if (!peer_transfers.count(e.transfer_id)) {
return false; return false;
} }

View File

@ -80,8 +80,6 @@ class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public
using InfoBuilderEntry = std::function<void(void)>; using InfoBuilderEntry = std::function<void(void)>;
std::list<InfoBuilderEntry> _info_builder_queue; std::list<InfoBuilderEntry> _info_builder_queue;
static uint64_t combineIds(const uint32_t group_number, const uint32_t peer_number);
void updateMessages(ObjectHandle ce); void updateMessages(ObjectHandle ce);
std::optional<std::pair<uint32_t, uint32_t>> selectPeerForRequest(ObjectHandle ce); std::optional<std::pair<uint32_t, uint32_t>> selectPeerForRequest(ObjectHandle ce);

View File

@ -0,0 +1,13 @@
#pragma once
#include <cstdint>
inline static uint64_t combine_ids(const uint32_t group_number, const uint32_t peer_number) {
return (uint64_t(group_number) << 32) | peer_number;
}
inline static void decompose_ids(const uint64_t combined_id, uint32_t& group_number, uint32_t& peer_number) {
group_number = combined_id >> 32;
peer_number = combined_id & 0xffffffff;
}