ft raw packet function refactor
This commit is contained in:
parent
9dadd8d254
commit
213457fccf
130
ngc_ft1.cpp
130
ngc_ft1.cpp
@ -19,16 +19,16 @@ struct NGC_FT1 {
|
||||
struct RecvTransfer {
|
||||
std::vector<uint8_t> file_id;
|
||||
|
||||
//enum class State {
|
||||
//INIT_SENT,
|
||||
//SENDING,
|
||||
//} state;
|
||||
enum class State {
|
||||
INITED, //init acked, but no data received yet (might be dropped)
|
||||
RECV, // receiving data
|
||||
} state;
|
||||
|
||||
// float time_since_last_activity ?
|
||||
size_t file_size {0};
|
||||
size_t file_size_current {0};
|
||||
|
||||
// sequenz array
|
||||
// sequence array
|
||||
// list of last x received seq_ids
|
||||
};
|
||||
std::array<std::optional<RecvTransfer>, 256> recv_transfers;
|
||||
@ -38,15 +38,23 @@ struct NGC_FT1 {
|
||||
std::vector<uint8_t> file_id;
|
||||
|
||||
enum class State {
|
||||
INIT_SENT,
|
||||
SENDING,
|
||||
INIT_SENT, // keep this state until ack or deny or giveup
|
||||
|
||||
SENDING, // we got the ack and are now sending data
|
||||
|
||||
// is this real?
|
||||
FINISHING, // we sent all data but acks still outstanding????
|
||||
|
||||
FINFIN, // we sent the data_fin and are waiting for the data_fin_ack
|
||||
|
||||
// delete
|
||||
} state;
|
||||
|
||||
// float time_since_last_activity ?
|
||||
size_t file_size {0};
|
||||
size_t file_size_current {0};
|
||||
|
||||
// sequenz array
|
||||
// sequence array
|
||||
// list of sent but not acked seq_ids
|
||||
};
|
||||
std::array<std::optional<SendTransfer>, 256> send_transfers;
|
||||
@ -57,6 +65,17 @@ struct NGC_FT1 {
|
||||
std::map<uint32_t, Group> groups;
|
||||
};
|
||||
|
||||
// send pkgs
|
||||
static bool _send_pkg_FT1_REQUEST(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t file_kind, const uint8_t* file_id, size_t file_id_size);
|
||||
static bool _send_pkg_FT1_INIT(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t file_kind, uint64_t file_size, uint8_t transfer_id, const uint8_t* file_id, size_t file_id_size);
|
||||
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);
|
||||
static bool _send_pkg_FT1_DATA_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id);
|
||||
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);
|
||||
@ -124,18 +143,7 @@ void NGC_FT1_send_request_private(
|
||||
|
||||
// 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);
|
||||
_send_pkg_FT1_REQUEST(tox, group_number, peer_number, file_kind, file_id, file_id_size);
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -145,6 +153,7 @@ void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_fi
|
||||
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,
|
||||
@ -185,25 +194,7 @@ bool NGC_FT1_send_init_private(
|
||||
}
|
||||
}
|
||||
|
||||
// - 1 byte packet id
|
||||
// - 1 byte (file_kind)
|
||||
// - 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)
|
||||
|
||||
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);
|
||||
_send_pkg_FT1_INIT(tox, group_number, peer_number, file_kind, file_size, idx, file_id, file_id_size);
|
||||
|
||||
peer.send_transfers[idx] = NGC_FT1::Group::Peer::SendTransfer{
|
||||
std::vector(file_id, file_id+file_id_size),
|
||||
@ -222,6 +213,55 @@ void NGC_FT1_register_callback_recv_init(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_
|
||||
ngc_ext_ctx->ngc_ft1_ctx->cb_init[file_kind] = callback;
|
||||
}
|
||||
|
||||
static bool _send_pkg_FT1_REQUEST(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t file_kind, const uint8_t* file_id, size_t file_id_size) {
|
||||
// - 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
|
||||
return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
|
||||
}
|
||||
|
||||
static bool _send_pkg_FT1_INIT(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t file_kind, uint64_t file_size, uint8_t transfer_id, const uint8_t* file_id, size_t file_id_size) {
|
||||
// - 1 byte packet id
|
||||
// - 1 byte (file_kind)
|
||||
// - 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)
|
||||
|
||||
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(transfer_id);
|
||||
for (size_t i = 0; i < file_id_size; i++) {
|
||||
pkg.push_back(file_id[i]);
|
||||
}
|
||||
|
||||
// lossless
|
||||
return tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
|
||||
}
|
||||
|
||||
static bool _send_pkg_FT1_INIT_ACK(const Tox* tox, uint32_t group_number, uint32_t peer_number, uint8_t transfer_id) {
|
||||
// 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
|
||||
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(
|
||||
@ -259,6 +299,7 @@ static void _handle_FT1_REQUEST(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void _handle_FT1_INIT(
|
||||
Tox* tox,
|
||||
NGC_EXT_CTX* ngc_ext_ctx,
|
||||
@ -316,16 +357,7 @@ static void _handle_FT1_INIT(
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
_send_pkg_FT1_INIT_ACK(tox, group_number, peer_number, transfer_id);
|
||||
fprintf(stderr, "accepted init\n");
|
||||
} else {
|
||||
// TODO deny?
|
||||
|
Reference in New Issue
Block a user