diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 61f9b56bae..b433b2a199 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -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; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 235f6be8f0..a030d51c6e 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -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; diff --git a/shared/src/UDPSocket.cpp b/shared/src/UDPSocket.cpp index fd6102f367..ec4c63803e 100644 --- a/shared/src/UDPSocket.cpp +++ b/shared/src/UDPSocket.cpp @@ -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); }