Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 3 additions & 38 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -531,7 +496,7 @@ bool CSocket::GetAndResetbJitterBufferOKFlag()
return true;
}

void CSocket::OnDataReceived()
void CSocket::OnDataReceived ( std::atomic<bool>& bRun )
{
/*
The strategy of this function is that only the "put audio" function is
Expand Down Expand Up @@ -585,7 +550,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 );
Expand Down Expand Up @@ -634,7 +599,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 );
Expand Down
13 changes: 5 additions & 8 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <QThread>
#include <QMutex>
#include <vector>
#include <atomic>
#include "global.h"
#include "protocol.h"
#include "util.h"
Expand Down Expand Up @@ -99,7 +100,6 @@ class CSocket : public QObject
void SendPacket ( const CVector<uint8_t>& vecbySendBuf, const CHostAddress& HostAddr );

bool GetAndResetbJitterBufferOKFlag();
void Close();

protected:
void Init ( const quint16 iPortNumber,
Expand Down Expand Up @@ -134,7 +134,7 @@ class CSocket : public QObject
void ProcessPacket ( const CHostAddress& RecHostAddr, const int iNumBytesRead );

public:
void OnDataReceived();
void OnDataReceived ( std::atomic<bool>& bRun );

signals:
void NewConnection(); // for the client
Expand Down Expand Up @@ -212,9 +212,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 );
}
Expand All @@ -232,13 +229,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<bool> bRun; // atomic, as it is set and tested by different threads
};

void Init()
Expand Down
Loading