extend the protocol to support larger data packets and set it to the new tox constants numbers

This commit is contained in:
Green Sky 2023-10-11 03:00:03 +02:00
parent 27fd9e688b
commit 77f21f01e9
No known key found for this signature in database
4 changed files with 84 additions and 21 deletions

View File

@ -112,6 +112,8 @@ bool NGCEXTEventProvider::parse_ft1_init_ack(
_DATA_HAVE(sizeof(e.transfer_id), std::cerr << "NGCEXT: packet too small, missing transfer_id\n"; return false) _DATA_HAVE(sizeof(e.transfer_id), std::cerr << "NGCEXT: packet too small, missing transfer_id\n"; return false)
e.transfer_id = data[curser++]; e.transfer_id = data[curser++];
e.max_lossy_data_size = 500-4; // -4 and 500 are hardcoded
return dispatch( return dispatch(
NGCEXT_Event::FT1_INIT_ACK, NGCEXT_Event::FT1_INIT_ACK,
e e
@ -224,6 +226,41 @@ bool NGCEXTEventProvider::parse_ft1_message(
); );
} }
bool NGCEXTEventProvider::parse_ft1_init_ack_v2(
uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size,
bool _private
) {
if (!_private) {
std::cerr << "NGCEXT: ft1_init_ack_v2 cant be public\n";
return false;
}
Events::NGCEXT_ft1_init_ack e;
e.group_number = group_number;
e.peer_number = peer_number;
size_t curser = 0;
// - 1 byte (temporary_file_tf_id)
_DATA_HAVE(sizeof(e.transfer_id), std::cerr << "NGCEXT: packet too small, missing transfer_id\n"; return false)
e.transfer_id = data[curser++];
// - 2 byte (max_lossy_data_size)
if ((data_size - curser) >= sizeof(e.max_lossy_data_size)) {
e.max_lossy_data_size = 0;
for (size_t i = 0; i < sizeof(e.max_lossy_data_size); i++, curser++) {
e.max_lossy_data_size |= uint16_t(data[curser]) << (i*8);
}
} else {
e.max_lossy_data_size = 500-4; // default
}
return dispatch(
NGCEXT_Event::FT1_INIT_ACK,
e
);
}
bool NGCEXTEventProvider::handlePacket( bool NGCEXTEventProvider::handlePacket(
const uint32_t group_number, const uint32_t group_number,
const uint32_t peer_number, const uint32_t peer_number,
@ -247,7 +284,8 @@ bool NGCEXTEventProvider::handlePacket(
case NGCEXT_Event::FT1_INIT: case NGCEXT_Event::FT1_INIT:
return parse_ft1_init(group_number, peer_number, data+1, data_size-1, _private); return parse_ft1_init(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::FT1_INIT_ACK: case NGCEXT_Event::FT1_INIT_ACK:
return parse_ft1_init_ack(group_number, peer_number, data+1, data_size-1, _private); //return parse_ft1_init_ack(group_number, peer_number, data+1, data_size-1, _private);
return parse_ft1_init_ack_v2(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::FT1_DATA: case NGCEXT_Event::FT1_DATA:
return parse_ft1_data(group_number, peer_number, data+1, data_size-1, _private); return parse_ft1_data(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::FT1_DATA_ACK: case NGCEXT_Event::FT1_DATA_ACK:

View File

@ -70,8 +70,6 @@ namespace Events {
// - X bytes (file_kind dependent id, differnt sizes) // - X bytes (file_kind dependent id, differnt sizes)
std::vector<uint8_t> file_id; std::vector<uint8_t> file_id;
// TODO: max supported lossy packet size
}; };
struct NGCEXT_ft1_init_ack { struct NGCEXT_ft1_init_ack {
@ -81,7 +79,8 @@ namespace Events {
// - 1 byte (transfer_id) // - 1 byte (transfer_id)
uint8_t transfer_id; uint8_t transfer_id;
// TODO: max supported lossy packet size // - 2 byte (self_max_lossy_data_size)
uint16_t max_lossy_data_size;
}; };
struct NGCEXT_ft1_data { struct NGCEXT_ft1_data {
@ -163,6 +162,7 @@ enum class NGCEXT_Event : uint8_t {
// acknowlage init (like an accept) // acknowlage init (like an accept)
// like tox ft control continue // like tox ft control continue
// - 1 byte (transfer_id) // - 1 byte (transfer_id)
// - 2 byte (self_max_lossy_data_size) (optional since v2)
FT1_INIT_ACK, FT1_INIT_ACK,
// TODO: init deny, speed up non acceptance // TODO: init deny, speed up non acceptance
@ -263,6 +263,12 @@ class NGCEXTEventProvider : public ToxEventI, public NGCEXTEventProviderI {
bool _private bool _private
); );
bool parse_ft1_init_ack_v2(
uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size,
bool _private
);
bool handlePacket( bool handlePacket(
const uint32_t group_number, const uint32_t group_number,
const uint32_t peer_number, const uint32_t peer_number,

View File

@ -71,6 +71,12 @@ bool NGCFT1::sendPKG_FT1_INIT_ACK(
pkg.push_back(static_cast<uint8_t>(NGCEXT_Event::FT1_INIT_ACK)); pkg.push_back(static_cast<uint8_t>(NGCEXT_Event::FT1_INIT_ACK));
pkg.push_back(transfer_id); pkg.push_back(transfer_id);
// - 2 bytes max_lossy_data_size
const uint16_t max_lossy_data_size = _t.toxGroupMaxCustomLossyPacketLength() - 4;
for (size_t i = 0; i < sizeof(uint16_t); i++) {
pkg.push_back((max_lossy_data_size>>(i*8)) & 0xff);
}
// lossless // lossless
return _t.toxGroupSendCustomPrivatePacket(group_number, peer_number, true, pkg) == TOX_ERR_GROUP_SEND_CUSTOM_PRIVATE_PACKET_OK; return _t.toxGroupSendCustomPrivatePacket(group_number, peer_number, true, pkg) == TOX_ERR_GROUP_SEND_CUSTOM_PRIVATE_PACKET_OK;
} }
@ -283,6 +289,7 @@ void NGCFT1::updateSendTransfer(float time_delta, uint32_t group_number, uint32_
} }
void NGCFT1::iteratePeer(float time_delta, uint32_t group_number, uint32_t peer_number, Group::Peer& peer) { void NGCFT1::iteratePeer(float time_delta, uint32_t group_number, uint32_t peer_number, Group::Peer& peer) {
if (peer.cca) {
auto timeouts = peer.cca->getTimeouts(); auto timeouts = peer.cca->getTimeouts();
std::set<CCAI::SeqIDType> timeouts_set{timeouts.cbegin(), timeouts.cend()}; std::set<CCAI::SeqIDType> timeouts_set{timeouts.cbegin(), timeouts.cend()};
@ -302,6 +309,7 @@ void NGCFT1::iteratePeer(float time_delta, uint32_t group_number, uint32_t peer_
updateSendTransfer(time_delta, group_number, peer_number, peer, idx, timeouts_set, can_packet_size); updateSendTransfer(time_delta, group_number, peer_number, peer, idx, timeouts_set, can_packet_size);
} }
} }
}
// TODO: receiving tranfers? // TODO: receiving tranfers?
} }
@ -473,7 +481,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init& e) {
bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) { bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) {
//#if !NDEBUG //#if !NDEBUG
std::cout << "NGCFT1: FT1_INIT_ACK\n"; std::cout << "NGCFT1: FT1_INIT_ACK mds:" << e.max_lossy_data_size << "\n";
//#endif //#endif
// we now should start sending data // we now should start sending data
@ -493,10 +501,18 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) {
using State = Group::Peer::SendTransfer::State; using State = Group::Peer::SendTransfer::State;
if (transfer.state != State::INIT_SENT) { if (transfer.state != State::INIT_SENT) {
std::cerr << "NGCFT1 error: inti_ack but not in INIT_SENT state\n"; std::cerr << "NGCFT1 error: init_ack but not in INIT_SENT state\n";
return true; return true;
} }
// negotiated packet_data_size
const auto negotiated_packet_data_size = std::min<uint32_t>(e.max_lossy_data_size, _t.toxGroupMaxCustomLossyPacketLength()-4);
// TODO: reset cca with new pkg size
if (!peer.cca) {
peer.max_packet_data_size = negotiated_packet_data_size;
peer.cca = std::make_unique<CUBIC>(peer.max_packet_data_size);
}
// iterate will now call NGC_FT1_send_data_cb // iterate will now call NGC_FT1_send_data_cb
transfer.state = State::SENDING; transfer.state = State::SENDING;
transfer.time_since_activity = 0.f; transfer.time_since_activity = 0.f;
@ -698,7 +714,7 @@ bool NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) {
} }
// reset cca // reset cca
peer.cca = std::make_unique<CUBIC>(500-4); // TODO: replace with tox_group_max_custom_lossy_packet_length()-4 peer.cca.reset(); // dont actually reallocate
return false; return false;
} }

View File

@ -2,6 +2,7 @@
// solanaceae port of tox_ngc_ft1 // solanaceae port of tox_ngc_ft1
#include <cstdint>
#include <solanaceae/toxcore/tox_interface.hpp> #include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/toxcore/tox_event_interface.hpp> #include <solanaceae/toxcore/tox_event_interface.hpp>
@ -141,7 +142,9 @@ class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProvider
struct Group { struct Group {
struct Peer { struct Peer {
std::unique_ptr<CCAI> cca = std::make_unique<CUBIC>(500-4); // TODO: replace with tox_group_max_custom_lossy_packet_length()-4 uint32_t max_packet_data_size {500-4};
//std::unique_ptr<CCAI> cca = std::make_unique<CUBIC>(max_packet_data_size); // TODO: replace with tox_group_max_custom_lossy_packet_length()-4
std::unique_ptr<CCAI> cca;
struct RecvTransfer { struct RecvTransfer {
uint32_t file_kind; uint32_t file_kind;