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_ext_common.cpp

103 lines
2.0 KiB
C++
Raw Normal View History

2022-10-03 01:15:49 +02:00
#include "./ngc_ext_common.hpp"
//#include <cstdint>
//#include <cassert>
#include <new>
//#include <map>
//#include <list>
//#include <set>
//#include <optional>
//#include <algorithm>
2022-10-03 03:54:16 +02:00
bool _GroupKey::operator<(const _GroupKey& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] < rhs.data[i]) {
return true;
} else if (data[i] > rhs.data[i]) {
return false;
}
}
return false; // equal
}
bool _GroupKey::operator==(const _GroupKey& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] != rhs.data[i]) {
return false;
}
}
return true;
}
bool _PeerKey::operator<(const _PeerKey& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] < rhs.data[i]) {
return true;
} else if (data[i] > rhs.data[i]) {
return false;
}
}
return false; // equal
}
bool _PeerKey::operator==(const _PeerKey& rhs) const {
for (size_t i = 0; i < data.size(); i++) {
if (data[i] != rhs.data[i]) {
return false;
}
}
return true;
}
2022-10-03 01:15:49 +02:00
NGC_EXT_CTX* NGC_EXT_new(void) {
auto* ctx = new NGC_EXT_CTX;
// TODO: this is likely not necessary
for (auto& it : ctx->callbacks) {
it = nullptr;
}
return ctx;
}
void NGC_EXT_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx;
}
#define _EXT_HAVE(x, error) if ((length - curser) < (x)) { error; }
void NGC_EXT_handle_group_custom_packet(
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
//void *user_data
) {
size_t curser = 0;
_EXT_HAVE(1, return)
_PacketType pkg_type = static_cast<_PacketType>(*(data + curser));
curser++;
2023-01-04 20:25:41 +01:00
fprintf(stderr, "EX: custom_packet [%s] %lu\n", _pkgid2str(pkg_type), length);
2022-10-03 01:15:49 +02:00
if (pkg_type == INVALID) {
fprintf(stderr, "(invalid)\n");
return;
}
auto handle_fn = ngc_ext_ctx->callbacks[pkg_type];
if (handle_fn == nullptr) {
2023-01-04 20:25:41 +01:00
fprintf(stderr, "EX: !!! no handler for packet\n");
2022-10-03 01:15:49 +02:00
return;
}
handle_fn(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser);
}
#undef _EXT_HAVE