From 9dadd8d2542f35294322345b4f2f646469bcd89f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 8 Oct 2022 20:58:10 +0200 Subject: [PATCH] start refactoring again --- ngc_ext_common.hpp | 2 + ngc_hs1.cpp | 63 +++++------------------------- ngc_hs1.hpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 ngc_hs1.hpp diff --git a/ngc_ext_common.hpp b/ngc_ext_common.hpp index d1818f1..e09793d 100644 --- a/ngc_ext_common.hpp +++ b/ngc_ext_common.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include "ngc_ext_common.h" diff --git a/ngc_hs1.cpp b/ngc_hs1.cpp index 4a445ec..0b4d2ca 100644 --- a/ngc_hs1.cpp +++ b/ngc_hs1.cpp @@ -1,4 +1,4 @@ -#include "./ngc_hs1.h" +#include "./ngc_hs1.hpp" #include "ngc_ext_common.hpp" #include "ngc_ft1.h" @@ -12,51 +12,6 @@ #include #include -struct NGC_HS1 { - NGC_HS1_options options; - - // callbacks - //tox_group_message_cb* client_tox_msg_callback; - - - // key - key - key - value store - // group pubkey - peer pubkey - msg_id - message(type + text) - struct Message { - uint32_t msg_id{}; - Tox_Message_Type type{}; - std::string text{}; - }; - - struct Peer { - std::optional id; - 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; - - struct PendingFTRequest { - uint32_t peer_number; // the peer we requested the message from - float time_since_ft_activity {0.f}; - }; - std::map pending; // key msg_id - - // dont start immediatly - float time_since_last_request_sent {0.f}; - - void append(uint32_t msg_id, Tox_Message_Type type, const std::string& text); - - // returns if new (from that peer) - bool hear(uint32_t msg_id, uint32_t peer_number); - }; - - struct Group { - std::map<_PeerKey, Peer> peers; - }; - - std::map<_GroupKey, Group> history; -}; - void NGC_HS1::Peer::append(uint32_t msg_id, Tox_Message_Type type, const std::string& text) { order.push_back(msg_id); @@ -94,7 +49,7 @@ bool NGC_HS1::Peer::hear(uint32_t msg_id, uint32_t peer_number) { return true; } -static void _handle_HS1_REQUEST_LAST_IDS( +void _handle_HS1_REQUEST_LAST_IDS( Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, @@ -105,7 +60,7 @@ static void _handle_HS1_REQUEST_LAST_IDS( size_t length ); -static void _handle_HS1_RESPONSE_LAST_IDS( +void _handle_HS1_RESPONSE_LAST_IDS( Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, @@ -116,14 +71,14 @@ static void _handle_HS1_RESPONSE_LAST_IDS( size_t length ); -static void _handle_HS1_ft_request_message( +void _handle_HS1_ft_request_message( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size ); -static bool _handle_HS1_ft_init_message( +bool _handle_HS1_ft_init_message( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, @@ -395,7 +350,7 @@ void NGC_HS1_record_message( ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length}); } -static void _handle_HS1_ft_request_message( +void _handle_HS1_ft_request_message( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, @@ -456,7 +411,7 @@ static void _handle_HS1_ft_request_message( ); } -static bool _handle_HS1_ft_init_message( +bool _handle_HS1_ft_init_message( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, @@ -513,7 +468,7 @@ static bool _handle_HS1_ft_init_message( #define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; } -static void _handle_HS1_REQUEST_LAST_IDS( +void _handle_HS1_REQUEST_LAST_IDS( Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, @@ -586,7 +541,7 @@ static void _handle_HS1_REQUEST_LAST_IDS( tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); } -static void _handle_HS1_RESPONSE_LAST_IDS( +void _handle_HS1_RESPONSE_LAST_IDS( Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, diff --git a/ngc_hs1.hpp b/ngc_hs1.hpp new file mode 100644 index 0000000..948cccb --- /dev/null +++ b/ngc_hs1.hpp @@ -0,0 +1,96 @@ +#pragma once + +#include "./ngc_hs1.h" + +#include "ngc_ext_common.hpp" +#include "ngc_ft1.h" + +#include +#include +#include +#include +#include + +struct NGC_HS1 { + NGC_HS1_options options; + + // callbacks + //tox_group_message_cb* client_tox_msg_callback; + + + // key - key - key - value store + // group pubkey - peer pubkey - msg_id - message(type + text) + struct Message { + uint32_t msg_id{}; + Tox_Message_Type type{}; + std::string text{}; + }; + + struct Peer { + std::optional id; + 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; + + struct PendingFTRequest { + uint32_t peer_number; // the peer we requested the message from + float time_since_ft_activity {0.f}; + }; + std::map pending; // key msg_id + + // dont start immediatly + float time_since_last_request_sent {0.f}; + + void append(uint32_t msg_id, Tox_Message_Type type, const std::string& text); + + // returns if new (from that peer) + bool hear(uint32_t msg_id, uint32_t peer_number); + }; + + struct Group { + std::map<_PeerKey, Peer> peers; + }; + + std::map<_GroupKey, Group> history; +}; + +void _handle_HS1_REQUEST_LAST_IDS( + Tox* tox, + NGC_EXT_CTX* ngc_ext_ctx, + + uint32_t group_number, + uint32_t peer_number, + + const uint8_t *data, + size_t length +); + +void _handle_HS1_RESPONSE_LAST_IDS( + Tox* tox, + NGC_EXT_CTX* ngc_ext_ctx, + + uint32_t group_number, + uint32_t peer_number, + + const uint8_t *data, + size_t length +); + +void _handle_HS1_ft_request_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size +); + +bool _handle_HS1_ft_init_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size, + const uint8_t transfer_id, + const size_t file_size +); +