cleanup: Split msi callback array into 1 member per callback

This commit is contained in:
Robin Linden 2022-02-06 15:18:55 +01:00
parent bd71210e6f
commit f87a6df938
No known key found for this signature in database
GPG Key ID: 601A604B7E605776
2 changed files with 69 additions and 4 deletions

View File

@ -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_push(MSICall *call, const MSIMessage *msg);
static void handle_pop(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 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);
/* /*
@ -85,7 +86,39 @@ void msi_register_callback(MSISession *session, msi_action_cb *callback, MSICall
} }
pthread_mutex_lock(session->mutex); 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); pthread_mutex_unlock(session->mutex);
} }
MSISession *msi_new(Messenger *m) MSISession *msi_new(Messenger *m)
@ -492,11 +525,12 @@ static int send_error(Messenger *m, uint32_t friend_number, MSIError error)
static int invoke_callback(MSICall *call, MSICallbackID cb) static int invoke_callback(MSICall *call, MSICallbackID cb)
{ {
assert(call); 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); 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, LOGGER_WARNING(call->session->messenger->log,
"Callback state handling failed, sending error"); "Callback state handling failed, sending error");
goto FAILURE; goto FAILURE;
@ -857,3 +891,28 @@ static void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_
pthread_mutex_unlock(session->mutex); 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;
}

View File

@ -101,7 +101,13 @@ typedef struct MSISession {
Messenger *messenger; Messenger *messenger;
pthread_mutex_t mutex[1]; 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; } MSISession;
/** /**