]> git.mxchange.org Git - simgear.git/commitdiff
canvas::Image: abort http requests if image is destroyed.
authorThomas Geymayer <tomgey@gmail.com>
Thu, 5 Jun 2014 15:25:12 +0000 (17:25 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 5 Jun 2014 15:25:12 +0000 (17:25 +0200)
simgear/canvas/elements/CanvasImage.cxx
simgear/canvas/elements/CanvasImage.hxx
simgear/io/HTTPRequest.cxx

index 03079f110c50f3a8e3e74b0365ffb98a38469782..2b91053933027739a691ed1b309b420a4402f3b0 100644 (file)
@@ -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() << "': "
index eb730fc53c8b3890c2abe7c1d6ecaf5bee9db9b8..a68390456a041b38ff9c1b680ab223495acd74e0 100644 (file)
@@ -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;
index 535ec402da8fa9c043ded3de7f9f7e0d0238fa63..900b7fc29f9178ea9d2a572752e8af0b2bbe9bc5 100644 (file)
@@ -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;
 }