midway through refactor, ft done (?)

This commit is contained in:
Green Sky 2023-01-12 22:48:51 +01:00
parent 2031ef94b6
commit 89f9610730
No known key found for this signature in database
5 changed files with 258 additions and 178 deletions

View File

@ -1,6 +1,6 @@
#include "./ngc_ft1.h"
#include "ngc_ext_common.hpp"
#include "../tox_ngc_ext/ngc_ext.hpp"
#include <vector>
#include <deque>
@ -104,6 +104,10 @@ struct NGC_FT1 {
std::unordered_map<NGC_FT1_file_kind, NGC_FT1_recv_init_cb*> cb_recv_init;
std::unordered_map<NGC_FT1_file_kind, NGC_FT1_recv_data_cb*> cb_recv_data;
std::unordered_map<NGC_FT1_file_kind, NGC_FT1_send_data_cb*> cb_send_data;
std::unordered_map<NGC_FT1_file_kind, void*> ud_recv_request;
std::unordered_map<NGC_FT1_file_kind, void*> ud_recv_init;
std::unordered_map<NGC_FT1_file_kind, void*> ud_recv_data;
std::unordered_map<NGC_FT1_file_kind, void*> ud_send_data;
struct Group {
struct Peer {
@ -167,18 +171,37 @@ static bool _send_pkg_FT1_INIT(const Tox* tox, uint32_t group_number, uint32_t p
static bool _send_pkg_FT1_INIT_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id);
static bool _send_pkg_FT1_DATA(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, uint16_t sequence_id, const uint8_t* data, size_t data_size);
static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const uint16_t* seq_ids, size_t seq_ids_size);
//static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id);
//static bool _send_pkg_FT1_DATA_FIN_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id);
// handle pkgs
static void _handle_FT1_REQUEST(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
static void _handle_FT1_INIT(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
static void _handle_FT1_INIT_ACK(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
static void _handle_FT1_DATA(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
static void _handle_FT1_DATA_ACK(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
//static void _handle_FT1_DATA_FIN(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
//static void _handle_FT1_DATA_FIN_ACK(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length);
static void _handle_FT1_REQUEST(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length, void* user_data);
static void _handle_FT1_INIT(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length, void* user_data);
static void _handle_FT1_INIT_ACK(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length, void* user_data);
static void _handle_FT1_DATA(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length, void* user_data);
static void _handle_FT1_DATA_ACK(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length, void* user_data);
NGC_FT1* NGC_FT1_new(const struct NGC_FT1_options* options) {
NGC_FT1* ngc_ft1_ctx = new NGC_FT1;
ngc_ft1_ctx->options = *options;
return ngc_ft1_ctx;
}
bool NGC_FT1_register_ext(NGC_FT1* ngc_ft1_ctx, NGC_EXT_CTX* ngc_ext_ctx) {
ngc_ext_ctx->callbacks[NGC_EXT::FT1_REQUEST] = _handle_FT1_REQUEST;
ngc_ext_ctx->callbacks[NGC_EXT::FT1_INIT] = _handle_FT1_INIT;
ngc_ext_ctx->callbacks[NGC_EXT::FT1_INIT_ACK] = _handle_FT1_INIT_ACK;
ngc_ext_ctx->callbacks[NGC_EXT::FT1_DATA] = _handle_FT1_DATA;
ngc_ext_ctx->callbacks[NGC_EXT::FT1_DATA_ACK] = _handle_FT1_DATA_ACK;
ngc_ext_ctx->user_data[NGC_EXT::FT1_REQUEST] = ngc_ft1_ctx;
ngc_ext_ctx->user_data[NGC_EXT::FT1_INIT] = ngc_ft1_ctx;
ngc_ext_ctx->user_data[NGC_EXT::FT1_INIT_ACK] = ngc_ft1_ctx;
ngc_ext_ctx->user_data[NGC_EXT::FT1_DATA] = ngc_ft1_ctx;
ngc_ext_ctx->user_data[NGC_EXT::FT1_DATA_ACK] = ngc_ft1_ctx;
return true;
}
#if 0
bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* options) {
ngc_ext_ctx->ngc_ft1_ctx = new NGC_FT1;
ngc_ext_ctx->ngc_ft1_ctx->options = *options;
@ -191,17 +214,24 @@ bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* option
return true;
}
#endif
void NGC_FT1_kill(NGC_FT1* ngc_ft1_ctx) {
delete ngc_ft1_ctx;
}
#if 0
void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx->ngc_ft1_ctx;
ngc_ext_ctx->ngc_ft1_ctx = nullptr;
}
#endif
void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx) {
//void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
assert(ngc_ft1_ctx);
for (auto& [group_number, group] : ngc_ext_ctx->ngc_ft1_ctx->groups) {
for (auto& [group_number, group] : ngc_ft1_ctx->groups) {
for (auto& [peer_number, peer] : group.peers) {
//for (auto& tf_opt : peer.send_transfers) {
for (size_t idx = 0; idx < peer.send_transfers.size(); idx++) {
@ -238,7 +268,7 @@ void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
}
});
assert(ngc_ext_ctx->ngc_ft1_ctx->cb_send_data.count(tf.file_kind));
assert(ngc_ft1_ctx->cb_send_data.count(tf.file_kind));
// if chunks in flight < window size (1 lol)
while (tf.ssb.size() < 1) {
@ -252,7 +282,14 @@ void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
new_data.resize(chunk_size);
ngc_ext_ctx->ngc_ft1_ctx->cb_send_data[tf.file_kind](tox, ngc_ext_ctx, group_number, peer_number, idx, tf.file_size_current, new_data.data(), new_data.size());
ngc_ft1_ctx->cb_send_data[tf.file_kind](
tox,
group_number, peer_number,
idx,
tf.file_size_current,
new_data.data(), new_data.size(),
ngc_ft1_ctx->ud_send_data.count(tf.file_kind) ? ngc_ft1_ctx->ud_send_data.at(tf.file_kind) : nullptr
);
uint16_t seq_id = tf.ssb.add(std::move(new_data));
_send_pkg_FT1_DATA(tox, group_number, peer_number, idx, seq_id, tf.ssb.entries.at(seq_id).data.data(), tf.ssb.entries.at(seq_id).data.size());
@ -279,69 +316,55 @@ void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
}
void NGC_FT1_register_callback_recv_request(
NGC_EXT_CTX* ngc_ext_ctx,
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_request_cb* callback
NGC_FT1_recv_request_cb* callback,
void* user_data
) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
assert(ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_recv_request[file_kind] = callback;
ngc_ft1_ctx->cb_recv_request[file_kind] = callback;
ngc_ft1_ctx->ud_recv_request[file_kind] = user_data;
}
void NGC_FT1_register_callback_recv_init(
NGC_EXT_CTX* ngc_ext_ctx,
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_init_cb* callback
NGC_FT1_recv_init_cb* callback,
void* user_data
) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
assert(ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_recv_init[file_kind] = callback;
ngc_ft1_ctx->cb_recv_init[file_kind] = callback;
ngc_ft1_ctx->ud_recv_init[file_kind] = user_data;
}
void NGC_FT1_register_callback_recv_data(
NGC_EXT_CTX* ngc_ext_ctx,
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_data_cb* callback
NGC_FT1_recv_data_cb* callback,
void* user_data
) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
assert(ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_recv_data[file_kind] = callback;
ngc_ft1_ctx->cb_recv_data[file_kind] = callback;
ngc_ft1_ctx->ud_recv_data[file_kind] = user_data;
}
void NGC_FT1_register_callback_send_data(
NGC_EXT_CTX* ngc_ext_ctx,
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_send_data_cb* callback
NGC_FT1_send_data_cb* callback,
void* user_data
) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
assert(ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_send_data[file_kind] = callback;
}
void NGC_FT1_send_request(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
NGC_FT1_file_kind file_kind,
const uint8_t* file_id,
size_t file_id_size
) {
// just call private for every peer in group?
for (;;) {
uint32_t peer_number = 0;
NGC_FT1_send_request_private(tox, ngc_ext_ctx, group_number, peer_number, file_kind, file_id, file_id_size);
assert(false && "not implemented");
}
ngc_ft1_ctx->cb_send_data[file_kind] = callback;
ngc_ft1_ctx->ud_send_data[file_kind] = user_data;
}
void NGC_FT1_send_request_private(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
Tox *tox, NGC_FT1* ngc_ft1_ctx,
uint32_t group_number,
uint32_t peer_number,
@ -352,8 +375,7 @@ void NGC_FT1_send_request_private(
size_t file_id_size
) {
assert(tox);
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
assert(ngc_ft1_ctx);
// record locally that we sent(or want to send) the request?
@ -361,7 +383,7 @@ void NGC_FT1_send_request_private(
}
bool NGC_FT1_send_init_private(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
Tox *tox, NGC_FT1* ngc_ft1_ctx,
uint32_t group_number, uint32_t peer_number,
NGC_FT1_file_kind file_kind,
const uint8_t* file_id, size_t file_id_size,
@ -376,7 +398,7 @@ bool NGC_FT1_send_init_private(
return false;
}
auto& peer = ngc_ext_ctx->ngc_ft1_ctx->groups[group_number].peers[peer_number];
auto& peer = ngc_ft1_ctx->groups[group_number].peers[peer_number];
// allocate transfer_id
size_t idx = peer.next_send_transfer_idx;
@ -425,7 +447,7 @@ static bool _send_pkg_FT1_REQUEST(const Tox* tox, uint32_t group_number, uint32_
// - 1 byte (TODO: more?) file_kind
// - X bytes file_id
std::vector<uint8_t> pkg;
pkg.push_back(FT1_REQUEST);
pkg.push_back(NGC_EXT::FT1_REQUEST);
pkg.push_back(file_kind);
for (size_t i = 0; i < file_id_size; i++) {
pkg.push_back(file_id[i]);
@ -443,7 +465,7 @@ static bool _send_pkg_FT1_INIT(const Tox* tox, uint32_t group_number, uint32_t p
// - X bytes (file_kind dependent id, differnt sizes)
std::vector<uint8_t> pkg;
pkg.push_back(FT1_INIT);
pkg.push_back(NGC_EXT::FT1_INIT);
pkg.push_back(file_kind);
for (size_t i = 0; i < sizeof(file_size); i++) {
pkg.push_back((file_size>>(i*8)) & 0xff);
@ -462,7 +484,7 @@ static bool _send_pkg_FT1_INIT_ACK(const Tox* tox, uint32_t group_number, uint32
// - 1 byte packet id
// - 1 byte transfer_id
std::vector<uint8_t> pkg;
pkg.push_back(FT1_INIT_ACK);
pkg.push_back(NGC_EXT::FT1_INIT_ACK);
pkg.push_back(transfer_id);
// lossless
@ -476,7 +498,7 @@ static bool _send_pkg_FT1_DATA(const Tox* tox, uint32_t group_number, uint32_t p
// check header_size+data_size <= max pkg size
std::vector<uint8_t> pkg;
pkg.push_back(FT1_DATA);
pkg.push_back(NGC_EXT::FT1_DATA);
pkg.push_back(transfer_id);
pkg.push_back(sequence_id & 0xff);
pkg.push_back((sequence_id >> (1*8)) & 0xff);
@ -492,7 +514,7 @@ static bool _send_pkg_FT1_DATA(const Tox* tox, uint32_t group_number, uint32_t p
static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id, const uint16_t* seq_ids, size_t seq_ids_size) {
std::vector<uint8_t> pkg;
pkg.push_back(FT1_DATA_ACK);
pkg.push_back(NGC_EXT::FT1_DATA_ACK);
pkg.push_back(transfer_id);
// TODO: optimize
@ -505,15 +527,6 @@ static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32
return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
}
//static bool _send_pkg_FT1_DATA_FIN(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) {
//std::vector<uint8_t> pkg;
//pkg.push_back(FT1_DATA_FIN);
//pkg.push_back(transfer_id);
//// lossless?
//return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
//}
#define _DATA_HAVE(x, error) if ((length - curser) < (x)) { error; }
static void _handle_FT1_REQUEST(
@ -524,8 +537,10 @@ static void _handle_FT1_REQUEST(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_FT1* ngc_ft1_ctx = static_cast<NGC_FT1*>(user_data);
size_t curser = 0;
// TODO: might be uint16_t or even larger
@ -541,11 +556,15 @@ static void _handle_FT1_REQUEST(
fprintf(stderr, "]\n");
NGC_FT1_recv_request_cb* fn_ptr = nullptr;
if (ngc_ext_ctx->ngc_ft1_ctx->cb_recv_request.count(file_kind)) {
fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_recv_request.at(file_kind);
if (ngc_ft1_ctx->cb_recv_request.count(file_kind)) {
fn_ptr = ngc_ft1_ctx->cb_recv_request.at(file_kind);
}
void* ud_ptr = nullptr;
if (ngc_ft1_ctx->ud_recv_request.count(file_kind)) {
ud_ptr = ngc_ft1_ctx->ud_recv_request.at(file_kind);
}
if (fn_ptr) {
fn_ptr(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser);
fn_ptr(tox, group_number, peer_number, data+curser, length-curser, ud_ptr);
} else {
fprintf(stderr, "FT: missing cb for requests\n");
}
@ -559,8 +578,10 @@ static void _handle_FT1_INIT(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_FT1* ngc_ft1_ctx = static_cast<NGC_FT1*>(user_data);
size_t curser = 0;
// - 1 byte (file_kind)
@ -596,13 +617,17 @@ static void _handle_FT1_INIT(
// did we allready ack this and the other side just did not see the ack?
NGC_FT1_recv_init_cb* fn_ptr = nullptr;
if (ngc_ext_ctx->ngc_ft1_ctx->cb_recv_init.count(file_kind)) {
fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_recv_init.at(file_kind);
if (ngc_ft1_ctx->cb_recv_init.count(file_kind)) {
fn_ptr = ngc_ft1_ctx->cb_recv_init.at(file_kind);
}
void* ud_ptr = nullptr;
if (ngc_ft1_ctx->ud_recv_init.count(file_kind)) {
ud_ptr = ngc_ft1_ctx->ud_recv_init.at(file_kind);
}
bool accept_ft;
if (fn_ptr) {
// last part of message (file_id) is not yet parsed, just give it to cb
accept_ft = fn_ptr(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser, transfer_id, file_size);
accept_ft = fn_ptr(tox, group_number, peer_number, data+curser, length-curser, transfer_id, file_size, ud_ptr);
} else {
fprintf(stderr, "FT: missing cb for init\n");
accept_ft = false;
@ -611,7 +636,7 @@ static void _handle_FT1_INIT(
if (accept_ft) {
_send_pkg_FT1_INIT_ACK(tox, group_number, peer_number, transfer_id);
fprintf(stderr, "FT: accepted init\n");
auto& peer = ngc_ext_ctx->ngc_ft1_ctx->groups[group_number].peers[peer_number];
auto& peer = ngc_ft1_ctx->groups[group_number].peers[peer_number];
if (peer.recv_transfers[transfer_id].has_value()) {
fprintf(stderr, "FT: overwriting existing recv_transfer %d\n", transfer_id);
}
@ -637,8 +662,10 @@ static void _handle_FT1_INIT_ACK(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_FT1* ngc_ft1_ctx = static_cast<NGC_FT1*>(user_data);
size_t curser = 0;
// - 1 byte (transfer_id)
@ -648,7 +675,7 @@ static void _handle_FT1_INIT_ACK(
// we now should start sending data
auto& groups = ngc_ext_ctx->ngc_ft1_ctx->groups;
auto& groups = ngc_ft1_ctx->groups;
if (!groups.count(group_number)) {
fprintf(stderr, "FT: init_ack for unknown group\n");
return;
@ -680,8 +707,10 @@ static void _handle_FT1_DATA(
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data, size_t length
const uint8_t *data, size_t length,
void* user_data
) {
NGC_FT1* ngc_ft1_ctx = static_cast<NGC_FT1*>(user_data);
size_t curser = 0;
// - 1 byte (transfer_id)
@ -700,7 +729,7 @@ static void _handle_FT1_DATA(
return;
}
auto& groups = ngc_ext_ctx->ngc_ft1_ctx->groups;
auto& groups = ngc_ft1_ctx->groups;
if (!groups.count(group_number)) {
fprintf(stderr, "FT: data for unknown group\n");
return;
@ -718,8 +747,13 @@ static void _handle_FT1_DATA(
transfer.rsb.add(sequence_id, std::vector<uint8_t>(data+curser, data+curser+(length-curser)));
NGC_FT1_recv_data_cb* fn_ptr = nullptr;
if (ngc_ext_ctx->ngc_ft1_ctx->cb_recv_data.count(transfer.file_kind)) {
fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_recv_data.at(transfer.file_kind);
if (ngc_ft1_ctx->cb_recv_data.count(transfer.file_kind)) {
fn_ptr = ngc_ft1_ctx->cb_recv_data.at(transfer.file_kind);
}
void* ud_ptr = nullptr;
if (ngc_ft1_ctx->ud_recv_data.count(transfer.file_kind)) {
ud_ptr = ngc_ft1_ctx->ud_recv_data.at(transfer.file_kind);
}
if (!fn_ptr) {
@ -731,7 +765,7 @@ static void _handle_FT1_DATA(
while (transfer.rsb.canPop()) {
auto data = transfer.rsb.pop();
fn_ptr(tox, ngc_ext_ctx, group_number, peer_number, transfer_id, transfer.file_size_current, data.data(), data.size());
fn_ptr(tox, group_number, peer_number, transfer_id, transfer.file_size_current, data.data(), data.size(), ud_ptr);
transfer.file_size_current += data.size();
}
@ -751,8 +785,10 @@ static void _handle_FT1_DATA_ACK(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
) {
NGC_FT1* ngc_ft1_ctx = static_cast<NGC_FT1*>(user_data);
size_t curser = 0;
// - 1 byte (transfer_id)
@ -760,7 +796,7 @@ static void _handle_FT1_DATA_ACK(
_DATA_HAVE(sizeof(transfer_id), fprintf(stderr, "FT: packet too small, missing transfer_id\n"); return)
transfer_id = data[curser++];
auto& groups = ngc_ext_ctx->ngc_ft1_ctx->groups;
auto& groups = ngc_ft1_ctx->groups;
if (!groups.count(group_number)) {
fprintf(stderr, "FT: data_ack for unknown group\n");
return;

View File

@ -5,7 +5,7 @@
#include <tox/tox.h>
#include "ngc_ext_common.h"
#include "../tox_ngc_ext/ngc_ext.h"
#ifdef __cplusplus
extern "C" {
@ -37,47 +37,56 @@ typedef enum NGC_FT1_file_kind /*: uint8_t*/ {
// id: infohash
TORRENT_V1_METAINFO = 8u,
// id: sha1
TORRENT_V1_CHUNK,
TORRENT_V1_CHUNK, // alias with SHA1_CHUNK?
// id: infohash
TORRENT_V2_METAINFO,
TORRENT_V2_METAINFO, // meta info is kind of more complicated than that <.<
// id: sha256
TORRENT_V2_CHUNK,
} NGC_FT1_file_kind;
// ========== init / kill ==========
// (see tox api)
bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* options);
void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx);
NGC_FT1* NGC_FT1_new(const struct NGC_FT1_options* options);
bool NGC_FT1_register_ext(NGC_FT1* ngc_ft1_ctx, NGC_EXT_CTX* ngc_ext_ctx);
//bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* options);
void NGC_FT1_kill(NGC_FT1* ngc_ft1_ctx);
//void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx);
// ========== iterate ==========
void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/);
void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_ft1_ctx);
//void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/);
// TODO: announce
// ========== request ==========
// TODO: remove?
void NGC_FT1_request(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
NGC_FT1_file_kind file_kind,
const uint8_t* file_id, size_t file_id_size
);
// TODO: public variant?
void NGC_FT1_send_request_private(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
Tox *tox, NGC_FT1* ngc_ft1_ctx,
uint32_t group_number, uint32_t peer_number,
NGC_FT1_file_kind file_kind,
const uint8_t* file_id, size_t file_id_size
);
typedef void NGC_FT1_recv_request_cb(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);
typedef void NGC_FT1_recv_request_cb(
Tox *tox,
uint32_t group_number, uint32_t peer_number,
const uint8_t* file_id, size_t file_id_size,
void* user_data
);
void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_request_cb* callback);
void NGC_FT1_register_callback_recv_request(
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_request_cb* callback,
void* user_data
);
// ========== send/accept ==========
// public does not make sense here
bool NGC_FT1_send_init_private(
Tox *tox, NGC_EXT_CTX* ngc_ext_ctx,
Tox *tox, NGC_FT1* ngc_ft1_ctx,
uint32_t group_number, uint32_t peer_number,
NGC_FT1_file_kind file_kind,
const uint8_t* file_id, size_t file_id_size,
@ -86,40 +95,60 @@ bool NGC_FT1_send_init_private(
);
// return true to accept, false to deny
typedef bool NGC_FT1_recv_init_cb(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);
typedef bool NGC_FT1_recv_init_cb(
Tox *tox,
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,
void* user_data
);
void NGC_FT1_register_callback_recv_init(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_init_cb* callback);
typedef bool NGC_FT1_begin_send(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t transfer_id);
void NGC_FT1_register_callback_recv_init(
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_init_cb* callback,
void* user_data
);
// ========== data ==========
typedef void NGC_FT1_recv_data_cb(
Tox *tox,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
uint8_t transfer_id,
size_t data_offset, const uint8_t* data, size_t data_size
size_t data_offset, const uint8_t* data, size_t data_size,
void* user_data
);
void NGC_FT1_register_callback_recv_data(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_data_cb* callback);
void NGC_FT1_register_callback_recv_data(
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_recv_data_cb* callback,
void* user_data
);
// request to fill data_size bytes into data
typedef void NGC_FT1_send_data_cb(
Tox *tox,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
uint8_t transfer_id,
size_t data_offset, uint8_t* data, size_t data_size
size_t data_offset, uint8_t* data, size_t data_size,
void* user_data
);
void NGC_FT1_register_callback_send_data(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_send_data_cb* callback);
void NGC_FT1_register_callback_send_data(
NGC_FT1* ngc_ft1_ctx,
NGC_FT1_file_kind file_kind,
NGC_FT1_send_data_cb* callback,
void* user_data
);
// ========== peer online/offline ==========

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);
}

View File

@ -9,7 +9,8 @@
#include <tox/tox.h>
#include "ngc_ext_common.h"
#include "../tox_ngc_ext/ngc_ext.h"
#include "./ngc_ft1.h"
#ifdef __cplusplus
extern "C" {
@ -39,10 +40,12 @@ struct NGC_HS1_options {
// ========== init / kill ==========
// (see tox api)
//NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options);
bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options);
//void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx);
void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx);
NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options);
bool NGC_HS1_register_ext(NGC_HS1* ngc_hs1_ctx, NGC_EXT_CTX* ngc_ext_ctx);
bool NGC_HS1_register_ft1(NGC_HS1* ngc_hs1_ctx, NGC_FT1* ngc_ft1_ctx);
//bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options);
void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx);
//void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx);
// ========== iterate ==========
void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/);

View File

@ -2,7 +2,7 @@
#include "./ngc_hs1.h"
#include "ngc_ext_common.hpp"
#include "../tox_ngc_ext/ngc_ext.hpp"
#include "ngc_ft1.h"
#include <cstdint>
@ -51,10 +51,10 @@ struct NGC_HS1 {
};
struct Group {
std::map<_PeerKey, Peer> peers;
std::map<NGC_EXT::PeerKey, Peer> peers;
struct FileTransfers {
_PeerKey msg_peer;
NGC_EXT::PeerKey msg_peer;
uint32_t msg_id;
float time_since_ft_activity {0.f};
std::vector<uint8_t> recv_buffer; // message gets dumped into here
@ -64,13 +64,13 @@ struct NGC_HS1 {
std::map<std::pair<uint32_t, uint8_t>, FileTransfers> transfers;
struct Sending {
_PeerKey msg_peer;
NGC_EXT::PeerKey msg_peer;
uint32_t msg_id;
};
std::map<std::pair<uint32_t, uint8_t>, Sending> sending;
};
std::map<_GroupKey, Group> history;
std::map<NGC_EXT::GroupKey, Group> history;
};
void _handle_HS1_REQUEST_LAST_IDS(
@ -81,7 +81,8 @@ void _handle_HS1_REQUEST_LAST_IDS(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
);
void _handle_HS1_RESPONSE_LAST_IDS(
@ -92,7 +93,8 @@ void _handle_HS1_RESPONSE_LAST_IDS(
uint32_t peer_number,
const uint8_t *data,
size_t length
size_t length,
void* user_data
);
void _handle_HS1_ft_request_message(