From 42af1797703bbbe6228c392b6f888b81e742a00e Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 16 Feb 2016 09:31:33 -0500 Subject: [PATCH] Add a 10 second read timeout to the RTSP connection --- limelight-common/PlatformSockets.c | 16 +++++++++++++++- limelight-common/PlatformSockets.h | 1 + limelight-common/RtspConnection.c | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/limelight-common/PlatformSockets.c b/limelight-common/PlatformSockets.c index d6e22fe..71db8bf 100644 --- a/limelight-common/PlatformSockets.c +++ b/limelight-common/PlatformSockets.c @@ -27,6 +27,20 @@ void shutdownTcpSocket(SOCKET s) { shutdown(s, SHUT_RDWR); } +void setRecvTimeout(SOCKET s, int timeoutSec) { +#if defined(LC_WINDOWS) + int val = timeoutSec * 1000; +#else + struct timeval val; + val.tv_sec = timeoutSec; + val.tv_usec = 0; +#endif + + if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&val, sizeof(val)) < 0) { + Limelog("setsockopt(SO_RCVTIMEO) failed: %d\n", (int)LastSocketError()); + } +} + int recvUdpSocket(SOCKET s, char* buffer, int size) { fd_set readfds; int err; @@ -50,7 +64,7 @@ int recvUdpSocket(SOCKET s, char* buffer, int size) { } void closeSocket(SOCKET s) { -#ifdef _WIN32 +#if defined(LC_WINDOWS) closesocket(s); #else close(s); diff --git a/limelight-common/PlatformSockets.h b/limelight-common/PlatformSockets.h index 0124000..fe8dc62 100644 --- a/limelight-common/PlatformSockets.h +++ b/limelight-common/PlatformSockets.h @@ -46,4 +46,5 @@ SOCKET bindUdpSocket(int addrfamily, int bufferSize); int enableNoDelay(SOCKET s); int recvUdpSocket(SOCKET s, char* buffer, int size); void shutdownTcpSocket(SOCKET s); +void setRecvTimeout(SOCKET s, int timeoutSec); void closeSocket(SOCKET s); \ No newline at end of file diff --git a/limelight-common/RtspConnection.c b/limelight-common/RtspConnection.c index 2a01753..2905cea 100644 --- a/limelight-common/RtspConnection.c +++ b/limelight-common/RtspConnection.c @@ -2,6 +2,7 @@ #include "Rtsp.h" #define RTSP_MAX_RESP_SIZE 32768 +#define RTSP_READ_TIMEOUT_SEC 10 static SOCKET sock = INVALID_SOCKET; static int currentSeqNumber; @@ -93,6 +94,7 @@ static int transactRtspMessage(PRTSP_MESSAGE request, PRTSP_MESSAGE response, in return ret; } enableNoDelay(sock); + setRecvTimeout(sock, RTSP_READ_TIMEOUT_SEC); serializedMessage = serializeRtspMessage(request, &messageLen); if (serializedMessage == NULL) {