Rearranged mutex actions. Now allow calling av actions from callbacks

This commit is contained in:
mannol 2014-05-29 23:57:58 +02:00
parent 20ab02cf4b
commit 08ddce44d8

View File

@ -1038,7 +1038,9 @@ void *handle_timeout ( void *arg )
int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
{
LOGGER_DEBUG("Session: %p Handling 'invite' on call: %s", session, call ? (char *)call->id : "making new");
pthread_mutex_lock(&session->mutex);
if ( call ) {
if ( call->peers[0] == msg->friend_id ) {
/* The glare case. A calls B when at the same time
@ -1051,17 +1053,20 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
if ( call_id_bigger (call->id, msg->callid.header_value) == 1 ) { /* Peer has advantage */
terminate_call(session, call);
} else {
pthread_mutex_unlock(&session->mutex);
return 0; /* Wait for ringing from peer */
}
} else {
handle_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
pthread_mutex_unlock(&session->mutex);
return 0;
}
}
if ( !msg->callid.header_value ) {
handle_error ( session, call, error_no_callid, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return 0;
}
@ -1069,6 +1074,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
if ( !new_call ) {
handle_error ( session, call, error_busy, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return 0;
}
@ -1084,19 +1090,25 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
free_message ( _msg_ringing );
invoke_callback(new_call->call_idx, MSI_OnInvite);
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
{
LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id );
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 )
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return -1;
if ( !msg->cryptokey.header_value )
return handle_error ( session, call, error_no_crypto_key, msg->friend_id );
}
if ( !msg->cryptokey.header_value ) {
int rc = handle_error ( session, call, error_no_crypto_key, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return rc;
}
call->state = call_active;
call->key_peer = calloc ( sizeof ( uint8_t ), crypto_box_KEYBYTES );
@ -1108,15 +1120,19 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
flush_peer_type ( call, msg, 0 );
invoke_callback(call->call_idx, MSI_OnStart);
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
{
LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->id);
if ( has_call_error ( session, call, msg ) == 0 )
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
@ -1131,16 +1147,19 @@ int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
*/
terminate_call(session, call);
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
{
LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->id );
if ( has_call_error ( session, call, msg ) == 0 )
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
/* Act as end message */
/*
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
@ -1150,16 +1169,19 @@ int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
invoke_callback(call->call_idx, MSI_OnCancel);
terminate_call ( session, call );
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
{
LOGGER_DEBUG("Session: %p Handling 'end' on call: %s", session, call->id );
if ( has_call_error ( session, call, msg ) == 0 )
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
send_message ( session, call, _msg_ending, msg->friend_id );
@ -1168,33 +1190,44 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
invoke_callback(call->call_idx, MSI_OnEnd);
terminate_call ( session, call );
pthread_mutex_unlock(&session->mutex);
return 1;
}
/********** Response handlers **********/
int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
{
if ( has_call_error ( session, call, msg ) == 0 )
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %s", session, call->id );
call->ringing_timer_id = event.timer_alloc ( handle_timeout, call, call->ringing_tout_ms );
invoke_callback(call->call_idx, MSI_OnRinging);
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
{
if ( has_call_error ( session, call, msg ) == 0 )
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
if ( !msg->cryptokey.header_value ) {
return handle_error ( session, call, error_no_crypto_key, msg->friend_id );
int rc = handle_error ( session, call, error_no_crypto_key, msg->friend_id );
pthread_mutex_unlock(&session->mutex);
return rc;
}
/* Generate local key/nonce to send */
@ -1225,13 +1258,17 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
event.timer_release ( call->ringing_timer_id );
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
{
if ( has_call_error ( session, call, msg ) == 0 )
return 0;
pthread_mutex_lock(&session->mutex);
if ( has_call_error ( session, call, msg ) == 0 ) {
pthread_mutex_unlock(&session->mutex);
return 0;
}
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
/* Stop timer */
@ -1242,12 +1279,16 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
/* Terminate call */
terminate_call ( session, call );
pthread_mutex_unlock(&session->mutex);
return 1;
}
int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
{
pthread_mutex_lock(&session->mutex);
if ( !call ) {
LOGGER_WARNING("Handling 'error' on non-existing call!");
pthread_mutex_unlock(&session->mutex);
return -1;
}
@ -1263,6 +1304,8 @@ int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
invoke_callback(call->call_idx, MSI_OnEnding);
terminate_call ( session, call );
pthread_mutex_unlock(&session->mutex);
return 1;
}
@ -1324,13 +1367,10 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
_msg->friend_id = source;
pthread_mutex_lock(&_session->mutex);
/* Find what call */
MSICall *_call = _msg->callid.header_value ? find_call(_session, _msg->callid.header_value ) : NULL;
/* Now handle message */
if ( _msg->request.header_value ) { /* Handle request */
@ -1403,7 +1443,6 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
free_end:
free_message ( _msg );
pthread_mutex_unlock(&_session->mutex);
}