This repository has been archived on 2023-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
NGC_HS1/ngc_ft1.cpp

338 lines
9.9 KiB
C++
Raw Normal View History

#include "./ngc_ft1.h"
#include "ngc_ext_common.hpp"
2022-10-03 23:33:08 +02:00
#include <vector>
2022-10-06 02:24:23 +02:00
#include <unordered_map>
#include <map>
#include <optional>
2022-10-03 23:33:08 +02:00
#include <cassert>
struct NGC_FT1 {
NGC_FT1_options options;
2022-10-06 02:24:23 +02:00
std::unordered_map<NGC_FT1_file_kind, NGC_FT1_recv_request_cb*> cb_request;
std::unordered_map<NGC_FT1_file_kind, NGC_FT1_recv_init_cb*> cb_init;
struct Group {
struct Peer {
struct RecvTransfer {
std::vector<uint8_t> file_id;
//enum class State {
//INIT_SENT,
//SENDING,
//} state;
// float time_since_last_activity ?
size_t file_size {0};
size_t file_size_current {0};
// sequenz array
// list of last x received seq_ids
};
std::array<std::optional<RecvTransfer>, 256> recv_transfers;
size_t next_recv_transfer_idx {0}; // next id will be 0
struct SendTransfer {
std::vector<uint8_t> file_id;
enum class State {
INIT_SENT,
SENDING,
} state;
// float time_since_last_activity ?
size_t file_size {0};
size_t file_size_current {0};
// sequenz array
// list of sent but not acked seq_ids
};
std::array<std::optional<SendTransfer>, 256> send_transfers;
size_t next_send_transfer_idx {0}; // next id will be 0
};
std::map<uint32_t, Peer> peers;
};
std::map<uint32_t, Group> groups;
};
2022-10-03 23:33:08 +02:00
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);
2022-10-06 22:00:06 +02:00
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);
2022-10-03 23:33:08 +02:00
//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);
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;
2022-10-03 23:33:08 +02:00
ngc_ext_ctx->callbacks[FT1_REQUEST] = _handle_FT1_REQUEST;
2022-10-06 22:00:06 +02:00
ngc_ext_ctx->callbacks[FT1_INIT] = _handle_FT1_INIT;
2022-10-03 03:54:16 +02:00
ngc_ext_ctx->callbacks[FT1_INIT_ACK] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA_ACK] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA_FIN] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA_FIN_ACK] = nullptr;
//ngc_ext_ctx->callbacks[HS1_REQUEST_LAST_IDS] = _handle_HS1_REQUEST_LAST_IDS;
//ngc_ext_ctx->callbacks[HS1_RESPONSE_LAST_IDS] = _handle_HS1_RESPONSE_LAST_IDS;
return true;
}
void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx->ngc_ft1_ctx;
ngc_ext_ctx->ngc_ft1_ctx = nullptr;
}
2022-10-03 23:33:08 +02:00
// iterate
2022-10-06 02:24:23 +02:00
void NGC_FT1_send_request(
2022-10-03 23:33:08 +02:00
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;
2022-10-06 02:24:23 +02:00
NGC_FT1_send_request_private(tox, ngc_ext_ctx, group_number, peer_number, file_kind, file_id, file_id_size);
2022-10-03 23:33:08 +02:00
assert(false && "not implemented");
}
}
2022-10-06 02:24:23 +02:00
void NGC_FT1_send_request_private(
2022-10-03 23:33:08 +02:00
Tox *tox, NGC_EXT_CTX* ngc_ext_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
) {
assert(tox);
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
// record locally that we sent(or want to send) the request?
// - 1 byte packet id
// - 1 byte (TODO: more?) file_kind
// - X bytes file_id
std::vector<uint8_t> pkg;
pkg.push_back(FT1_REQUEST);
pkg.push_back(file_kind);
for (size_t i = 0; i < file_id_size; i++) {
pkg.push_back(file_id[i]);
}
// lossless
tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
}
2022-10-06 02:24:23 +02:00
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) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_request[file_kind] = callback;
}
bool NGC_FT1_send_init_private(
Tox *tox, NGC_EXT_CTX* ngc_ext_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,
size_t file_size
) {
//fprintf(stderr, "TODO: init ft for %08X\n", msg_id);
fprintf(stderr, "init ft\n");
if (tox_group_peer_get_connection_status(tox, group_number, peer_number, nullptr) == TOX_CONNECTION_NONE) {
fprintf(stderr, "error: cant init ft, peer offline\n");
return false;
}
auto& peer = ngc_ext_ctx->ngc_ft1_ctx->groups[group_number].peers[peer_number];
// allocate transfer_id
size_t idx = peer.next_send_transfer_idx;
peer.next_send_transfer_idx = (peer.next_send_transfer_idx + 1) % 256;
{ // TODO: extract
size_t i = idx;
bool found = false;
do {
if (!peer.send_transfers[i].has_value()) {
// free slot
idx = i;
found = true;
break;
}
i = (i + 1) % 256;
} while (i != idx);
if (!found) {
fprintf(stderr, "error: cant init ft, no free transfer slot\n");
return false;
}
}
// - 1 byte packet id
// - 1 byte (file_kind)
2022-10-06 22:00:06 +02:00
// - 8 bytes (data size)
2022-10-06 02:24:23 +02:00
// - 1 byte (temporary_file_tf_id, for this peer only, technically just a prefix to distinguish between simultainious fts)
// - X bytes (file_kind dependent id, differnt sizes)
std::vector<uint8_t> pkg;
pkg.push_back(FT1_INIT);
pkg.push_back(file_kind);
for (size_t i = 0; i < sizeof(file_size); i++) {
pkg.push_back((file_size>>i) & 0xff);
}
pkg.push_back(idx);
for (size_t i = 0; i < file_id_size; i++) {
pkg.push_back(file_id[i]);
}
// lossless
tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
peer.send_transfers[idx] = NGC_FT1::Group::Peer::SendTransfer{
std::vector(file_id, file_id+file_id_size),
NGC_FT1::Group::Peer::SendTransfer::State::INIT_SENT,
file_size,
0,
};
return true;
}
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) {
assert(ngc_ext_ctx);
assert(ngc_ext_ctx->ngc_ft1_ctx);
ngc_ext_ctx->ngc_ft1_ctx->cb_init[file_kind] = callback;
}
2022-10-03 23:33:08 +02:00
#define _DATA_HAVE(x, error) if ((length - curser) < (x)) { error; }
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
) {
size_t curser = 0;
// TODO: might be uint16_t or even larger
2022-10-06 02:24:23 +02:00
uint8_t file_kind_u8;
_DATA_HAVE(sizeof(file_kind_u8), fprintf(stderr, "packet too small, missing file_kind\n"); return)
file_kind_u8 = data[curser++];
auto file_kind = static_cast<NGC_FT1_file_kind>(file_kind_u8);
2022-10-03 23:33:08 +02:00
2022-10-06 02:24:23 +02:00
fprintf(stderr, "got FT request with file_kind %u [", file_kind_u8);
for (size_t curser_copy = curser; curser_copy < length; curser_copy++) {
fprintf(stderr, "%02X", data[curser_copy]);
2022-10-03 23:33:08 +02:00
}
fprintf(stderr, "]\n");
2022-10-06 02:24:23 +02:00
NGC_FT1_recv_request_cb* fn_ptr = nullptr;
if (ngc_ext_ctx->ngc_ft1_ctx->cb_request.count(file_kind)) {
fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_request.at(file_kind);
}
if (fn_ptr) {
fn_ptr(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser);
} else {
fprintf(stderr, "missing cb for requests\n");
}
2022-10-03 23:33:08 +02:00
}
2022-10-06 22:00:06 +02:00
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
) {
size_t curser = 0;
// - 1 byte (file_kind)
// TODO: might be uint16_t or even larger
uint8_t file_kind_u8;
_DATA_HAVE(sizeof(file_kind_u8), fprintf(stderr, "packet too small, missing file_kind\n"); return)
file_kind_u8 = data[curser++];
auto file_kind = static_cast<NGC_FT1_file_kind>(file_kind_u8);
// - 8 bytes (data size)
size_t file_size {0u};
_DATA_HAVE(sizeof(file_size), fprintf(stderr, "packet too small, missing file_size\n"); return)
for (size_t i = 0; i < sizeof(file_size); i++, curser++) {
file_size |= size_t(data[curser]) << i;
}
// - 1 byte (temporary_file_tf_id, for this peer only, technically just a prefix to distinguish between simultainious fts)
uint8_t transfer_id;
_DATA_HAVE(sizeof(transfer_id), fprintf(stderr, "packet too small, missing transfer_id\n"); return)
transfer_id = data[curser++];
// - X bytes (file_kind dependent id, differnt sizes)
fprintf(stderr, "got FT init with file_kind:%u file_size:%lu tf_id:%u [", file_kind_u8, file_size, transfer_id);
for (size_t curser_copy = curser; curser_copy < length; curser_copy++) {
fprintf(stderr, "%02X", data[curser_copy]);
}
fprintf(stderr, "]\n");
// check if slot free ?
// did we allready ack this and the other side just did not see the ack?
2022-10-06 22:00:06 +02:00
NGC_FT1_recv_init_cb* fn_ptr = nullptr;
if (ngc_ext_ctx->ngc_ft1_ctx->cb_init.count(file_kind)) {
fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_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);
} else {
fprintf(stderr, "missing cb for init\n");
accept_ft = false;
}
if (accept_ft) {
// send ack
// - 1 byte packet id
// - 1 byte transfer_id
std::vector<uint8_t> pkg;
pkg.push_back(FT1_INIT_ACK);
pkg.push_back(transfer_id);
// lossless
tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
fprintf(stderr, "accepted init\n");
2022-10-06 22:00:06 +02:00
} else {
// TODO deny?
fprintf(stderr, "rejected init\n");
2022-10-06 22:00:06 +02:00
}
}
2022-10-03 23:33:08 +02:00
#undef _DATA_HAVE