wip more refactoring

This commit is contained in:
Green Sky 2022-10-03 01:15:49 +02:00
parent 9a8bfe36f8
commit 5d6a5d8c01
No known key found for this signature in database
7 changed files with 252 additions and 109 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
.h -> c header (public interface)
.hpp -> c++ header (private interface)
.cpp -> c++ source (private implementation)

71
ngc_ext_common.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "./ngc_ext_common.hpp"
//#include <cstdint>
//#include <cassert>
#include <new>
//#include <map>
//#include <list>
//#include <set>
//#include <optional>
//#include <algorithm>
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;
}
//static void _handle_HS1_REQUEST_LAST_IDS(
//Tox* tox,
//NGC_HS1* ngc_hs1_ctx,
//uint32_t group_number,
//uint32_t peer_number,
//const uint8_t *data,
//size_t length
//);
#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++;
fprintf(stderr, "custom_packet [%s] %lu\n", _pkgid2str(pkg_type), length);
if (pkg_type == INVALID) {
fprintf(stderr, "(invalid)\n");
return;
}
auto handle_fn = ngc_ext_ctx->callbacks[pkg_type];
if (handle_fn == nullptr) {
fprintf(stderr, "!!! no handler for packet\n");
return;
}
handle_fn(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser);
}
#undef _EXT_HAVE

46
ngc_ext_common.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef C_NGC_EXT_COMMON_H
#define C_NGC_EXT_COMMON_H
// this is a c header
#include <tox/tox.h>
#ifdef __cplusplus
extern "C" {
#endif
// copy from tox.h:
#ifndef TOX_DEFINED
#define TOX_DEFINED
typedef struct Tox Tox;
#endif /* TOX_DEFINED */
typedef struct NGC_EXT_CTX NGC_EXT_CTX;
NGC_EXT_CTX* NGC_EXT_new(void);
void NGC_EXT_kill(NGC_EXT_CTX* ngc_ext_ctx);
void NGC_EXT_handle_group_custom_packet(
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
// bool private,
const uint8_t *data,
size_t length
//void *user_data
);
#ifdef __cplusplus
}
#endif
#endif // C_NGC_EXT_COMMON_H

View File

@ -1,5 +1,9 @@
#include <tox/tox.h>
#include "ngc_ext_common.h"
#include "ngc_hs1.h"
#include "ngc_ft1.h"
//#include <cstdint>
//#include <cassert>
//#include <new>
@ -71,7 +75,6 @@ enum _PacketType : uint8_t {
INVALID = 0u,
//TODO: make it possible to go further back
// request last (few) message_ids for a peer
// - peer_key bytes (peer key we want to know ids for)
// - 1 byte (uint8_t count ids, atleast 1)
@ -148,3 +151,21 @@ static const char* _pkgid2str(_PacketType type) {
#undef _EXT_CASE
}
using handle_group_custom_packet_cb = void(*)(
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
struct NGC_EXT_CTX {
std::array<handle_group_custom_packet_cb, 256> callbacks;
NGC_HS1* ngc_hs1_ctx = nullptr;
NGC_FT1* ngc_ft1_ctx = nullptr;
};

61
ngc_ft1.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef C_NGC_FT1_H
#define C_NGC_FT1_H
// this is a c header
#include <tox/tox.h>
#ifdef __cplusplus
extern "C" {
#endif
// copy from tox.h:
#ifndef TOX_DEFINED
#define TOX_DEFINED
typedef struct Tox Tox;
#endif /* TOX_DEFINED */
// ========== struct / typedef ==========
typedef struct NGC_FT1 NGC_FT1;
struct NGC_FT1_options {
int tmp;
};
// ========== init / kill ==========
// (see tox api)
bool NGC_FT1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_FT1_options* options);
void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx);
// ========== iterate ==========
void NGC_FT1_iterate(Tox *tox, NGC_FT1* ngc_hs1_ctx/*, void *user_data*/);
// ========== peer online/offline ==========
//void NGC_FT1_peer_online(Tox* tox, NGC_FT1* ngc_hs1_ctx, uint32_t group_number, uint32_t peer_number, bool online);
// "callback"
void NGC_FT1_handle_group_custom_packet(
Tox* tox,
NGC_FT1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
//void *user_data
);
#ifdef __cplusplus
}
#endif
#endif // C_NGC_FT1_H

View File

