Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Split msi callback array into 1 member per callback #1985

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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