From d11050db34dd82995614e8f4b66a95b569b6d03d Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 13 Jan 2014 11:22:50 +0100 Subject: [PATCH] Added timeout to DTSC::Stream::waitForMeta() function --- lib/dtsc.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/dtsc.cpp b/lib/dtsc.cpp index dcc62dd7..9fd21f4e 100644 --- a/lib/dtsc.cpp +++ b/lib/dtsc.cpp @@ -180,18 +180,21 @@ void DTSC::Stream::endStream(){ /// Blocks until either the stream has metadata available or the sourceSocket errors. /// This function is intended to be run before any commands are sent and thus will not throw away anything important. +/// It will time out after 5 seconds, disconnecting the sourceSocket. void DTSC::Stream::waitForMeta(Socket::Connection & sourceSocket){ - while ( !metadata && sourceSocket.connected()){ + //cancel the attempt after 5000 milliseconds + long long int start = Util::getMS(); + while ( !metadata && sourceSocket.connected() && Util::getMS() - start < 5000){ //we have data? attempt to read header if (sourceSocket.Received().size()){ - //return value is ignore because we're not interested in data packets, just metadata. + //return value is ignored because we're not interested in data packets, just metadata. parsePacket(sourceSocket.Received()); } //still no header? check for more data if ( !metadata){ if (sourceSocket.spool()){ //more received? attempt to read - //return value is ignore because we're not interested in data packets, just metadata. + //return value is ignored because we're not interested in data packets, just metadata. parsePacket(sourceSocket.Received()); }else{ //nothing extra to receive? wait a bit and retry @@ -199,6 +202,14 @@ void DTSC::Stream::waitForMeta(Socket::Connection & sourceSocket){ } } } + //if the timeout has passed, close the socket + if (Util::getMS() - start >= 5000){ + sourceSocket.close(); + //and optionally print a debug message that this happened + #if DEBUG >= 4 + fprintf(stderr, "Timed out while waiting for metadata\n"); + #endif + } } /// Resets the stream by clearing the buffers and keyframes, making sure to call the deletionCallback first.