Skip to content

Instantly share code, notes, and snippets.

@zoff99
Created December 12, 2021 13:38
Show Gist options
  • Save zoff99/c12ba45c1807db3025c27ce171dab988 to your computer and use it in GitHub Desktop.
Save zoff99/c12ba45c1807db3025c27ce171dab988 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <tox/tox.h>
#include <sodium.h>
static Tox *tox1 = NULL;
static Tox *tox2 = NULL;
int online1 = 0;
int online2 = 0;
int added_friend = 0;
int stop_all = 0;
int count_sent = 0;
int count_rcv = 0;
int count_ack = 0;
void self_connection_status(Tox *tox, TOX_CONNECTION status, void *userData)
{
if (status == TOX_CONNECTION_NONE) {
printf("Lost connection to the tox network tox=%p\n", tox);
} else {
printf("Connected to the tox network, status: %d tox=%p\n", status, tox);
if (tox == tox1)
{
online1 = 1;
printf("Connected to the tox network, status: %d #1\n", status);
}
else if (tox == tox2)
{
online2 = 1;
printf("Connected to the tox network, status: %d #2\n", status);
}
}
}
void friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data)
{
TOX_ERR_FRIEND_ADD err;
tox_friend_add_norequest(tox, public_key, &err);
if (err != TOX_ERR_FRIEND_ADD_OK) {
printf("Could not add friend, error: %d\n", err);
} else {
printf("Added to our friend list tox=%p\n", tox);
added_friend = 1;
}
}
void friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data)
{
// printf("got receipt num=%d tox=%p\n", message_id, tox);
count_ack++;
}
void friend_message(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data)
{
char dest_msg[length + 1];
dest_msg[length] = '\0';
memcpy(dest_msg, message, length);
// printf("got message=%s tox=%p\n", dest_msg, tox);
count_rcv++;
}
static void *run_tox1(void *arg)
{
Tox *tox = (Tox *)arg;
TOX_ERR_FRIEND_SEND_MESSAGE error;
while (stop_all == 0) {
tox_iterate(tox, NULL);
long long time = tox_iteration_interval(tox) * 1000000L;
nanosleep((const struct timespec[]){{0, time}}, NULL);
if ((online1 == 1) && (online2 == 1) && (added_friend == 1))
{
tox_friend_send_message(tox, 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)"x", 1, &error);
if (error == TOX_ERR_FRIEND_SEND_MESSAGE_OK)
{
count_sent++;
}
}
}
printf("stop iteration tox=%p\n", tox);
pthread_exit(0);
}
static void *run_tox2(void *arg)
{
Tox *tox = (Tox *)arg;
while (stop_all == 0) {
tox_iterate(tox, NULL);
long long time = tox_iteration_interval(tox) * 1000000L;
nanosleep((const struct timespec[]){{0, time}}, NULL);
}
printf("stop iteration tox=%p\n", tox);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
stop_all = 0;
struct Tox_Options options;
tox_options_default(&options);
tox1 = tox_new(&options, NULL);
tox2 = tox_new(&options, NULL);
tox_callback_self_connection_status(tox1, self_connection_status);
tox_callback_friend_request(tox1, friend_request);
tox_callback_friend_message(tox1, friend_message);
tox_callback_friend_read_receipt(tox1, friend_read_receipt);
tox_callback_self_connection_status(tox2, self_connection_status);
tox_callback_friend_request(tox2, friend_request);
tox_callback_friend_message(tox2, friend_message);
tox_callback_friend_read_receipt(tox2, friend_read_receipt);
uint8_t address_bin1[TOX_ADDRESS_SIZE];
tox_self_get_address(tox1, (uint8_t *)address_bin1);
uint8_t address_bin2[TOX_ADDRESS_SIZE];
tox_self_get_address(tox2, (uint8_t *)address_bin2);
const char *key_hex1 = "1C5293AEF2114717547B39DA8EA6F1E331E5E358B35F9B6B5F19317911C5F976";
uint8_t key_bin1[TOX_PUBLIC_KEY_SIZE];
sodium_hex2bin(key_bin1, sizeof(key_bin1), key_hex1, strlen(key_hex1), NULL, NULL, NULL);
tox_add_tcp_relay(tox1, "tox.verdict.gg", 33445, key_bin1, NULL);
const char *key_hex2 = "7A6098B590BDC73F9723FC59F82B3F9085A64D1B213AAF8E610FD351930D052D";
uint8_t key_bin2[TOX_PUBLIC_KEY_SIZE];
sodium_hex2bin(key_bin2, sizeof(key_bin2), key_hex2, strlen(key_hex2), NULL, NULL, NULL);
tox_add_tcp_relay(tox2, "tox2.abilinski.com", 33445, key_bin2, NULL);
pthread_t tox_thread1;
pthread_t tox_thread2;
pthread_create(&tox_thread1, NULL, &run_tox1, tox1);
pthread_create(&tox_thread2, NULL, &run_tox2, tox2);
sleep(15);
tox_friend_add(tox1, address_bin2, (const uint8_t *)"AA", 2, NULL);
sleep(10);
stop_all = 1;
sleep(1);
tox_kill(tox1);
tox_kill(tox2);
printf("msgs sent=%d rcvd=%d acks=%d\n", count_sent, count_rcv, count_ack);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment