From e989621fe063787c892f068b2734a96b21b7ea77 Mon Sep 17 00:00:00 2001 From: bossiel Date: Mon, 15 Jun 2015 07:48:04 +0000 Subject: [PATCH] Add send retry support for WIN32 TCP transport --- .../tinyDAV/src/codecs/vpx/tdav_codec_vp8.c | 24 ++++--- .../tinyNET/src/tnet_transport_win32.c | 69 ++++++++++--------- .../2.0/doubango/tinyNET/src/tnet_utils.h | 11 ++- .../tinyNET/src/turn/tnet_turn_session.c | 12 ++++ 4 files changed, 67 insertions(+), 49 deletions(-) diff --git a/branches/2.0/doubango/tinyDAV/src/codecs/vpx/tdav_codec_vp8.c b/branches/2.0/doubango/tinyDAV/src/codecs/vpx/tdav_codec_vp8.c index b0f4a5d4..f8e7f2fa 100644 --- a/branches/2.0/doubango/tinyDAV/src/codecs/vpx/tdav_codec_vp8.c +++ b/branches/2.0/doubango/tinyDAV/src/codecs/vpx/tdav_codec_vp8.c @@ -324,16 +324,17 @@ static tsk_size_t tdav_codec_vp8_decode(tmedia_codec_t* self, const void* in_dat } { /* 4.2. VP8 Payload Descriptor */ - uint8_t X, R, N, I, L, T, K;//FIXME: store + uint8_t X, R, N, I, L, T, K;//TODO: store - X = (*pdata & 0x80) >> 7; - R = (*pdata & 0x40) >> 6; + X = ((*pdata & 0x80) >> 7); + R = ((*pdata & 0x40) >> 6); if (R) { TSK_DEBUG_ERROR("R<>0"); - return 0; + fatal_error = tsk_true; + goto bail; } - N = (*pdata & 0x20) >> 5; - S = (*pdata & 0x10) >> 4; + N = ((*pdata & 0x20) >> 5); + S = ((*pdata & 0x10) >> 4); PartID = (*pdata & 0x0F); // skip "REQUIRED" header if (++pdata >= pdata_end) { @@ -419,10 +420,14 @@ static tsk_size_t tdav_codec_vp8_decode(tmedia_codec_t* self, const void* in_dat // Make sure the header is present if (S != 1 || PartID != 0 || in_size < 3) { TSK_DEBUG_WARN("VP8 payload header is missing"); - fatal_error = tsk_true; - goto bail; +#if 0 + if (in_size < 3) +#endif + { + fatal_error = tsk_true; + goto bail; + } } - { /* SizeN: The size of the first partition in bytes is calculated from the 19 bits in Size0, Size1, and Size2 as 1stPartitionSize = Size0 @@ -563,7 +568,6 @@ bail: TMEDIA_CODEC_VIDEO(self)->in.result.proto_hdr = proto_hdr; TMEDIA_CODEC_VIDEO(self)->in.callback(&TMEDIA_CODEC_VIDEO(self)->in.result); } - fatal_error = tsk_true; // vp8->decoder.last_PartID = PartID; // vp8->decoder.last_S = S; diff --git a/branches/2.0/doubango/tinyNET/src/tnet_transport_win32.c b/branches/2.0/doubango/tinyNET/src/tnet_transport_win32.c index 592bccae..814d28e4 100644 --- a/branches/2.0/doubango/tinyNET/src/tnet_transport_win32.c +++ b/branches/2.0/doubango/tinyNET/src/tnet_transport_win32.c @@ -227,50 +227,55 @@ tsk_size_t tnet_transport_send(const tnet_transport_handle_t *handle, tnet_fd_t { tnet_transport_t *transport = (tnet_transport_t*)handle; int ret = -1; - DWORD numberOfBytesSent = 0; + tsk_size_t sent = 0; if (!transport){ TSK_DEBUG_ERROR("Invalid transport handle."); goto bail; } - if (transport->tls.enabled){ - transport_socket_xt* socket = getSocket(transport->context, from); - if (socket && socket->tlshandle){ - if (!tnet_tls_socket_send(socket->tlshandle, buf, size)){ - numberOfBytesSent = (DWORD)size; - } - else{ - TSK_DEBUG_ERROR("Tring to use a socket without TLS handle to send data"); - numberOfBytesSent = 0; - } - goto bail; - } - } - else{ - WSABUF wsaBuffer; - wsaBuffer.buf = (CHAR*)buf; - wsaBuffer.len = (ULONG)size; - - if ((ret = WSASend(from, &wsaBuffer, 1, &numberOfBytesSent, 0, NULL, NULL)) == SOCKET_ERROR){ - if ((ret = WSAGetLastError()) == WSA_IO_PENDING){ - TSK_DEBUG_INFO("WSA_IO_PENDING error for WSASend SSESSION"); - ret = 0; - } - else{ - TNET_PRINT_LAST_ERROR("WSASend have failed."); - - //tnet_sockfd_close(&from); - goto bail; + while (sent < size) { + int try_guard = 10; + if (transport->tls.enabled){ + transport_socket_xt* socket = getSocket(transport->context, from); + if (socket && socket->tlshandle) { + if (tnet_tls_socket_send(socket->tlshandle, buf, size) == 0) { + sent = size; + } + else { + TSK_DEBUG_ERROR("Tring to use a socket without TLS handle to send data"); + } + goto bail; // TLS do not retry } } - else{ - ret = 0; + else { + WSABUF wsaBuffer; + DWORD numberOfBytesSent = 0; + wsaBuffer.buf = ((CHAR*)buf) + sent; + wsaBuffer.len = (ULONG)(size - sent); +try_again: + if ((ret = WSASend(from, &wsaBuffer, 1, &numberOfBytesSent, 0, NULL, NULL)) == SOCKET_ERROR) { + ret = WSAGetLastError(); + if (ret == WSA_IO_PENDING || ret == WSAEWOULDBLOCK) { + TSK_DEBUG_INFO("SendTCP() - WouldBlock. Retrying..."); + if (try_guard--) { + tsk_thread_sleep(10); + goto try_again; + } + } + else { + TNET_PRINT_LAST_ERROR("WSASend have failed."); + goto bail; + } + } + else { + sent += numberOfBytesSent; + } } } bail: - return numberOfBytesSent; + return sent; } /* diff --git a/branches/2.0/doubango/tinyNET/src/tnet_utils.h b/branches/2.0/doubango/tinyNET/src/tnet_utils.h index 158fee08..2c4a0ad6 100644 --- a/branches/2.0/doubango/tinyNET/src/tnet_utils.h +++ b/branches/2.0/doubango/tinyNET/src/tnet_utils.h @@ -1,7 +1,5 @@ /* -* Copyright (C) 2010-2011 Mamadou Diop. -* -* Contact: Mamadou Diop +* Copyright (C) 2010-2015 Mamadou DIOP. * * This file is part of Open Source Doubango Framework. * @@ -23,9 +21,6 @@ /**@file tnet_utils.h * @brief Network utility functions. * - * @author Mamadou Diop - * - */ #ifndef TNET_UTILS_H #define TNET_UTILS_H @@ -39,7 +34,9 @@ TNET_BEGIN_DECLS /**@ingroup tnet_utils_group */ -#define TNET_CONNECT_TIMEOUT 2000 +#if !defined(TNET_CONNECT_TIMEOUT) +# define TNET_CONNECT_TIMEOUT 2000 +#endif /* TNET_CONNECT_TIMEOUT */ /**Interface. */ diff --git a/branches/2.0/doubango/tinyNET/src/turn/tnet_turn_session.c b/branches/2.0/doubango/tinyNET/src/turn/tnet_turn_session.c index aa12347e..0b3eae98 100644 --- a/branches/2.0/doubango/tinyNET/src/turn/tnet_turn_session.c +++ b/branches/2.0/doubango/tinyNET/src/turn/tnet_turn_session.c @@ -1496,13 +1496,21 @@ static int _tnet_turn_session_send_buff_0(tnet_turn_session_t* p_self, const tne } if (TNET_SOCKET_TYPE_IS_DGRAM(p_self->p_lcl_sock->type)) { +#if 0 + u_sent_bytes = tnet_transport_sendto(p_self->p_transport, p_self->p_lcl_sock->fd, (const struct sockaddr *)&p_self->srv_addr, pc_buff_ptr, u_buff_size); +#else u_sent_bytes = tnet_sockfd_sendto(p_self->p_lcl_sock->fd, (const struct sockaddr *)&p_self->srv_addr, pc_buff_ptr, u_buff_size); +#endif } else { if (pc_peer && pc_peer->b_stream_connected && pc_peer->conn_fd != TNET_INVALID_FD) { // Send using Peer connection if connected // Should never be called because for now requested transport is always equal to UDP +#if 0 + u_sent_bytes = tnet_transport_send(p_self->p_transport, pc_peer->conn_fd, pc_buff_ptr, u_buff_size); +#else u_sent_bytes = tnet_sockfd_send(pc_peer->conn_fd, pc_buff_ptr, u_buff_size, 0); +#endif } else { // Connect if not already done @@ -1511,7 +1519,11 @@ static int _tnet_turn_session_send_buff_0(tnet_turn_session_t* p_self, const tne p_self->b_stream_connected = (ret == 0); } if (p_self->b_stream_connected) { +#if 0 + u_sent_bytes = tnet_transport_send(p_self->p_transport, p_self->p_lcl_sock->fd, pc_buff_ptr, u_buff_size); +#else u_sent_bytes = tnet_socket_send_stream(p_self->p_lcl_sock, pc_buff_ptr, u_buff_size); +#endif } } }