Skip to content

Commit

Permalink
cleanup: Add explicit callback setters for MSI callbacks.
Browse files Browse the repository at this point in the history
Instead of a callback ID and a `switch`.
  • Loading branch information
iphydf committed Mar 4, 2022
1 parent c68d823 commit 0447ab1
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 92 deletions.
2 changes: 1 addition & 1 deletion auto_tests/dht_getnodes_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static bool node_crawled(Dht_Node **nodes, size_t num_nodes, const uint8_t *publ
return false;
}

static bool all_nodes_crawled(AutoTox *autotoxes, uint32_t num_toxes, uint8_t **public_key_list)
static bool all_nodes_crawled(const AutoTox *autotoxes, uint32_t num_toxes, uint8_t **public_key_list)
{
for (uint32_t i = 0; i < num_toxes; ++i) {
const State *state = (const State *)autotoxes[i].state;
Expand Down
17 changes: 4 additions & 13 deletions auto_tests/friend_request_spam_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,16 @@ static void test_friend_request(AutoTox *autotoxes)
}

for (uint32_t t = 0; t < 100; ++t) {
bool connected = true;
for (uint32_t i = 0; i < FR_TOX_COUNT; ++i) {
uint32_t size = tox_self_get_friend_list_size(autotoxes[i].tox);
for (uint32_t fn = 0; fn < size; ++fn) {
if (tox_friend_get_connection_status(autotoxes[i].tox, fn, nullptr) == TOX_CONNECTION_NONE) {
connected = false;
}
}
}

if (connected) {
if (all_friends_connected(autotoxes, FR_TOX_COUNT)) {
break;
}

iterate_all_wait(autotoxes, FR_TOX_COUNT, ITERATION_INTERVAL);
}

uint32_t size = tox_self_get_friend_list_size(autotoxes[0].tox);
printf("Tox clients connected took %lu seconds; tox1 has %u friends.\n", (unsigned long)(time(nullptr) - con_time), size);
const size_t size = tox_self_get_friend_list_size(autotoxes[0].tox);
printf("Tox clients connected took %lu seconds; tox1 has %u friends.\n",
(unsigned long)(time(nullptr) - con_time), (unsigned int)size);
}

int main(void)
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ int main(int argc, char *argv[])
int is_waiting_for_dht_connection = 1;

uint64_t last_LANdiscovery = 0;
Broadcast_Info *broadcast = lan_discovery_init();
const Broadcast_Info *broadcast = lan_discovery_init();

while (1) {
mono_time_update(mono_time);
Expand Down
63 changes: 23 additions & 40 deletions toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,31 @@ static msi_action_cb *get_callback(MSISession *session, MSICallbackID id);
* Public functions
*/

void msi_register_callback(MSISession *session, msi_action_cb *callback, MSICallbackID id)
void msi_callback_invite(MSISession *session, msi_action_cb *callback)
{
if (session == nullptr) {
return;
}

pthread_mutex_lock(session->mutex);

switch (id) {
case MSI_ON_INVITE: {
session->invite_callback = callback;
break;
}

case MSI_ON_START: {
session->start_callback = callback;
break;
}

case MSI_ON_END: {
session->end_callback = callback;
break;
}

case MSI_ON_ERROR: {
session->error_callback = callback;
break;
}

case MSI_ON_PEERTIMEOUT: {
session->peertimeout_callback = callback;
break;
}

case MSI_ON_CAPABILITIES: {
session->capabilities_callback = callback;
break;
}
}

pthread_mutex_unlock(session->mutex);
session->invite_callback = callback;
}
void msi_callback_start(MSISession *session, msi_action_cb *callback)
{
session->start_callback = callback;
}
void msi_callback_end(MSISession *session, msi_action_cb *callback)
{
session->end_callback = callback;
}
void msi_callback_error(MSISession *session, msi_action_cb *callback)
{
session->error_callback = callback;
}
void msi_callback_peertimeout(MSISession *session, msi_action_cb *callback)
{
session->peertimeout_callback = callback;
}
void msi_callback_capabilities(MSISession *session, msi_action_cb *callback)
{
session->capabilities_callback = callback;
}

MSISession *msi_new(Messenger *m)
{
if (m == nullptr) {
Expand Down
9 changes: 7 additions & 2 deletions toxav/msi.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ MSISession *msi_new(Messenger *m);
*/
int msi_kill(MSISession *session, const Logger *log);
/**
* Callback setter.
* Callback setters.
*/
void msi_register_callback(MSISession *session, msi_action_cb *callback, MSICallbackID id);
void msi_callback_invite(MSISession *session, msi_action_cb *callback);
void msi_callback_start(MSISession *session, msi_action_cb *callback);
void msi_callback_end(MSISession *session, msi_action_cb *callback);
void msi_callback_error(MSISession *session, msi_action_cb *callback);
void msi_callback_peertimeout(MSISession *session, msi_action_cb *callback);
void msi_callback_capabilities(MSISession *session, msi_action_cb *callback);
/**
* Send invite request to friend_number.
*/
Expand Down
12 changes: 6 additions & 6 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error)
init_decode_time_stats(&av->video_stats);
av->msi->av = av;

msi_register_callback(av->msi, callback_invite, MSI_ON_INVITE);
msi_register_callback(av->msi, callback_start, MSI_ON_START);
msi_register_callback(av->msi, callback_end, MSI_ON_END);
msi_register_callback(av->msi, callback_error, MSI_ON_ERROR);
msi_register_callback(av->msi, callback_error, MSI_ON_PEERTIMEOUT);
msi_register_callback(av->msi, callback_capabilites, MSI_ON_CAPABILITIES);
msi_callback_invite(av->msi, callback_invite);
msi_callback_start(av->msi, callback_start);
msi_callback_end(av->msi, callback_end);
msi_callback_error(av->msi, callback_error);
msi_callback_peertimeout(av->msi, callback_error);
msi_callback_capabilities(av->msi, callback_capabilites);

RETURN:

Expand Down
24 changes: 12 additions & 12 deletions toxav/toxav_old.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
int toxav_add_av_groupchat(Tox *tox, audio_data_cb *audio_callback, void *userdata)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return add_av_groupchat(m->log, tox, m->conferences_object, audio_callback, userdata);
}
Expand All @@ -38,9 +38,9 @@ int toxav_join_av_groupchat(Tox *tox, uint32_t friendnumber, const uint8_t *data
audio_data_cb *audio_callback, void *userdata)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return join_av_groupchat(m->log, tox, m->conferences_object, friendnumber, data, length, audio_callback, userdata);
}
Expand All @@ -62,9 +62,9 @@ int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, u
uint32_t sample_rate)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return group_send_audio(m->conferences_object, groupnumber, pcm, samples, channels, sample_rate);
}
Expand All @@ -87,9 +87,9 @@ int toxav_group_send_audio(Tox *tox, uint32_t groupnumber, const int16_t *pcm, u
int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, audio_data_cb *audio_callback, void *userdata)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return groupchat_enable_av(m->log, tox, m->conferences_object, groupnumber, audio_callback, userdata);
}
Expand All @@ -102,9 +102,9 @@ int toxav_groupchat_enable_av(Tox *tox, uint32_t groupnumber, audio_data_cb *aud
int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return groupchat_disable_av(m->conferences_object, groupnumber);
}
Expand All @@ -114,9 +114,9 @@ int toxav_groupchat_disable_av(Tox *tox, uint32_t groupnumber)
bool toxav_groupchat_av_enabled(Tox *tox, uint32_t groupnumber)
{
// TODO(iphydf): Don't rely on toxcore internals.
Messenger *m;
const Messenger *m;
//!TOKSTYLE-
m = *(Messenger **)tox;
m = *(const Messenger **)tox;
//!TOKSTYLE+
return groupchat_av_enabled(m->conferences_object, groupnumber);
}
2 changes: 1 addition & 1 deletion toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key
if (in_list
|| replace_all(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key, &ipp_copy,
dht->friends_list[i].public_key)) {
DHT_Friend *dht_friend = &dht->friends_list[i];
const DHT_Friend *dht_friend = &dht->friends_list[i];

if (pk_equal(public_key, dht_friend->public_key)) {
friend_foundip = dht_friend;
Expand Down
14 changes: 5 additions & 9 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ bool ip_is_lan(const IP *ip)
}


bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk, uint16_t port)
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk, uint16_t port)
{
if (broadcast == nullptr) {
return false;
Expand All @@ -356,20 +356,16 @@ bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, c
if (net_family_is_ipv6(net_family(net))) {
ip_port.ip = broadcast_ip(net_family_ipv6, net_family_ipv6);

if (ip_isset(&ip_port.ip)) {
if (sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = true;
}
if (ip_isset(&ip_port.ip) && sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = true;
}
}

/* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is IPv6 */
ip_port.ip = broadcast_ip(net_family(net), net_family_ipv4);

if (ip_isset(&ip_port.ip)) {
if (sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = true;
}
if (ip_isset(&ip_port.ip) && sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = true;
}

return res;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct Broadcast_Info Broadcast_Info;
* @return true on success, false on failure.
*/
non_null()
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk, uint16_t port);
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk, uint16_t port);

/**
* Discovers broadcast devices and IP addresses.
Expand Down
6 changes: 2 additions & 4 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ static int realloc_friendlist(Messenger *m, uint32_t num)
int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk)
{
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status > 0) {
if (pk_equal(real_pk, m->friendlist[i].real_pk)) {
return i;
}
if (m->friendlist[i].status > 0 && pk_equal(real_pk, m->friendlist[i].real_pk)) {
return i;
}
}

Expand Down
4 changes: 2 additions & 2 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static bool connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip
* return 0 on failure.
*/
non_null()
static int proxy_http_generate_connection_request(const Logger *logger, TCP_Client_Connection *tcp_conn)
static int proxy_http_generate_connection_request(TCP_Client_Connection *tcp_conn)
{
char one[] = "CONNECT ";
char two[] = " HTTP/1.1\nHost: ";
Expand Down Expand Up @@ -569,7 +569,7 @@ TCP_Client_Connection *new_TCP_connection(const Logger *logger, const Mono_Time
switch (proxy_info->proxy_type) {
case TCP_PROXY_HTTP: {
temp->status = TCP_CLIENT_PROXY_HTTP_CONNECTING;
proxy_http_generate_connection_request(logger, temp);
proxy_http_generate_connection_request(temp);
break;
}

Expand Down

0 comments on commit 0447ab1

Please sign in to comment.