Skip to content

Commit

Permalink
cleanup: Split msi callback array into 1 member per callback
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Feb 6, 2022
1 parent bd71210 commit f87a6df
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
65 changes: 62 additions & 3 deletions toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static void handle_init(MSICall *call, const MSIMessage *msg);
static void handle_push(MSICall *call, const MSIMessage *msg);
static void handle_pop(MSICall *call, const MSIMessage *msg);
static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object);
static msi_action_cb *get_callback(MSISession *session, MSICallbackID id);


/*
Expand All @@ -85,7 +86,39 @@ void msi_register_callback(MSISession *session, msi_action_cb *callback, MSICall
}

pthread_mutex_lock(session->mutex);
session->callbacks[id] = callback;

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);
}
MSISession *msi_new(Messenger *m)
Expand Down Expand Up @@ -492,11 +525,12 @@ static int send_error(Messenger *m, uint32_t friend_number, MSIError error)
static int invoke_callback(MSICall *call, MSICallbackID cb)
{
assert(call);
msi_action_cb *callback = get_callback(call->session, cb);

if (call->session->callbacks[cb]) {
if (callback != nullptr) {
LOGGER_DEBUG(call->session->messenger->log, "Invoking callback function: %d", cb);

if (call->session->callbacks[cb](call->session->av, call) != 0) {
if (callback(call->session->av, call) != 0) {
LOGGER_WARNING(call->session->messenger->log,
"Callback state handling failed, sending error");
goto FAILURE;
Expand Down Expand Up @@ -857,3 +891,28 @@ static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_

pthread_mutex_unlock(session->mutex);
}

static msi_action_cb *get_callback(MSISession *session, MSICallbackID id)
{
switch (id) {
case MSI_ON_INVITE:
return session->invite_callback;

case MSI_ON_START:
return session->start_callback;

case MSI_ON_END:
return session->end_callback;

case MSI_ON_ERROR:
return session->error_callback;

case MSI_ON_PEERTIMEOUT:
return session->peertimeout_callback;

case MSI_ON_CAPABILITIES:
return session->capabilities_callback;
}

return nullptr;
}
8 changes: 7 additions & 1 deletion toxav/msi.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ typedef struct MSISession {
Messenger *messenger;

pthread_mutex_t mutex[1];
msi_action_cb *callbacks[7];

msi_action_cb *invite_callback;
msi_action_cb *start_callback;
msi_action_cb *end_callback;
msi_action_cb *error_callback;
msi_action_cb *peertimeout_callback;
msi_action_cb *capabilities_callback;
} MSISession;

/**
Expand Down

0 comments on commit f87a6df

Please sign in to comment.