From: Thomas Geymayer Date: Thu, 5 Jun 2014 15:25:12 +0000 (+0200) Subject: canvas::Image: abort http requests if image is destroyed. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e302ad092eb52efce99f74327ede5fa00e03a3ef;p=simgear.git canvas::Image: abort http requests if image is destroyed. --- diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 03079f11..2b910539 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -25,7 +25,6 @@ #include #include #include -#include #include #include @@ -149,7 +148,8 @@ namespace canvas //---------------------------------------------------------------------------- Image::~Image() { - + if( _http_request ) + _http_request->abort("image destroyed"); } //---------------------------------------------------------------------------- @@ -526,6 +526,13 @@ namespace canvas if( name == "file" ) SG_LOG(SG_GL, SG_WARN, "'file' is deprecated. Use 'src' instead"); + // Abort pending request + if( _http_request ) + { + _http_request->abort("setting new image"); + _http_request.reset(); + } + static const std::string PROTOCOL_SEP = "://"; std::string url = child->getStringValue(), @@ -580,9 +587,11 @@ namespace canvas else if( protocol == "http" || protocol == "https" ) // TODO check https { - Canvas::getSystemAdapter() + _http_request = + Canvas::getSystemAdapter() ->getHTTPClient() ->load(url) + // TODO handle capture of 'this' ->done(this, &Image::handleImageLoadDone); } else @@ -663,6 +672,11 @@ namespace canvas //---------------------------------------------------------------------------- void Image::handleImageLoadDone(HTTP::Request* req) { + // Ignore stale/expired requests + if( _http_request != req ) + return; + _http_request.reset(); + if( req->responseCode() != 200 ) { SG_LOG(SG_IO, SG_WARN, "failed to download '" << req->url() << "': " diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index eb730fc5..a6839045 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -22,6 +22,7 @@ #include "CanvasElement.hxx" #include +#include #include #include @@ -113,6 +114,7 @@ namespace canvas osg::ref_ptr _texture; // TODO optionally forward events to canvas CanvasWeakPtr _src_canvas; + HTTP::Request_ptr _http_request; osg::ref_ptr _geom; osg::ref_ptr_prim; diff --git a/simgear/io/HTTPRequest.cxx b/simgear/io/HTTPRequest.cxx index 535ec402..900b7fc2 100644 --- a/simgear/io/HTTPRequest.cxx +++ b/simgear/io/HTTPRequest.cxx @@ -304,7 +304,9 @@ void Request::setFailure(int code, const std::string& reason) { _responseStatus = code; _responseReason = reason; - setReadyState(FAILED); + + if( !isComplete() ) + setReadyState(FAILED); } //------------------------------------------------------------------------------ @@ -346,9 +348,6 @@ void Request::abort() //---------------------------------------------------------------------------- void Request::abort(const std::string& reason) { - if( isComplete() ) - return; - setFailure(-1, reason); _willClose = true; }