wip send file request
This commit is contained in:
85
ngc_hs1.cpp
85
ngc_hs1.cpp
@@ -1,6 +1,7 @@
|
||||
#include "./ngc_hs1.h"
|
||||
|
||||
#include "ngc_ext_common.hpp"
|
||||
#include "ngc_ft1.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
@@ -34,6 +35,12 @@ struct NGC_HS1 {
|
||||
// msg_ids we have only heard of, with peer_number of who we heard it from
|
||||
std::map<uint32_t, std::set<uint32_t>> heard_of;
|
||||
|
||||
struct PendingFTRequest {
|
||||
uint32_t peer_number; // the peer we requested the message from
|
||||
float time_since_ft_activity {0.f};
|
||||
};
|
||||
std::map<uint32_t, PendingFTRequest> pending;
|
||||
|
||||
// dont start immediatly
|
||||
float time_since_last_request_sent {0.f};
|
||||
|
||||
@@ -124,7 +131,9 @@ void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
|
||||
ngc_ext_ctx->ngc_hs1_ctx = nullptr;
|
||||
}
|
||||
|
||||
static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, float time_delta) {
|
||||
static void _iterate_group(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, float time_delta) {
|
||||
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
|
||||
|
||||
//fprintf(stderr, "g:%u\n", g_i);
|
||||
_GroupKey g_id{};
|
||||
{ // TODO: error
|
||||
@@ -144,13 +153,13 @@ static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number
|
||||
auto& group = ngc_hs1_ctx->history[g_id];
|
||||
|
||||
// for each peer
|
||||
for (auto& [key, peer] : group.peers) {
|
||||
for (auto& [peer_key, peer] : group.peers) {
|
||||
//fprintf(stderr, " p: %X%X%X%X\n", key.data.data()[0], key.data.data()[1], key.data.data()[2], key.data.data()[3]);
|
||||
peer.time_since_last_request_sent += time_delta;
|
||||
if (peer.time_since_last_request_sent > ngc_hs1_ctx->options.query_interval_per_peer) {
|
||||
peer.time_since_last_request_sent = 0.f;
|
||||
|
||||
fprintf(stderr, "requesting ids for %X%X%X%X\n", key.data.data()[0], key.data.data()[1], key.data.data()[2], key.data.data()[3]);
|
||||
fprintf(stderr, "requesting ids for %X%X%X%X\n", peer_key.data.data()[0], peer_key.data.data()[1], peer_key.data.data()[2], peer_key.data.data()[3]);
|
||||
|
||||
// TODO: other way around?
|
||||
// ask everyone if they have newer stuff for this peer
|
||||
@@ -160,11 +169,65 @@ static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number
|
||||
// - 1 byte (uint8_t count ids, atleast 1)
|
||||
std::array<uint8_t, 1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1> pkg;
|
||||
pkg[0] = HS1_REQUEST_LAST_IDS;
|
||||
std::copy(key.data.begin(), key.data.end(), pkg.begin()+1);
|
||||
std::copy(peer_key.data.begin(), peer_key.data.end(), pkg.begin()+1);
|
||||
pkg[1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE] = ngc_hs1_ctx->options.last_msg_ids_count; // request last (up to) 5 msg_ids
|
||||
|
||||
tox_group_send_custom_packet(tox, group_number, true, pkg.data(), pkg.size(), nullptr);
|
||||
}
|
||||
|
||||
// check if pending msg requests have timed out
|
||||
for (auto it = peer.pending.begin(); it != peer.pending.end();) {
|
||||
it->second.time_since_ft_activity += time_delta;
|
||||
if (it->second.time_since_ft_activity >= ngc_hs1_ctx->options.ft_activity_timeout) {
|
||||
// timed out
|
||||
fprintf(stderr, "!!! pending ft request timed out (%08X)\n", it->first);
|
||||
it = peer.pending.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
// request FT for only heard of message_ids
|
||||
size_t request_made_count = 0;
|
||||
for (const auto& [msg_id, remote_peer_numbers] : peer.heard_of) {
|
||||
if (request_made_count >= 2) { // 2 for test
|
||||
// TODO: limit requests per iterate option
|
||||
break;
|
||||
}
|
||||
|
||||
if (peer.pending.count(msg_id)) {
|
||||
continue; // allready requested
|
||||
}
|
||||
|
||||
if (remote_peer_numbers.empty()) {
|
||||
fprintf(stderr, "!!! msg_id we heard of, but no remote peer !!!\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t remote_peer_number = *remote_peer_numbers.begin();
|
||||
|
||||
// craft file id
|
||||
std::array<uint8_t, TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t)> file_id{};
|
||||
{
|
||||
std::copy(peer_key.data.cbegin(), peer_key.data.cend(), file_id.begin());
|
||||
|
||||
// HACK: little endian
|
||||
const uint8_t* tmp_ptr = reinterpret_cast<const uint8_t*>(&msg_id);
|
||||
std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), file_id.begin()+TOX_GROUP_PEER_PUBLIC_KEY_SIZE);
|
||||
}
|
||||
|
||||
// send request
|
||||
NGC_FT1_request_private(
|
||||
tox, ngc_ext_ctx,
|
||||
group_number, remote_peer_number,
|
||||
NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID,
|
||||
file_id.data(), file_id.size()
|
||||
);
|
||||
|
||||
peer.pending[msg_id] = {remote_peer_number, 0.f};
|
||||
|
||||
request_made_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert(ngc_hs1_ctx->history.size() != 0);
|
||||
@@ -174,8 +237,7 @@ static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number
|
||||
|
||||
void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
|
||||
assert(ngc_ext_ctx);
|
||||
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
|
||||
assert(ngc_hs1_ctx);
|
||||
assert(ngc_ext_ctx->ngc_hs1_ctx);
|
||||
|
||||
//fprintf(stderr, "groups: %u\n", ngc_hs1_ctx->history.size());
|
||||
|
||||
@@ -186,7 +248,7 @@ void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
|
||||
if (tox_group_is_connected(tox, g_i, &g_err)) {
|
||||
// valid and connected here
|
||||
// TODO: delta time, or other timers
|
||||
_iterate_group(tox, ngc_hs1_ctx, g_i, 0.02f);
|
||||
_iterate_group(tox, ngc_ext_ctx, g_i, 0.02f);
|
||||
g_c_done++;
|
||||
} else if (g_err != TOX_ERR_GROUP_IS_CONNECTED_GROUP_NOT_FOUND) {
|
||||
g_c_done++;
|
||||
@@ -429,11 +491,17 @@ static void _handle_HS1_RESPONSE_LAST_IDS(
|
||||
// get peer
|
||||
auto& peer = ngc_hs1_ctx->history[g_id].peers[p_key];
|
||||
|
||||
//std::vector<uint32_t> message_ids{};
|
||||
|
||||
for (size_t i = 0; i < last_msg_id_count && curser+sizeof(uint32_t) <= length; i++) {
|
||||
uint32_t msg_id;
|
||||
|
||||
// HACK: little endian
|
||||
std::copy(data+curser, data+curser+sizeof(uint32_t), reinterpret_cast<uint8_t*>(&msg_id));
|
||||
curser += sizeof(uint32_t);
|
||||
|
||||
//message_ids.push_back(msg_id);
|
||||
|
||||
fprintf(stderr, " %08X", msg_id);
|
||||
|
||||
if (peer.hear(msg_id, peer_number)) {
|
||||
@@ -442,6 +510,9 @@ static void _handle_HS1_RESPONSE_LAST_IDS(
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
// TODO: replace, remote crash
|
||||
assert(curser == length);
|
||||
}
|
||||
|
||||
#undef _HS1_HAVE
|
||||
|
||||
Reference in New Issue
Block a user