From 167d344e39884bab540925bfa183c61c7c74eca0 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 15 Feb 2016 17:45:29 -0500 Subject: [PATCH] Add a shutdown function for UDP sockets --- limelight-common/AudioStream.c | 8 ++++++-- limelight-common/ControlStream.c | 2 +- limelight-common/InputStream.c | 2 +- limelight-common/PlatformSockets.c | 21 ++++++++++++++++++++- limelight-common/PlatformSockets.h | 3 ++- limelight-common/RtspConnection.c | 2 +- limelight-common/VideoStream.c | 15 +++++++++++---- 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/limelight-common/AudioStream.c b/limelight-common/AudioStream.c index c67e7b0..737e0de 100644 --- a/limelight-common/AudioStream.c +++ b/limelight-common/AudioStream.c @@ -255,8 +255,7 @@ void stopAudioStream(void) { } if (rtpSocket != INVALID_SOCKET) { - closeSocket(rtpSocket); - rtpSocket = INVALID_SOCKET; + shutdownUdpSocket(rtpSocket); } PltJoinThread(&udpPingThread); @@ -270,6 +269,11 @@ void stopAudioStream(void) { if ((AudioCallbacks.capabilities & CAPABILITY_DIRECT_SUBMIT) == 0) { PltCloseThread(&decoderThread); } + + if (rtpSocket != INVALID_SOCKET) { + closeSocket(rtpSocket); + rtpSocket = INVALID_SOCKET; + } AudioCallbacks.cleanup(); } diff --git a/limelight-common/ControlStream.c b/limelight-common/ControlStream.c index fc90304..d868021 100644 --- a/limelight-common/ControlStream.c +++ b/limelight-common/ControlStream.c @@ -393,7 +393,7 @@ int stopControlStream(void) { PltSetEvent(&invalidateRefFramesEvent); if (ctlSock != INVALID_SOCKET) { - shutdownSocket(ctlSock); + shutdownTcpSocket(ctlSock); } PltInterruptThread(&lossStatsThread); diff --git a/limelight-common/InputStream.c b/limelight-common/InputStream.c index 587f590..f03f3d0 100644 --- a/limelight-common/InputStream.c +++ b/limelight-common/InputStream.c @@ -294,7 +294,7 @@ int stopInputStream(void) { PltInterruptThread(&inputSendThread); if (inputSock != INVALID_SOCKET) { - shutdownSocket(inputSock); + shutdownTcpSocket(inputSock); } PltJoinThread(&inputSendThread); diff --git a/limelight-common/PlatformSockets.c b/limelight-common/PlatformSockets.c index 2678fe8..9b13859 100644 --- a/limelight-common/PlatformSockets.c +++ b/limelight-common/PlatformSockets.c @@ -21,12 +21,31 @@ void addrToUrlSafeString(struct sockaddr_storage* addr, char* string) } } -void shutdownSocket(SOCKET s) { +void shutdownTcpSocket(SOCKET s) { // Calling shutdown() prior to close wakes up callers // blocked in connect(), recv(), and friends. shutdown(s, SHUT_RDWR); } +void shutdownUdpSocket(SOCKET s) { + SOCKADDR_LEN len; + struct sockaddr_storage addr; + unsigned char buf[1]; + + // UDP sockets can't be shutdown(), so we'll indicate + // termination by sending a 0 byte packet to ourselves + + if (getsockname(s, (struct sockaddr*)&addr, &len) < 0) { + Limelog("getsockname() failed: %d\n", (int)LastSocketError()); + return; + } + + if (sendto(s, buf, 0, 0, (struct sockaddr*)&addr, len) < 0) { + Limelog("sendto() failed: %d\n", (int)LastSocketError()); + return; + } +} + void closeSocket(SOCKET s) { #ifdef _WIN32 closesocket(s); diff --git a/limelight-common/PlatformSockets.h b/limelight-common/PlatformSockets.h index 0383e7a..971227f 100644 --- a/limelight-common/PlatformSockets.h +++ b/limelight-common/PlatformSockets.h @@ -43,5 +43,6 @@ void addrToUrlSafeString(struct sockaddr_storage* addr, char* string); SOCKET connectTcpSocket(struct sockaddr_storage* dstaddr, SOCKADDR_LEN addrlen, unsigned short port); SOCKET bindUdpSocket(int addrfamily, int bufferSize); int enableNoDelay(SOCKET s); -void shutdownSocket(SOCKET s); +void shutdownUdpSocket(SOCKET s); +void shutdownTcpSocket(SOCKET s); void closeSocket(SOCKET s); \ No newline at end of file diff --git a/limelight-common/RtspConnection.c b/limelight-common/RtspConnection.c index 24ddb54..2a01753 100644 --- a/limelight-common/RtspConnection.c +++ b/limelight-common/RtspConnection.c @@ -148,7 +148,7 @@ Exit: // The thread waiting on RTSP will close the socket. void terminateRtspHandshake(void) { if (sock != INVALID_SOCKET) { - shutdownSocket(sock); + shutdownTcpSocket(sock); } } diff --git a/limelight-common/VideoStream.c b/limelight-common/VideoStream.c index 5470c01..a38c103 100644 --- a/limelight-common/VideoStream.c +++ b/limelight-common/VideoStream.c @@ -159,12 +159,10 @@ void stopVideoStream(void) { } if (firstFrameSocket != INVALID_SOCKET) { - closeSocket(firstFrameSocket); - firstFrameSocket = INVALID_SOCKET; + shutdownTcpSocket(firstFrameSocket); } if (rtpSocket != INVALID_SOCKET) { - closeSocket(rtpSocket); - rtpSocket = INVALID_SOCKET; + shutdownUdpSocket(rtpSocket); } PltJoinThread(&udpPingThread); @@ -178,6 +176,15 @@ void stopVideoStream(void) { if ((VideoCallbacks.capabilities & CAPABILITY_DIRECT_SUBMIT) == 0) { PltCloseThread(&decoderThread); } + + if (firstFrameSocket != INVALID_SOCKET) { + closeSocket(firstFrameSocket); + firstFrameSocket = INVALID_SOCKET; + } + if (rtpSocket != INVALID_SOCKET) { + closeSocket(rtpSocket); + rtpSocket = INVALID_SOCKET; + } VideoCallbacks.cleanup(); }