diff --git a/src/osgPlugins/curl/ReaderWriterCURL.cpp b/src/osgPlugins/curl/ReaderWriterCURL.cpp index 16dd3778a..48235421a 100644 --- a/src/osgPlugins/curl/ReaderWriterCURL.cpp +++ b/src/osgPlugins/curl/ReaderWriterCURL.cpp @@ -83,6 +83,8 @@ EasyCurl::EasyCurl() osg::notify(osg::INFO)<<"EasyCurl::EasyCurl()"<getAuthenticationMap() : osgDB::Registry::instance()->getAuthenticationMap(); + // Set the timeout value here: + // Note that this has an effect only in a connection phase. + // WARNING: here we make the assumption that if someone starts using the timeouts settings + // he will not try to disable them afterwards (a value must be provided or the previous value is used). + if(_connectTimeout > 0) + curl_easy_setopt(_curl, CURLOPT_CONNECTTIMEOUT, _connectTimeout); + if(_timeout > 0) + curl_easy_setopt(_curl, CURLOPT_TIMEOUT, _timeout); + if(!proxyAddress.empty()) { osg::notify(osg::INFO)<<"Setting proxy: "<getOptionString()); @@ -296,6 +311,10 @@ osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType optProxy = opt.substr( index+1 ); else if( opt.substr( 0, index ) == "OSG_CURL_PROXYPORT" ) optProxyPort = opt.substr( index+1 ); + else if( opt.substr( 0, index ) == "OSG_CURL_CONNECTTIMEOUT" ) + connectTimeout = atol(opt.substr( index+1 ).c_str()); // this will return 0 in case of improper format. + else if( opt.substr( 0, index ) == "OSG_CURL_TIMEOUT" ) + timeout = atol(opt.substr( index+1 ).c_str()); // this will return 0 in case of improper format. } //Setting Proxy by OSG Options @@ -379,6 +398,10 @@ osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType EasyCurl::StreamObject sp(&buffer, std::string()); + // setup the timeouts: + getEasyCurl().setConnectionTimeout(connectTimeout); + getEasyCurl().setTimeout(timeout); + ReadResult curlResult = getEasyCurl().read(proxyAddress, fileName, sp, options); if (curlResult.status()==ReadResult::FILE_LOADED) diff --git a/src/osgPlugins/curl/ReaderWriterCURL.h b/src/osgPlugins/curl/ReaderWriterCURL.h index b41b1177b..ab7a1af39 100644 --- a/src/osgPlugins/curl/ReaderWriterCURL.h +++ b/src/osgPlugins/curl/ReaderWriterCURL.h @@ -51,6 +51,14 @@ class EasyCurl : public osg::Referenced EasyCurl(); + // Added this function to set the desired connection timeout if needed (in case someone needs to try to connect + // to offline servers without freezing the process for a very long time) [even if, as stated on curl website, + // some normal transfer may be timed out this way]. + inline void setConnectionTimeout(long val) { _connectTimeout = val; } + + // the timeout variable is used to limit the whole transfer duration instead of the connection phase only. + inline void setTimeout(long val) { _timeout = val; } + osgDB::ReaderWriter::ReadResult read(const std::string& proxyAddress, const std::string& fileName, StreamObject& sp, const osgDB::ReaderWriter::Options *options); /** Returns the mime type of the data retrieved with the provided stream object on a @@ -70,6 +78,8 @@ class EasyCurl : public osg::Referenced std::string _previousPassword; long _previousHttpAuthentication; + long _connectTimeout; + long _timeout; };