canvas::Image: abort http requests if image is destroyed.
This commit is contained in:
parent
7a65d42a4c
commit
e302ad092e
@ -25,7 +25,6 @@
|
||||
#include <simgear/scene/util/OsgMath.hxx>
|
||||
#include <simgear/scene/util/parse_color.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/io/HTTPClient.hxx>
|
||||
|
||||
#include <osg/Array>
|
||||
#include <osg/Geometry>
|
||||
@ -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() << "': "
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "CanvasElement.hxx"
|
||||
|
||||
#include <simgear/canvas/canvas_fwd.hxx>
|
||||
#include <simgear/io/HTTPClient.hxx>
|
||||
#include <simgear/misc/CSSBorder.hxx>
|
||||
#include <osg/Texture2D>
|
||||
|
||||
@ -113,6 +114,7 @@ namespace canvas
|
||||
osg::ref_ptr<osg::Texture2D> _texture;
|
||||
// TODO optionally forward events to canvas
|
||||
CanvasWeakPtr _src_canvas;
|
||||
HTTP::Request_ptr _http_request;
|
||||
|
||||
osg::ref_ptr<osg::Geometry> _geom;
|
||||
osg::ref_ptr<osg::DrawArrays>_prim;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user