wip more refactoring
This commit is contained in:
103
ngc_hs1.cpp
103
ngc_hs1.cpp
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user