From e5a436b1314719b675d53d74f9f43c0b92eef4e5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 10 Jun 2009 09:24:12 +0000 Subject: [PATCH] From Emmanuel Roche, "I've updated the CURL plugin to support the CURL_CONNECTTIMEOUT and CURL_TIMEOUT options. Those two additional options can now be set using the Options::setOptionsString() function (just like the already existing OSG_CURL_PROXY & OSG_CURL_PROXYPORT options). This is a convient solution to limit the freezing effect one may face in case the targeted server is down or too slow. I successfully compiled and used this updated version on Windows in my application. And by default those settings are not set (so no change in the behavior if you don't need them). " --- src/osgPlugins/curl/ReaderWriterCURL.cpp | 25 +++++++++++++++++++++++- src/osgPlugins/curl/ReaderWriterCURL.h | 10 ++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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; };