added http::callback_progress() to cancel transfers quicker and more reliably

This commit is contained in:
tastytea 2018-05-20 12:29:04 +02:00
parent 6b47feb732
commit d342edfd21
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 35 additions and 10 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.13.0
VERSION 0.13.1
LANGUAGES CXX
)

View File

@ -12,6 +12,10 @@ There are [examples](https://github.com/tastytea/mastodon-cpp/tree/master/exampl
## Upgrading from below 0.10.0
You have to recompile all applications linking against this library.
## Upgrading from below 0.10.0
`Mastodon::API::get`, `::get_stream`, `::post`, `::put` and `::del` don't take
`std::string` as parameter to API-calls anymore, only `parametermap`s. The old behaviour is
still supported but is deprecated and will be removed in version 1.0.0.

View File

@ -84,7 +84,10 @@ const uint_fast16_t API::http::request(const method &meth,
request.setOpt<curlopts::HttpHeader>(headers);
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlopts::WriteFunction>
(std::bind(&http::callback, this, _1, _2, _3, &answer));
(std::bind(&http::callback_write, this, _1, _2, _3, &answer));
request.setOpt<curlopts::ProgressFunction>
(std::bind(&http::callback_progress, this, _1, _2, _3, _4));
request.setOpt<curlopts::NoProgress>(0);
if (!formdata.empty())
{
request.setOpt<curlopts::HttpPost>(formdata);
@ -138,7 +141,8 @@ const uint_fast16_t API::http::request(const method &meth,
catch (curlpp::RuntimeError &e)
{
// This error is thrown if http.cancel_stream() is used.
if (std::strncmp(e.what(), "Failed writing body", 19) == 0)
if ((std::strncmp(e.what(), "Callback aborted", 16) == 0) ||
(std::strncmp(e.what(), "Failed writing body", 19) == 0))
{
ttdebug << "Request was cancelled by user\n";
return 14;
@ -173,20 +177,32 @@ const void API::http::get_headers(string &headers) const
headers = _headers;
}
const size_t API::http::callback(char* data, size_t size, size_t nmemb,
string *str)
const size_t API::http::callback_write(char* data, size_t size, size_t nmemb,
string *str)
{
std::lock_guard<std::mutex> lock(_mutex);
if (_cancel_stream)
{
// This throws the runtime error: Failed writing body
return 0;
}
str->append(data, size * nmemb);
// ttdebug << "Received " << size * nmemb << " Bytes\n";
return size * nmemb;
};
const size_t API::http::callback(char* data, size_t size, size_t nmemb,
string *str)
{
return callback_write(data, size, nmemb, str);
};
double API::http::callback_progress(double /* dltotal */, double /* dlnow */,
double /* ultotal */, double /* ulnow */)
{
if (_cancel_stream)
{
// This throws the runtime error: Callback aborted
return 1;
}
return 0;
};
const void API::http::cancel_stream()
{
_cancel_stream = true;

View File

@ -154,8 +154,13 @@ public:
bool _cancel_stream;
std::mutex _mutex;
const size_t callback_write(char* data, size_t size, size_t nmemb,
string *oss);
[[deprecated("Will vanish in 1.0.0. Use callback_write() instead.")]]
const size_t callback(char* data, size_t size, size_t nmemb,
string *oss);
double callback_progress(double /* dltotal */, double /* dlnow */,
double /* ultotal */, double /* ulnow */);
};
/*!