From bbf000b4b5b0b0e46e364aeb82217618b0124176 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 1 Oct 2022 01:38:42 +0200 Subject: [PATCH] record heard msg ids --- ngc_hs1.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/ngc_hs1.cpp b/ngc_hs1.cpp index 4cb2ab8..1279f4c 100644 --- a/ngc_hs1.cpp +++ b/ngc_hs1.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -95,7 +96,6 @@ const char* _pkgid2str(_PacketType type) { #undef _HS1_CASE } - struct NGC_HS1 { NGC_HS1_options options; @@ -116,6 +116,9 @@ struct NGC_HS1 { std::map dict; std::list order; // ordered list of message ids + // msg_ids we have only heard of, with peer_number of who we heard it from + std::map> heard_of; + // dont start immediatly float time_since_last_request_sent {0.f}; @@ -128,12 +131,34 @@ struct NGC_HS1 { new_msg.type = type; new_msg.text = text; + if (heard_of.count(msg_id)) { + // we got history before we got the message + heard_of.erase(msg_id); + } + fprintf(stderr, "######## last msgs ########\n"); auto rit = order.crbegin(); for (size_t i = 0; i < 10 && rit != order.crend(); i++, rit++) { fprintf(stderr, " %08X - %s\n", *rit, dict.at(*rit).text.c_str()); } } + + // returns if new (from that peer) + bool hear(uint32_t msg_id, uint32_t peer_number) { + if (dict.count(msg_id)) { + // we know + return false; + } + + if (heard_of.count(msg_id) && heard_of.at(msg_id).count(peer_number)) { + // we heard it from that peer before + return false; + } + + heard_of[msg_id].emplace(peer_number); + + return true; + } }; struct Group { @@ -410,7 +435,7 @@ static void _handle_HS_REQUEST_LAST_IDS( _PeerID p_key; _HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return) - + std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin()); curser += p_key.data.size(); @@ -448,16 +473,21 @@ static void _handle_HS_REQUEST_LAST_IDS( std::vector pkg; pkg.resize(1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+sizeof(uint32_t)*message_ids.size()); - pkg[0] = HS_RESPONSE_LAST_IDS; + size_t packing_curser = 0; - std::copy(p_key.data.begin(), p_key.data.end(), pkg.begin()+1); + pkg[packing_curser++] = HS_RESPONSE_LAST_IDS; - pkg[1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE] = message_ids.size(); + std::copy(p_key.data.begin(), p_key.data.end(), pkg.begin()+packing_curser); + packing_curser += p_key.data.size(); + + pkg[packing_curser++] = message_ids.size(); for (size_t i = 0; i < message_ids.size(); i++) { - uint8_t* tmp_ptr = reinterpret_cast(message_ids.data()+i); + const uint8_t* tmp_ptr = reinterpret_cast(message_ids.data()+i); // HACK: little endian - std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+i*sizeof(uint32_t)); + //std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+i*sizeof(uint32_t)); + std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+packing_curser); + packing_curser += sizeof(uint32_t); } tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); @@ -477,7 +507,7 @@ static void _handle_HS_RESPONSE_LAST_IDS( _PeerID p_key; _HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return) - + std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin()); curser += p_key.data.size(); @@ -486,7 +516,34 @@ static void _handle_HS_RESPONSE_LAST_IDS( _HS1_HAVE(1, fprintf(stderr, "packet too small, missing count\n"); return) uint8_t last_msg_id_count = data[curser++]; - fprintf(stderr, "got response with last %u ids\n", last_msg_id_count); + fprintf(stderr, "got response with last %u ids:\n", last_msg_id_count); + + if (last_msg_id_count == 0) { + return; + } + + // get group id + _GroupID g_id{}; + { // TODO: error + tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr); + } + + // get peer + auto& peer = ngc_hs1_ctx->history[g_id].peers[p_key]; + + 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(&msg_id)); + curser += sizeof(uint32_t); + fprintf(stderr, " %08X", msg_id); + + if (peer.hear(msg_id, peer_number)) { + fprintf(stderr, " - NEW"); + } + + fprintf(stderr, "\n"); + } } #undef _HS1_HAVE