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:
parent
97828a8e4e
commit
3521ed44e4
@ -30,9 +30,7 @@ namespace HTTP
|
|||||||
FileRequest::FileRequest(const std::string& url, const std::string& path, bool append):
|
FileRequest::FileRequest(const std::string& url, const std::string& path, bool append):
|
||||||
Request(url, "GET"),
|
Request(url, "GET"),
|
||||||
_filename(path),
|
_filename(path),
|
||||||
_append(append),
|
_append(append)
|
||||||
_callback(nullptr),
|
|
||||||
_callback_ref(nullptr)
|
|
||||||
{
|
{
|
||||||
if (append && _filename.isFile()) {
|
if (append && _filename.isFile()) {
|
||||||
size_t size = _filename.sizeInBytes();
|
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 = 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)) {
|
else if (_append && (responseCode() == 206 || responseCode() == 416)) {
|
||||||
/* See comments for simgear::HTTP::Client::setRange(). */
|
/* 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());
|
<< responseCode());
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
SG_LOG(SG_GENERAL, SG_ALERT, "failure. calling setFailure()."
|
SG_LOG(SG_GENERAL, SG_DEBUG, "failure. calling setFailure()."
|
||||||
<< " responseCode()=" << responseCode()
|
<< " responseCode()=" << responseCode()
|
||||||
<< " responseReason()=" << responseReason()
|
<< " responseReason()=" << responseReason()
|
||||||
);
|
);
|
||||||
@ -116,7 +117,7 @@ namespace HTTP
|
|||||||
|
|
||||||
_file.write(s, n);
|
_file.write(s, n);
|
||||||
if (_callback) {
|
if (_callback) {
|
||||||
_callback(_callback_ref, s, n);
|
_callback(s, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +126,7 @@ namespace HTTP
|
|||||||
{
|
{
|
||||||
_file.close();
|
_file.close();
|
||||||
if (_callback) {
|
if (_callback) {
|
||||||
_callback(_callback_ref, nullptr, 0);
|
_callback(nullptr, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,23 +53,18 @@ namespace HTTP
|
|||||||
FileRequest(const std::string& url, const std::string& path, bool append=false);
|
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<void(const void* data, size_t numbytes)> 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:
|
protected:
|
||||||
SGPath _filename;
|
SGPath _filename;
|
||||||
sg_ofstream _file;
|
sg_ofstream _file;
|
||||||
bool _append;
|
bool _append;
|
||||||
Callback _callback;
|
std::function<void(const void* data, size_t numbytes)> _callback;
|
||||||
void* _callback_ref;
|
|
||||||
|
|
||||||
virtual void responseHeadersComplete();
|
virtual void responseHeadersComplete();
|
||||||
virtual void gotBodyData(const char* s, int n);
|
virtual void gotBodyData(const char* s, int n);
|
||||||
|
Loading…
Reference in New Issue
Block a user