diff --git a/simgear/io/HTTPFileRequest.cxx b/simgear/io/HTTPFileRequest.cxx index 22b25b16..b594774e 100644 --- a/simgear/io/HTTPFileRequest.cxx +++ b/simgear/io/HTTPFileRequest.cxx @@ -30,9 +30,7 @@ namespace HTTP FileRequest::FileRequest(const std::string& url, const std::string& path, bool append): Request(url, "GET"), _filename(path), - _append(append), - _callback(nullptr), - _callback_ref(nullptr) + _append(append) { if (append && _filename.isFile()) { size_t size = _filename.sizeInBytes(); @@ -45,10 +43,13 @@ namespace HTTP } } - void FileRequest::setCallback(Callback callback, void* ref) + void FileRequest::setCallback(std::function callback) { _callback = callback; - _callback_ref = ref; + if (readyState() == DONE) { + SG_LOG(SG_GENERAL, SG_DEBUG, "making callback because readyState() == DONE"); + _callback(nullptr, 0); + } } //---------------------------------------------------------------------------- @@ -67,12 +68,12 @@ namespace HTTP } else if (_append && (responseCode() == 206 || responseCode() == 416)) { /* See comments for simgear::HTTP::Client::setRange(). */ - SG_LOG(SG_IO, SG_ALERT, "_append is true so treating response code as success: " + SG_LOG(SG_IO, SG_DEBUG, "_append is true so treating response code as success: " << responseCode()); ok = true; } if (!ok) { - SG_LOG(SG_GENERAL, SG_ALERT, "failure. calling setFailure()." + SG_LOG(SG_GENERAL, SG_DEBUG, "failure. calling setFailure()." << " responseCode()=" << responseCode() << " responseReason()=" << responseReason() ); @@ -116,7 +117,7 @@ namespace HTTP _file.write(s, n); if (_callback) { - _callback(_callback_ref, s, n); + _callback(s, n); } } @@ -125,7 +126,7 @@ namespace HTTP { _file.close(); if (_callback) { - _callback(_callback_ref, nullptr, 0); + _callback(nullptr, 0); } } diff --git a/simgear/io/HTTPFileRequest.hxx b/simgear/io/HTTPFileRequest.hxx index 3c9c463e..422547e9 100644 --- a/simgear/io/HTTPFileRequest.hxx +++ b/simgear/io/HTTPFileRequest.hxx @@ -53,23 +53,18 @@ namespace HTTP FileRequest(const std::string& url, const std::string& path, bool append=false); /* - * Function pointer type for use with setCallback(). + * Set callback for each chunk of data we receive. Called with (nullptr, + * 0) when download has completed (successfully or unsuccesfully) - this + * will be done from inside setCallback() if the download has already + * finished. */ - typedef void (*Callback)(void* ref, const void* data, size_t numbytes); + void setCallback(std::function callbackf); - /* - * Set callback for each chunk of data we receive. Called with - * (ref, nullptr, 0) when download has completed (successfully or - * unsuccesfully). - */ - void setCallback(Callback callback, void* ref); - protected: SGPath _filename; sg_ofstream _file; bool _append; - Callback _callback; - void* _callback_ref; + std::function _callback; virtual void responseHeadersComplete(); virtual void gotBodyData(const char* s, int n);