Small change

This commit is contained in:
Federico Cecchetto 2022-02-17 20:01:40 +01:00
parent 0385697f5f
commit 4ab61d8242
3 changed files with 11 additions and 6 deletions

View File

@ -469,7 +469,7 @@ namespace ui_scripting::lua
::scheduler::once([url, id, dest]() ::scheduler::once([url, id, dest]()
{ {
auto last_report = std::chrono::high_resolution_clock::now(); auto last_report = std::chrono::high_resolution_clock::now();
const auto result = utils::http::get_data(url, {}, [&last_report, id](size_t progress, size_t total, float speed) const auto result = utils::http::get_data(url, {}, [&last_report, id](size_t progress, size_t total, size_t speed)
{ {
const auto now = std::chrono::high_resolution_clock::now(); const auto now = std::chrono::high_resolution_clock::now();
if (now - last_report < 100ms && progress < total) if (now - last_report < 100ms && progress < total)
@ -483,7 +483,12 @@ namespace ui_scripting::lua
{ {
event event; event event;
event.name = "http_request_progress"; event.name = "http_request_progress";
event.arguments = {id, static_cast<int>(progress), static_cast<int>(total), speed}; event.arguments = {
id,
static_cast<int>(progress),
static_cast<int>(total),
static_cast<int>(speed)
};
notify(event); notify(event);
}, ::scheduler::pipeline::lui); }, ::scheduler::pipeline::lui);

View File

@ -10,7 +10,7 @@ namespace utils::http
{ {
struct progress_helper struct progress_helper
{ {
const std::function<void(size_t, size_t, float)>* callback{}; const std::function<void(size_t, size_t, size_t)>* callback{};
std::exception_ptr exception{}; std::exception_ptr exception{};
std::chrono::high_resolution_clock::time_point start{}; std::chrono::high_resolution_clock::time_point start{};
}; };
@ -24,7 +24,7 @@ namespace utils::http
const auto now = std::chrono::high_resolution_clock::now(); const auto now = std::chrono::high_resolution_clock::now();
const auto count = std::chrono::duration_cast< const auto count = std::chrono::duration_cast<
std::chrono::milliseconds>(now - helper->start).count(); std::chrono::milliseconds>(now - helper->start).count();
const auto speed = (static_cast<float>(dlnow) / static_cast<float>(count)) * 1000.f; const auto speed = dlnow / count;
if (*helper->callback) if (*helper->callback)
{ {
@ -51,7 +51,7 @@ namespace utils::http
} }
std::optional<std::string> get_data(const std::string& url, const headers& headers, std::optional<std::string> get_data(const std::string& url, const headers& headers,
const std::function<void(size_t, size_t, float)>& callback) const std::function<void(size_t, size_t, size_t)>& callback)
{ {
curl_slist* header_list = nullptr; curl_slist* header_list = nullptr;
auto* curl = curl_easy_init(); auto* curl = curl_easy_init();

View File

@ -9,6 +9,6 @@ namespace utils::http
using headers = std::unordered_map<std::string, std::string>; using headers = std::unordered_map<std::string, std::string>;
std::optional<std::string> get_data(const std::string& url, const headers& headers = {}, std::optional<std::string> get_data(const std::string& url, const headers& headers = {},
const std::function<void(size_t, size_t, float)>& callback = {}); const std::function<void(size_t, size_t, size_t)>& callback = {});
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {}); std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {});
} }