From 8f3dd9d417917de3f241391eed760041dbb47be0 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Fri, 17 Jul 2026 12:42:56 +0100 Subject: [PATCH 1/2] Use atomic bRun for socket --- src/socket.cpp | 6 +++--- src/socket.h | 12 +++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/socket.cpp b/src/socket.cpp index 0a83922342..80662e0292 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -531,7 +531,7 @@ bool CSocket::GetAndResetbJitterBufferOKFlag() return true; } -void CSocket::OnDataReceived() +void CSocket::OnDataReceived ( std::atomic& bRun ) { /* The strategy of this function is that only the "put audio" function is @@ -585,7 +585,7 @@ void CSocket::OnDataReceived() sockaddr_in sa4; socklen_t sa4len = sizeof ( sa4 ); - while ( true ) + while ( bRun ) { const long iNumBytesRead = recvfrom ( UdpSocket4, (char*) &vecbyRecBuf[0], MAX_SIZE_BYTES_NETW_BUF, 0, (struct sockaddr*) &sa4, &sa4len ); @@ -634,7 +634,7 @@ void CSocket::OnDataReceived() sockaddr_in6 sa6; socklen_t sa6len = sizeof ( sa6 ); - while ( true ) + while ( bRun ) { const long iNumBytesRead = recvfrom ( UdpSocket6, (char*) &vecbyRecBuf[0], MAX_SIZE_BYTES_NETW_BUF, 0, (struct sockaddr*) &sa6, &sa6len ); diff --git a/src/socket.h b/src/socket.h index 4cd80bb1a2..48c6109604 100644 --- a/src/socket.h +++ b/src/socket.h @@ -50,6 +50,7 @@ #include #include #include +#include #include "global.h" #include "protocol.h" #include "util.h" @@ -134,7 +135,7 @@ class CSocket : public QObject void ProcessPacket ( const CHostAddress& RecHostAddr, const int iNumBytesRead ); public: - void OnDataReceived(); + void OnDataReceived ( std::atomic& bRun ); signals: void NewConnection(); // for the client @@ -212,9 +213,6 @@ class CHighPrioSocket : public QObject // disable run flag so that the thread loop can be exit bRun = false; - // to leave blocking wait for receive - pSocket->Close(); - // give thread some time to terminate wait ( 5000 ); } @@ -232,13 +230,13 @@ class CHighPrioSocket : public QObject { // this function is a blocking function (waiting for network // packets to be received and processed) - pSocket->OnDataReceived(); + pSocket->OnDataReceived ( bRun ); } } } - CSocket* pSocket; - bool bRun; + CSocket* pSocket; + std::atomic bRun; // atomic, as it is set and tested by different threads }; void Init() From 2e696275d4c1729852d4eb03529e1a5d5eea4de8 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Fri, 17 Jul 2026 16:16:09 +0100 Subject: [PATCH 2/2] Remove Close method, not needed for non-blocking read --- src/socket.cpp | 35 ----------------------------------- src/socket.h | 1 - 2 files changed, 36 deletions(-) diff --git a/src/socket.cpp b/src/socket.cpp index 80662e0292..60557d7008 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -386,41 +386,6 @@ void CSocket::Init ( const quint16 iNewPortNumber, } } -void CSocket::Close() -{ - if ( UdpSocket4 != INVALID_SOCKET ) - { -#ifdef _WIN32 - // closesocket will cause recvfrom to return with an error because the - // socket is closed -> then the thread can safely be shut down - closesocket ( UdpSocket4 ); -#elif defined( __APPLE__ ) || defined( __MACOSX ) - // on Mac the general close has the same effect as closesocket on Windows - close ( UdpSocket4 ); -#else - // on Linux the shutdown call cancels the recvfrom - shutdown ( UdpSocket4, SHUT_RDWR ); -#endif - UdpSocket4 = INVALID_SOCKET; - } - - if ( UdpSocket6 != INVALID_SOCKET ) - { -#ifdef _WIN32 - // closesocket will cause recvfrom to return with an error because the - // socket is closed -> then the thread can safely be shut down - closesocket ( UdpSocket6 ); -#elif defined( __APPLE__ ) || defined( __MACOSX ) - // on Mac the general close has the same effect as closesocket on Windows - close ( UdpSocket6 ); -#else - // on Linux the shutdown call cancels the recvfrom - shutdown ( UdpSocket6, SHUT_RDWR ); -#endif - UdpSocket6 = INVALID_SOCKET; - } -} - CSocket::~CSocket() { // cleanup the socket (on Windows the WSA cleanup must also be called) diff --git a/src/socket.h b/src/socket.h index 48c6109604..010935d3ff 100644 --- a/src/socket.h +++ b/src/socket.h @@ -100,7 +100,6 @@ class CSocket : public QObject void SendPacket ( const CVector& vecbySendBuf, const CHostAddress& HostAddr ); bool GetAndResetbJitterBufferOKFlag(); - void Close(); protected: void Init ( const quint16 iPortNumber,