From 9eaa6d5caeb40b3b7c2c005c68b2a37a0d5938da Mon Sep 17 00:00:00 2001 From: Youness Alaoui Date: Fri, 18 Apr 2014 21:43:40 -0400 Subject: [PATCH] Wrap socket_send in udp-turn to avoid if/else on reliable everywhere --- socket/udp-turn.c | 86 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/socket/udp-turn.c b/socket/udp-turn.c index 36d31c0..942007e 100644 --- a/socket/udp-turn.c +++ b/socket/udp-turn.c @@ -530,6 +530,30 @@ priv_clear_permissions (UdpTurnPriv *priv) priv->permissions = NULL; } +static gint +_socket_send_messages_wrapped (NiceSocket *sock, const NiceAddress *to, + const NiceOutputMessage *messages, guint n_messages, gboolean reliable) +{ + if (reliable) + return nice_socket_send_messages_reliable (sock, to, messages, n_messages); + else + return nice_socket_send_messages (sock, to, messages, n_messages); +} + +static gssize +_socket_send_wrapped (NiceSocket *sock, const NiceAddress *to, + guint len, const gchar *buf, gboolean reliable) +{ + GOutputVector local_buf = { buf, len }; + NiceOutputMessage local_message = { &local_buf, 1}; + gint ret; + + ret = _socket_send_messages_wrapped (sock, to, &local_message, 1, reliable); + if (ret == 1) + return len; + return ret; +} + static void socket_enqueue_data(UdpTurnPriv *priv, const NiceAddress *to, guint len, const gchar *buf, gboolean reliable) @@ -560,12 +584,8 @@ socket_dequeue_all_data (UdpTurnPriv *priv, const NiceAddress *to) (SendData *) g_queue_pop_head(send_queue); nice_debug ("dequeuing data"); - if (data->reliable) - nice_socket_send_reliable (priv->base_socket, &priv->server_addr, - data->data_len, data->data); - else - nice_socket_send (priv->base_socket, &priv->server_addr, data->data_len, - data->data); + _socket_send_wrapped (priv->base_socket, &priv->server_addr, + data->data_len, data->data, data->reliable); g_free (data->data); g_slice_free (SendData, data); @@ -640,12 +660,8 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, goto error; } } else { - if (reliable) - ret = nice_socket_send_messages_reliable (priv->base_socket, - &priv->server_addr, message, 1); - else - ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr, - message, 1); + ret = _socket_send_messages_wrapped (priv->base_socket, + &priv->server_addr, message, 1, reliable); if (ret == 1) return output_message_get_size (message); @@ -743,12 +759,8 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, GOutputVector local_buf = { buffer, msg_len }; NiceOutputMessage local_message = {&local_buf, 1}; - if (reliable) - ret = nice_socket_send_messages_reliable (priv->base_socket, - &priv->server_addr, &local_message, 1); - else - ret = nice_socket_send_messages (priv->base_socket, &priv->server_addr, - &local_message, 1); + ret = _socket_send_messages_wrapped (priv->base_socket, + &priv->server_addr, &local_message, 1, reliable); if (ret == 1) return msg_len; @@ -757,10 +769,8 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to, } /* Error condition pass through to the base socket. */ - if (reliable) - ret = nice_socket_send_messages_reliable (priv->base_socket, to, message, 1); - else - ret = nice_socket_send_messages (priv->base_socket, to, message, 1); + ret = _socket_send_messages_wrapped (priv->base_socket, to, message, 1, + reliable); if (ret == 1) return output_message_get_size (message); return ret; @@ -1460,9 +1470,9 @@ priv_retransmissions_tick_unlocked (UdpTurnPriv *priv) } case STUN_USAGE_TIMER_RETURN_RETRANSMIT: /* Retransmit */ - nice_socket_send (priv->base_socket, &priv->server_addr, + _socket_send_wrapped (priv->base_socket, &priv->server_addr, stun_message_length (&priv->current_binding_msg->message), - (gchar *)priv->current_binding_msg->buffer); + (gchar *)priv->current_binding_msg->buffer, FALSE); ret = TRUE; break; case STUN_USAGE_TIMER_RETURN_SUCCESS: @@ -1525,9 +1535,9 @@ priv_retransmissions_create_permission_tick_unlocked (UdpTurnPriv *priv, GList * } case STUN_USAGE_TIMER_RETURN_RETRANSMIT: /* Retransmit */ - nice_socket_send (priv->base_socket, &priv->server_addr, + _socket_send_wrapped (priv->base_socket, &priv->server_addr, stun_message_length (¤t_create_permission_msg->message), - (gchar *)current_create_permission_msg->buffer); + (gchar *)current_create_permission_msg->buffer, FALSE); ret = TRUE; break; case STUN_USAGE_TIMER_RETURN_SUCCESS: @@ -1657,15 +1667,15 @@ priv_send_turn_message (UdpTurnPriv *priv, TURNMessage *msg) } if (nice_socket_is_reliable (priv->base_socket)) { - nice_socket_send_reliable (priv->base_socket, &priv->server_addr, - stun_len, (gchar *)msg->buffer); + _socket_send_wrapped (priv->base_socket, &priv->server_addr, + stun_len, (gchar *)msg->buffer, TRUE); stun_timer_start_reliable (&msg->timer, STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT); } else { - if (nice_socket_send_reliable (priv->base_socket, &priv->server_addr, - stun_len, (gchar *)msg->buffer) < 0) - nice_socket_send (priv->base_socket, &priv->server_addr, - stun_len, (gchar *)msg->buffer); + if (_socket_send_wrapped (priv->base_socket, &priv->server_addr, + stun_len, (gchar *)msg->buffer, TRUE) < 0) + _socket_send_wrapped (priv->base_socket, &priv->server_addr, + stun_len, (gchar *)msg->buffer, FALSE); stun_timer_start (&msg->timer, STUN_TIMER_DEFAULT_TIMEOUT, STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS); } @@ -1719,14 +1729,14 @@ priv_send_create_permission(UdpTurnPriv *priv, StunMessage *resp, if (msg_buf_len > 0) { if (nice_socket_is_reliable (priv->base_socket)) { - res = nice_socket_send_reliable (priv->base_socket, &priv->server_addr, - msg_buf_len, (gchar *) msg->buffer); + res = _socket_send_wrapped (priv->base_socket, &priv->server_addr, + msg_buf_len, (gchar *) msg->buffer, TRUE); } else { - res = nice_socket_send_reliable (priv->base_socket, &priv->server_addr, - msg_buf_len, (gchar *) msg->buffer); + res = _socket_send_wrapped (priv->base_socket, &priv->server_addr, + msg_buf_len, (gchar *) msg->buffer, TRUE); if (res < 0) - res = nice_socket_send (priv->base_socket, &priv->server_addr, - msg_buf_len, (gchar *) msg->buffer); + res = _socket_send_wrapped (priv->base_socket, &priv->server_addr, + msg_buf_len, (gchar *) msg->buffer, FALSE); } if (nice_socket_is_reliable (priv->base_socket)) { -- 2.7.4