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 3, 2022
1 parent 2cc8daf commit 8a5f32d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 75 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 @@ -217,7 +217,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(dht);
const Broadcast_Info *broadcast = lan_discovery_init(dht);

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 @@ -79,48 +79,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 @@ -194,12 +194,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);
}

0 comments on commit 8a5f32d

Please sign in to comment.