more callbacks, bootstrap

This commit is contained in:
Green Sky 2023-01-14 02:54:56 +01:00
parent 10571ae923
commit 4da9d3f81c
No known key found for this signature in database
5 changed files with 91 additions and 6 deletions

View File

@ -5,6 +5,8 @@
#include <memory>
#include <iostream>
#include <unordered_map>
#include <thread>
#include <chrono>
int main(int argc, char** argv) {
CommandLine cl(argc, argv);
@ -23,6 +25,7 @@ int main(int argc, char** argv) {
while (true) {
client.iterate();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return 0;

View File

@ -31,3 +31,15 @@ void group_invite_cb(Tox*, uint32_t friend_number, const uint8_t *invite_data, s
static_cast<ToxClient*>(user_data)->onToxGroupInvite(friend_number, invite_data, length, std::string_view{reinterpret_cast<const char*>(group_name), group_name_length});
}
void group_peer_join_cb(Tox*, uint32_t group_number, uint32_t peer_id, void *user_data) {
static_cast<ToxClient*>(user_data)->onToxGroupPeerJoin(group_number, peer_id);
}
void group_peer_exit_cb(Tox*, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length, void *user_data) {
static_cast<ToxClient*>(user_data)->onToxGroupPeerExit(group_number, peer_id, exit_type, std::string_view{reinterpret_cast<const char*>(name), name_length}, std::string_view{reinterpret_cast<const char*>(part_message), part_message_length});
}
void group_self_join_cb(Tox*, uint32_t group_number, void *user_data) {
static_cast<ToxClient*>(user_data)->onToxGroupSelfJoin(group_number);
}

View File

@ -32,9 +32,9 @@ void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *messa
void group_custom_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data);
void group_custom_private_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data);
void group_invite_cb(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, void *user_data);
//static void group_peer_join_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, void *user_data);
//static void group_peer_exit_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t length, void *user_data);
//static void group_self_join_cb(Tox *tox, uint32_t group_number, void *user_data);
void group_peer_join_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, void *user_data);
void group_peer_exit_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t length, void *user_data);
void group_self_join_cb(Tox *tox, uint32_t group_number, void *user_data);
//static void group_join_fail_cb(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type, void *user_data);
//static void group_moderation_cb(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type, void *user_data);

View File

@ -2,7 +2,12 @@
#include "./tox_utils.hpp"
#include "./tox_callbacks.hpp"
#include "toxcore/tox.h"
#include "ngc_ext.h"
#include "ngc_ft1.h"
#include <sodium.h>
#include <sodium.h>
#include <vector>
#include <fstream>
@ -20,7 +25,7 @@ ToxClient::ToxClient(const CommandLine& cl) :
// use cl for options
tox_options_set_log_callback(options, log_cb);
tox_options_set_local_discovery_enabled(options, true);
tox_options_set_local_discovery_enabled(options, false);
tox_options_set_udp_enabled(options, true);
tox_options_set_hole_punching_enabled(options, true);
@ -97,11 +102,55 @@ ToxClient::ToxClient(const CommandLine& cl) :
}
tox_self_set_name(_tox, reinterpret_cast<const uint8_t*>(_self_name.data()), _self_name.size(), nullptr);
_ext_ctx = NGC_EXT_new();
NGC_FT1_options ft1_options {};
_ft1_ctx = NGC_FT1_new(&ft1_options);
NGC_FT1_register_ext(_ft1_ctx, _ext_ctx);
// dht bootstrap
{
struct DHT_node {
const char *ip;
uint16_t port;
const char key_hex[TOX_PUBLIC_KEY_SIZE*2 + 1]; // 1 for null terminator
unsigned char key_bin[TOX_PUBLIC_KEY_SIZE];
};
DHT_node nodes[] =
{
// you can change or add your own bs and tcprelays here, ideally closer to you
{"tox.plastiras.org", 443, "8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725", {}}, // LU tha14
{"tox2.plastiras.org", 33445, "B6626D386BE7E3ACA107B46F48A5C4D522D29281750D44A0CBA6A2721E79C951", {}}, // DE tha14
};
for (size_t i = 0; i < sizeof(nodes)/sizeof(DHT_node); i ++) {
sodium_hex2bin(
nodes[i].key_bin, sizeof(nodes[i].key_bin),
nodes[i].key_hex, sizeof(nodes[i].key_hex)-1,
NULL, NULL, NULL
);
tox_bootstrap(_tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL);
// TODO: use extra tcp option to avoid error msgs
// ... this is hardcore
tox_add_tcp_relay(_tox, nodes[i].ip, nodes[i].port, nodes[i].key_bin, NULL);
}
}
_tox_profile_dirty = true;
}
ToxClient::~ToxClient(void) {
NGC_FT1_kill(_ft1_ctx);
NGC_EXT_kill(_ext_ctx);
tox_kill(_tox);
}
void ToxClient::iterate(void) {
tox_iterate(_tox, this);
NGC_FT1_iterate(_tox, _ft1_ctx);
if (_tox_profile_dirty) {
saveToxProfile();
@ -136,9 +185,12 @@ void ToxClient::onToxFriendRequest(const uint8_t* public_key, std::string_view m
}
void ToxClient::onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length) {
// TODO: signal private?
NGC_EXT_handle_group_custom_packet(_tox, _ext_ctx, group_number, peer_id, data, length);
}
void ToxClient::onToxGroupCustomPrivatePacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length) {
NGC_EXT_handle_group_custom_packet(_tox, _ext_ctx, group_number, peer_id, data, length);
}
void ToxClient::onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_data, size_t invite_length, std::string_view group_name) {
@ -148,6 +200,21 @@ void ToxClient::onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_d
_tox_profile_dirty = true;
}
void ToxClient::onToxGroupPeerJoin(uint32_t group_number, uint32_t peer_id) {
std::cout << "TCL group peer join " << group_number << ":" << peer_id << "\n";
_tox_profile_dirty = true;
}
void ToxClient::onToxGroupPeerExit(uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, std::string_view name, std::string_view part_message) {
std::cout << "TCL group peer esit " << group_number << ":" << peer_id << "\n";
_tox_profile_dirty = true;
}
void ToxClient::onToxGroupSelfJoin(uint32_t group_number) {
std::cout << "TCL group self join " << group_number << "\n";
_tox_profile_dirty = true;
}
void ToxClient::saveToxProfile(void) {
if (_tox_profile_path.empty()) {
return;

View File

@ -12,6 +12,7 @@
struct ToxClient {
public:
ToxClient(const CommandLine& cl);
~ToxClient(void);
void iterate(void);
@ -25,7 +26,9 @@ struct ToxClient {
void onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length);
void onToxGroupCustomPrivatePacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length);
void onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_data, size_t invite_length, std::string_view group_name);
void onToxGroupPeerJoin(uint32_t group_number, uint32_t peer_id);
void onToxGroupPeerExit(uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, std::string_view name, std::string_view part_message);
void onToxGroupSelfJoin(uint32_t group_number);
private:
void saveToxProfile(void);