record heard msg ids
This commit is contained in:
		
							
								
								
									
										71
									
								
								ngc_hs1.cpp
									
									
									
									
									
								
							
							
						
						
									
										71
									
								
								ngc_hs1.cpp
									
									
									
									
									
								
							@@ -5,6 +5,7 @@
 | 
			
		||||
#include <new>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <optional>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
 | 
			
		||||
@@ -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<uint32_t, Message> dict;
 | 
			
		||||
		std::list<uint32_t> order; // ordered list of message ids
 | 
			
		||||
 | 
			
		||||
		// 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;
 | 
			
		||||
 | 
			
		||||
		// 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 {
 | 
			
		||||
@@ -448,16 +473,21 @@ static void _handle_HS_REQUEST_LAST_IDS(
 | 
			
		||||
	std::vector<uint8_t> 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<uint8_t*>(message_ids.data()+i);
 | 
			
		||||
		const uint8_t* tmp_ptr = reinterpret_cast<uint8_t*>(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);
 | 
			
		||||
@@ -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<uint8_t*>(&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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user