midway through refactor, ft done (?)

This commit is contained in:
2023-01-12 22:48:51 +01:00
parent 2031ef94b6
commit 89f9610730
5 changed files with 258 additions and 178 deletions

View File

@@ -1,6 +1,5 @@
#include "./ngc_hs1.hpp"
#include "ngc_ext_common.hpp"
#include "ngc_ft1.h"
#include <cstdint>
@@ -49,28 +48,6 @@ bool NGC_HS1::Peer::hear(uint32_t msg_id, uint32_t peer_number) {
return true;
}
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_recv_request(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
@@ -106,6 +83,30 @@ void _handle_HS1_ft_send_data(
size_t data_offset, uint8_t* data, size_t data_size
);
NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options) {
auto* ngc_hs1_ctx = new NGC_HS1;
return ngc_hs1_ctx;
}
bool NGC_HS1_register_ext(NGC_HS1* ngc_hs1_ctx, NGC_EXT_CTX* ngc_ext_ctx) {
ngc_ext_ctx->callbacks[NGC_EXT::HS1_REQUEST_LAST_IDS] = _handle_HS1_REQUEST_LAST_IDS;
ngc_ext_ctx->callbacks[NGC_EXT::HS1_RESPONSE_LAST_IDS] = _handle_HS1_RESPONSE_LAST_IDS;
ngc_ext_ctx->user_data[NGC_EXT::HS1_REQUEST_LAST_IDS] = ngc_hs1_ctx;
ngc_ext_ctx->user_data[NGC_EXT::HS1_RESPONSE_LAST_IDS] = ngc_hs1_ctx;
return true;
}
bool NGC_HS1_register_ft1(NGC_HS1* ngc_hs1_ctx, NGC_FT1* ngc_ft1_ctx) {
NGC_FT1_register_callback_recv_request(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_recv_request);
NGC_FT1_register_callback_recv_init(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_recv_init);
NGC_FT1_register_callback_recv_data(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_recv_data);
NGC_FT1_register_callback_send_data(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_send_data);
}
#if 0
bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options) {
ngc_ext_ctx->ngc_hs1_ctx = new NGC_HS1;
ngc_ext_ctx->ngc_hs1_ctx->options = *options;
@@ -120,17 +121,24 @@ bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* option
return true;
}
#endif
void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx) {
delete ngc_hs1_ctx;
}
#if 0
void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx->ngc_hs1_ctx;
ngc_ext_ctx->ngc_hs1_ctx = nullptr;
}
#endif
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;
static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, /*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{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -175,7 +183,7 @@ static void _iterate_group(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_nu
// - peer_key bytes (peer key we want to know ids for)
// - 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;
pkg[0] = NGC_EXT::HS1_REQUEST_LAST_IDS;
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
@@ -255,7 +263,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_ext_ctx, g_i, 0.02f);
_iterate_group(tox, ngc_ext_ctx->ngc_hs1_ctx, g_i, 0.02f);
g_c_done++;
} else if (g_err != TOX_ERR_GROUP_IS_CONNECTED_GROUP_NOT_FOUND) {
g_c_done++;
@@ -272,7 +280,7 @@ void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
void NGC_HS1_peer_online(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, bool online) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -281,7 +289,7 @@ void NGC_HS1_peer_online(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_numb
if (online) {
// get peer id
_PeerKey p_id{};
NGC_EXT::PeerKey p_id{};
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
@@ -335,13 +343,13 @@ void NGC_HS1_record_own_message(
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
fprintf(stderr, "HS: record_own_message %08X\n", message_id);
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
_PeerKey p_id{};
NGC_EXT::PeerKey p_id{};
{ // TODO: error
tox_group_self_get_public_key(tox, group_number, p_id.data.data(), nullptr);
}
@@ -375,13 +383,13 @@ void NGC_HS1_record_message(
fprintf(stderr, "HS: record_message %08X\n", message_id);
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
_PeerKey p_id{};
NGC_EXT::PeerKey p_id{};
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
@@ -398,7 +406,7 @@ void _handle_HS1_ft_recv_request(
assert(file_id_size == TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t));
// get peer_key from file_id
_PeerKey peer_key;
NGC_EXT::PeerKey peer_key;
std::copy(file_id, file_id+peer_key.size(), peer_key.data.begin());
// get msg_id from file_id
@@ -410,7 +418,7 @@ void _handle_HS1_ft_recv_request(
fprintf(stderr, "HS: got a ft request for xxx msg_id %08X\n", msg_id);
// get group id
_GroupKey group_id{};
NGC_EXT::GroupKey group_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, group_id.data.data(), nullptr);
}
@@ -472,7 +480,7 @@ bool _handle_HS1_ft_recv_init(
assert(file_id_size == TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t));
// get peer_key from file_id
_PeerKey peer_key;
NGC_EXT::PeerKey peer_key;
std::copy(file_id, file_id+peer_key.size(), peer_key.data.begin());
// get msg_id from file_id
@@ -484,7 +492,7 @@ bool _handle_HS1_ft_recv_init(
// did we ask for this?
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -534,7 +542,7 @@ void _handle_HS1_ft_recv_data(
const uint8_t* data, size_t data_size
) {
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -600,7 +608,7 @@ void _handle_HS1_ft_send_data(
size_t data_offset, uint8_t* data, size_t data_size
) {
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -646,12 +654,13 @@ void _handle_HS1_REQUEST_LAST_IDS(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
size_t curser = 0;
_PeerKey p_key;
NGC_EXT::PeerKey p_key;
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "HS: packet too small, missing pkey\n"); return)
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
@@ -663,7 +672,7 @@ void _handle_HS1_REQUEST_LAST_IDS(
//fprintf(stderr, "HS: got request for last %u ids\n", last_msg_id_count);
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
@@ -693,7 +702,7 @@ void _handle_HS1_REQUEST_LAST_IDS(
size_t packing_curser = 0;
pkg[packing_curser++] = HS1_RESPONSE_LAST_IDS;
pkg[packing_curser++] = NGC_EXT::HS1_RESPONSE_LAST_IDS;
std::copy(p_key.data.begin(), p_key.data.end(), pkg.begin()+packing_curser);
packing_curser += p_key.data.size();
@@ -719,12 +728,13 @@ void _handle_HS1_RESPONSE_LAST_IDS(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
size_t curser = 0;
_PeerKey p_key;
NGC_EXT::PeerKey p_key;
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "HS: packet too small, missing pkey\n"); return)
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
@@ -742,7 +752,7 @@ void _handle_HS1_RESPONSE_LAST_IDS(
}
// get group id
_GroupKey g_id{};
NGC_EXT::GroupKey g_id{};
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}