simgear/io/HTTPFileRequest.cxx: Reinstated std::function callback.

Also fixed setCallback() to call the supplied function immediately if transfer
has already finished.
This commit is contained in:
Julian Smith 2021-02-20 13:22:32 +00:00
parent 97828a8e4e
commit 3521ed44e4
2 changed files with 16 additions and 20 deletions

View File

@ -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<void(const void* data, size_t numbytes)> 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);
}
}

View File

@ -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);
/*
* 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);
void setCallback(std::function<void(const void* data, size_t numbytes)> callbackf);
protected:
SGPath _filename;
sg_ofstream _file;
bool _append;
Callback _callback;
void* _callback_ref;
std::function<void(const void* data, size_t numbytes)> _callback;
virtual void responseHeadersComplete();
virtual void gotBodyData(const char* s, int n);