more init handle

This commit is contained in:
Green Sky 2022-10-06 22:00:06 +02:00
parent 6d4ee7de42
commit bd0c45f885
No known key found for this signature in database
3 changed files with 67 additions and 4 deletions

View File

@ -42,9 +42,9 @@ enum _PacketType : uint8_t {
// tell the other side you want to start a FT
// TODO: might use id layer instead. with it, it would look similar to friends_ft
// - 1 byte (file_kind)
// - X bytes (file_kind dependent id, differnt sizes)
// - 8 bytes (data size, can be 0 if unknown, BUT files have to be atleast 1 byte)
// - 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)
FT1_INIT,
// acknowlage init (like an accept)

View File

@ -58,7 +58,7 @@ struct NGC_FT1 {
};
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(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);
@ -70,7 +70,7 @@ bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* option
ngc_ext_ctx->ngc_ft1_ctx->options = *options;
ngc_ext_ctx->callbacks[FT1_REQUEST] = _handle_FT1_REQUEST;
ngc_ext_ctx->callbacks[FT1_INIT] = nullptr;
ngc_ext_ctx->callbacks[FT1_INIT] = _handle_FT1_INIT;
ngc_ext_ctx->callbacks[FT1_INIT_ACK] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA] = nullptr;
ngc_ext_ctx->callbacks[FT1_DATA_ACK] = nullptr;
@ -187,7 +187,7 @@ bool NGC_FT1_send_init_private(
// - 1 byte packet id
// - 1 byte (file_kind)
// - 8 bytes (data size) // TODO: do all file kinds have a size at init?
// - 8 bytes (data size)
// - 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)
@ -259,5 +259,67 @@ static void _handle_FT1_REQUEST(
}
}
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 ?
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
} else {
// TODO deny?
}
}
#undef _DATA_HAVE

View File

@ -464,6 +464,7 @@ static bool _handle_HS1_ft_init_message(
const uint8_t transfer_id,
const size_t file_size
) {
fprintf(stderr, "-------hs handle ft init\n");
return false; // deny
}