add ext have all packet

This commit is contained in:
Green Sky 2024-07-10 15:04:53 +02:00
parent f449cf623d
commit 244bcf132c
No known key found for this signature in database
2 changed files with 82 additions and 0 deletions

View File

@ -368,6 +368,37 @@ bool NGCEXTEventProvider::parse_ft1_bitset(
); );
} }
bool NGCEXTEventProvider::parse_ft1_have_all(
uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size,
bool _private
) {
// can be public
// TODO: warn on public?
Events::NGCEXT_ft1_have_all e;
e.group_number = group_number;
e.peer_number = peer_number;
size_t curser = 0;
// - 4 byte (file_kind)
e.file_kind = 0u;
_DATA_HAVE(sizeof(e.file_kind), std::cerr << "NGCEXT: packet too small, missing file_kind\n"; return false)
for (size_t i = 0; i < sizeof(e.file_kind); i++, curser++) {
e.file_kind |= uint32_t(data[curser]) << (i*8);
}
_DATA_HAVE(1, std::cerr << "NGCEXT: packet too small, missing file_id\n"; return false)
// - X bytes (file_id, differnt sizes)
e.file_id = {data+curser, data+curser+(data_size-curser)};
return dispatch(
NGCEXT_Event::FT1_HAVE_ALL,
e
);
}
bool NGCEXTEventProvider::parse_pc1_announce( bool NGCEXTEventProvider::parse_pc1_announce(
uint32_t group_number, uint32_t peer_number, uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size, const uint8_t* data, size_t data_size,
@ -423,6 +454,8 @@ bool NGCEXTEventProvider::handlePacket(
return parse_ft1_have(group_number, peer_number, data+1, data_size-1, _private); return parse_ft1_have(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::FT1_BITSET: case NGCEXT_Event::FT1_BITSET:
return parse_ft1_bitset(group_number, peer_number, data+1, data_size-1, _private); return parse_ft1_bitset(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::FT1_HAVE_ALL:
return parse_ft1_have_all(group_number, peer_number, data+1, data_size-1, _private);
case NGCEXT_Event::PC1_ANNOUNCE: case NGCEXT_Event::PC1_ANNOUNCE:
return parse_pc1_announce(group_number, peer_number, data+1, data_size-1, _private); return parse_pc1_announce(group_number, peer_number, data+1, data_size-1, _private);
default: default:
@ -647,6 +680,25 @@ bool NGCEXTEventProvider::send_ft1_bitset(
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;
} }
bool NGCEXTEventProvider::send_ft1_have_all(
uint32_t group_number, uint32_t peer_number,
uint32_t file_kind,
const uint8_t* file_id, size_t file_id_size
) {
std::vector<uint8_t> pkg;
pkg.push_back(static_cast<uint8_t>(NGCEXT_Event::FT1_BITSET));
for (size_t i = 0; i < sizeof(file_kind); i++) {
pkg.push_back((file_kind>>(i*8)) & 0xff);
}
for (size_t i = 0; i < file_id_size; i++) {
pkg.push_back(file_id[i]);
}
// lossless
return _t.toxGroupSendCustomPrivatePacket(group_number, peer_number, true, pkg) == TOX_ERR_GROUP_SEND_CUSTOM_PRIVATE_PACKET_OK;
}
static std::vector<uint8_t> build_pc1_announce(const uint8_t* id_data, size_t id_size) { static std::vector<uint8_t> build_pc1_announce(const uint8_t* id_data, size_t id_size) {
// - 1 byte packet id // - 1 byte packet id
// - X bytes (id, differnt sizes) // - X bytes (id, differnt sizes)

View File

@ -162,6 +162,17 @@ namespace Events {
std::vector<uint8_t> chunk_bitset; std::vector<uint8_t> chunk_bitset;
}; };
struct NGCEXT_ft1_have_all {
uint32_t group_number;
uint32_t peer_number;
// - 4 byte (file_kind)
uint32_t file_kind;
// - X bytes (file_kind dependent id, differnt sizes)
std::vector<uint8_t> file_id;
};
struct NGCEXT_pc1_announce { struct NGCEXT_pc1_announce {
uint32_t group_number; uint32_t group_number;
uint32_t peer_number; uint32_t peer_number;
@ -257,6 +268,12 @@ enum class NGCEXT_Event : uint8_t {
// - ] (filled up with zero) // - ] (filled up with zero)
FT1_BITSET, FT1_BITSET,
// announce you have all chunks, for given info
// prefer over have and bitset
// - 4 bytes (file_kind)
// - X bytes (file_kind dependent id, differnt sizes)
FT1_HAVE_ALL,
// TODO: FT1_IDONTHAVE, tell a peer you no longer have said chunk // TODO: FT1_IDONTHAVE, tell a peer you no longer have said chunk
// TODO: FT1_REJECT, tell a peer you wont fulfil the request // TODO: FT1_REJECT, tell a peer you wont fulfil the request
@ -282,6 +299,7 @@ struct NGCEXTEventI {
virtual bool onEvent(const Events::NGCEXT_ft1_message&) { return false; } virtual bool onEvent(const Events::NGCEXT_ft1_message&) { return false; }
virtual bool onEvent(const Events::NGCEXT_ft1_have&) { return false; } virtual bool onEvent(const Events::NGCEXT_ft1_have&) { return false; }
virtual bool onEvent(const Events::NGCEXT_ft1_bitset&) { return false; } virtual bool onEvent(const Events::NGCEXT_ft1_bitset&) { return false; }
virtual bool onEvent(const Events::NGCEXT_ft1_have_all&) { return false; }
virtual bool onEvent(const Events::NGCEXT_pc1_announce&) { return false; } virtual bool onEvent(const Events::NGCEXT_pc1_announce&) { return false; }
}; };
@ -361,6 +379,12 @@ class NGCEXTEventProvider : public ToxEventI, public NGCEXTEventProviderI {
bool _private bool _private
); );
bool parse_ft1_have_all(
uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size,
bool _private
);
bool parse_pc1_announce( bool parse_pc1_announce(
uint32_t group_number, uint32_t peer_number, uint32_t group_number, uint32_t peer_number,
const uint8_t* data, size_t data_size, const uint8_t* data, size_t data_size,
@ -431,6 +455,12 @@ class NGCEXTEventProvider : public ToxEventI, public NGCEXTEventProviderI {
const uint8_t* bitset_data, size_t bitset_size // size is bytes const uint8_t* bitset_data, size_t bitset_size // size is bytes
); );
bool send_ft1_have_all(
uint32_t group_number, uint32_t peer_number,
uint32_t file_kind,
const uint8_t* file_id, size_t file_id_size
);
bool send_pc1_announce( bool send_pc1_announce(
uint32_t group_number, uint32_t peer_number, uint32_t group_number, uint32_t peer_number,
const uint8_t* id_data, size_t id_size const uint8_t* id_data, size_t id_size