canvas::Image: abort http requests if image is destroyed.

This commit is contained in:
Thomas Geymayer
2014-06-05 17:25:12 +02:00
parent 7a65d42a4c
commit e302ad092e
3 changed files with 22 additions and 7 deletions
+17 -3
View File
@@ -25,7 +25,6 @@
#include <simgear/scene/util/OsgMath.hxx> #include <simgear/scene/util/OsgMath.hxx>
#include <simgear/scene/util/parse_color.hxx> #include <simgear/scene/util/parse_color.hxx>
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#include <simgear/io/HTTPClient.hxx>
#include <osg/Array> #include <osg/Array>
#include <osg/Geometry> #include <osg/Geometry>
@@ -149,7 +148,8 @@ namespace canvas
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
Image::~Image() Image::~Image()
{ {
if( _http_request )
_http_request->abort("image destroyed");
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@@ -526,6 +526,13 @@ namespace canvas
if( name == "file" ) if( name == "file" )
SG_LOG(SG_GL, SG_WARN, "'file' is deprecated. Use 'src' instead"); 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 = "://"; static const std::string PROTOCOL_SEP = "://";
std::string url = child->getStringValue(), std::string url = child->getStringValue(),
@@ -580,9 +587,11 @@ namespace canvas
else if( protocol == "http" || protocol == "https" ) else if( protocol == "http" || protocol == "https" )
// TODO check https // TODO check https
{ {
Canvas::getSystemAdapter() _http_request =
Canvas::getSystemAdapter()
->getHTTPClient() ->getHTTPClient()
->load(url) ->load(url)
// TODO handle capture of 'this'
->done(this, &Image::handleImageLoadDone); ->done(this, &Image::handleImageLoadDone);
} }
else else
@@ -663,6 +672,11 @@ namespace canvas
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void Image::handleImageLoadDone(HTTP::Request* req) void Image::handleImageLoadDone(HTTP::Request* req)
{ {
// Ignore stale/expired requests
if( _http_request != req )
return;
_http_request.reset();
if( req->responseCode() != 200 ) if( req->responseCode() != 200 )
{ {
SG_LOG(SG_IO, SG_WARN, "failed to download '" << req->url() << "': " SG_LOG(SG_IO, SG_WARN, "failed to download '" << req->url() << "': "
+2
View File
@@ -22,6 +22,7 @@
#include "CanvasElement.hxx" #include "CanvasElement.hxx"
#include <simgear/canvas/canvas_fwd.hxx> #include <simgear/canvas/canvas_fwd.hxx>
#include <simgear/io/HTTPClient.hxx>
#include <simgear/misc/CSSBorder.hxx> #include <simgear/misc/CSSBorder.hxx>
#include <osg/Texture2D> #include <osg/Texture2D>
@@ -113,6 +114,7 @@ namespace canvas
osg::ref_ptr<osg::Texture2D> _texture; osg::ref_ptr<osg::Texture2D> _texture;
// TODO optionally forward events to canvas // TODO optionally forward events to canvas
CanvasWeakPtr _src_canvas; CanvasWeakPtr _src_canvas;
HTTP::Request_ptr _http_request;
osg::ref_ptr<osg::Geometry> _geom; osg::ref_ptr<osg::Geometry> _geom;
osg::ref_ptr<osg::DrawArrays>_prim; osg::ref_ptr<osg::DrawArrays>_prim;
+3 -4
View File
@@ -304,7 +304,9 @@ void Request::setFailure(int code, const std::string& reason)
{ {
_responseStatus = code; _responseStatus = code;
_responseReason = reason; _responseReason = reason;
setReadyState(FAILED);
if( !isComplete() )
setReadyState(FAILED);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -346,9 +348,6 @@ void Request::abort()
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void Request::abort(const std::string& reason) void Request::abort(const std::string& reason)
{ {
if( isComplete() )
return;
setFailure(-1, reason); setFailure(-1, reason);
_willClose = true; _willClose = true;
} }