fix threaded socket joins so that socket has timeout on blocking receive

This commit is contained in:
Stephen Birarda 2013-02-22 13:37:57 -08:00
parent 200982dddc
commit 345ec8b938
3 changed files with 19 additions and 11 deletions

View file

@ -44,9 +44,8 @@ const char EC2_WEST_MIXER[] = "54.241.92.53";
const int AUDIO_UDP_LISTEN_PORT = 55444;
int starve_counter = 0;
bool stopAudioReceiveThread = false;
StDev stdev;
bool stopAudioReceiveThread = false;
#define LOG_SAMPLE_DELAY 1
@ -263,6 +262,8 @@ void *receiveAudioViaUDP(void *args) {
}
}
}
pthread_exit(0);
}
/**
@ -306,6 +307,7 @@ Audio::Audio(Oscilloscope * s)
Pa_StartStream(stream);
if (paError != paNoError) goto error;
return;
error:
@ -420,6 +422,9 @@ void Audio::render(int screenWidth, int screenHeight)
*/
bool Audio::terminate ()
{
stopAudioReceiveThread = true;
pthread_join(audioReceiveThread, NULL);
if (initialized) {
initialized = false;
@ -428,14 +433,10 @@ bool Audio::terminate ()
paError = Pa_Terminate();
if (paError != paNoError) goto error;
delete audioData;
logFile.close();
}
stopAudioReceiveThread = true;
pthread_join(audioReceiveThread, NULL);
logFile.close();
delete audioData;
return true;

View file

@ -60,7 +60,7 @@ const int DOMAINSERVER_PORT = 40102;
AgentList agentList;
pthread_t networkReceiveThread;
bool stopAgentDataReceiveThread = false;
bool stopNetworkReceiveThread = false;
// For testing, add milliseconds of delay for received UDP packets
int packetcount = 0;
@ -362,8 +362,9 @@ void terminate () {
//close(serial_fd);
audio.terminate();
stopAgentDataReceiveThread = true;
stopNetworkReceiveThread = true;
pthread_join(networkReceiveThread, NULL);
exit(EXIT_SUCCESS);
}
@ -767,7 +768,7 @@ void *networkReceive(void *args)
ssize_t bytesReceived;
char *incomingPacket = new char[MAX_PACKET_SIZE];
while (!stopAgentDataReceiveThread) {
while (!stopNetworkReceiveThread) {
if (agentList.getAgentSocket()->receive(&senderAddress, incomingPacket, &bytesReceived)) {
packetcount++;
bytescount += bytesReceived;

View file

@ -81,6 +81,12 @@ UDPSocket::UDPSocket(int listeningPort) {
return;
}
// set timeout on socket recieve to 0.5 seconds
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv);
printf("Created UDP socket listening on port %d.\n", listeningPort);
}