@ -87,16 +87,36 @@ bool NGC_HS1::Peer::hear(uint32_t msg_id, uint32_t peer_number) {
return true;
}
NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options) {
NGC_HS1* context = new NGC_HS1;
static void _handle_HS1_REQUEST_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
context->options = *options;
uint32_t group_number,
uint32_t peer_number,
return context;
const uint8_t *data,
size_t length
);
static void _handle_HS1_RESPONSE_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options) {
ngc_ext_ctx->ngc_hs1_ctx = new NGC_HS1;
ngc_ext_ctx->ngc_hs1_ctx->options = *options;
return true;
}
void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx) {
delete ngc_hs1_ctx;
void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx->ngc_hs1_ctx;
}
static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, float time_delta) {
@ -147,7 +167,9 @@ static void _iterate_group(Tox *tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number
assert(ngc_hs1_ctx->history.count(g_id));
}
void NGC_HS1_iterate(Tox *tox, NGC_HS1* ngc_hs1_ctx/*, void *user_data*/) {
void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
assert(ngc_ext_ctx);
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
assert(ngc_hs1_ctx);
//fprintf(stderr, "groups: %u\n", ngc_hs1_ctx->history.size());
@ -173,7 +195,8 @@ void NGC_HS1_iterate(Tox *tox, NGC_HS1* ngc_hs1_ctx/*, void *user_data*/) {
}
}
void NGC_HS1_peer_online(Tox* tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, uint32_t peer_number, bool online) {
void NGC_HS1_peer_online(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, bool online) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
// get group id
_GroupKey g_id{};
{ // TODO: error
@ -204,7 +227,7 @@ void NGC_HS1_peer_online(Tox* tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number,
bool NGC_HS1_shim_group_send_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
@ -221,7 +244,7 @@ bool NGC_HS1_shim_group_send_message(
bool ret = tox_group_send_message(tox, group_number, type, message, length, msg_id_ptr, error);
NGC_HS1_record_own_message(tox, ngc_hs1_ctx, group_number, type, message, length, *msg_id_ptr);
NGC_HS1_record_own_message(tox, ngc_ext_ctx, group_number, type, message, length, *msg_id_ptr);
return ret;
}
@ -230,12 +253,13 @@ bool NGC_HS1_shim_group_send_message(
// record own msg
void NGC_HS1_record_own_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
fprintf(stderr, "record_own_message %08X\n", message_id);
// get group id
_GroupKey g_id{};
@ -257,13 +281,14 @@ void NGC_HS1_record_own_message(
// record others msg
void NGC_HS1_record_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
if (!ngc_hs1_ctx->options.record_others) {
return;
}
@ -284,61 +309,8 @@ void NGC_HS1_record_message(
ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length});
}
static void _handle_HS1_REQUEST_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
static void _handle_HS1_RESPONSE_LAST_IDS(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
#define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; }
void NGC_HS1_handle_group_custom_packet(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
//void *user_data
) {
size_t curser = 0;
_HS1_HAVE(1, return)
_PacketType pkg_type = static_cast<_PacketType>(*(data + curser));
curser++;
fprintf(stderr, "custom_packet [%s] %u\n", _pkgid2str(pkg_type), length);
switch (pkg_type) {
case INVALID:
break;
case HS1_REQUEST_LAST_IDS:
_handle_HS1_REQUEST_LAST_IDS(tox, ngc_hs1_ctx, group_number, peer_number, data+curser, length-curser);
break;
case HS1_RESPONSE_LAST_IDS:
_handle_HS1_RESPONSE_LAST_IDS(tox, ngc_hs1_ctx, group_number, peer_number, data+curser, length-curser);
break;
}
}
static void _handle_HS1_REQUEST_LAST_IDS(
Tox* tox,
@ -466,3 +438,4 @@ static void _handle_HS1_RESPONSE_LAST_IDS(
}
#undef _HS1_HAVE

View File

@ -3,31 +3,18 @@
// this is a c header
// outline:
// i am disgusting
//#include <stdbool.h>
//#include <stddef.h>
//#include <stdint.h>
#include <tox/tox.h>
#include "ngc_ext_common.h"
#ifdef __cplusplus
extern "C" {
#endif
// copy from tox.h:
#ifndef TOX_DEFINED
#define TOX_DEFINED
typedef struct Tox Tox;
#endif /* TOX_DEFINED */
// ========== struct / typedef ==========
typedef struct NGC_HS1 NGC_HS1;
@ -50,15 +37,17 @@ struct NGC_HS1_options {
// ========== init / kill ==========
// (see tox api)
NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options);
void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx);
//NGC_HS1* NGC_HS1_new(const struct NGC_HS1_options* options);
bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options);
//void NGC_HS1_kill(NGC_HS1* ngc_hs1_ctx);
void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx);
// ========== iterate ==========
void NGC_HS1_iterate(Tox *tox, NGC_HS1* ngc_hs1_ctx/*, void *user_data*/);
void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/);
// ========== peer online/offline ==========
void NGC_HS1_peer_online(Tox* tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number, uint32_t peer_number, bool online);
void NGC_HS1_peer_online(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, bool online);
// ========== send ==========
@ -66,7 +55,7 @@ void NGC_HS1_peer_online(Tox* tox, NGC_HS1* ngc_hs1_ctx, uint32_t group_number,
// NGC_HS1_record_own_message()
bool NGC_HS1_shim_group_send_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
@ -80,7 +69,7 @@ bool NGC_HS1_shim_group_send_message(
// record own msg
void NGC_HS1_record_own_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
@ -98,7 +87,7 @@ void NGC_HS1_record_own_message(
// record others msg
void NGC_HS1_record_message(
const Tox *tox,
NGC_HS1* ngc_hs1_ctx,
NGC_EXT_CTX* ngc_ext_ctx,
uint32_t group_number,
uint32_t peer_number,
@ -106,27 +95,6 @@ void NGC_HS1_record_message(
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
);
// ========== receive custom ==========
// "callback"
void NGC_HS1_handle_group_custom_packet(
Tox* tox,
NGC_HS1* ngc_hs1_ctx,
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
//void *user_data
);
// ========== receive request ==========
//void NGC_HS1_custom_packet(
// ========== receive answer ==========
#ifdef __cplusplus
}
#endif