diff options
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/http_request.cpp | 34 | ||||
-rw-r--r-- | scene/main/http_request.h | 9 |
2 files changed, 22 insertions, 21 deletions
diff --git a/scene/main/http_request.cpp b/scene/main/http_request.cpp index 384e7d2652..ce7d6ef13c 100644 --- a/scene/main/http_request.cpp +++ b/scene/main/http_request.cpp @@ -49,7 +49,7 @@ Error HTTPRequest::_parse_url(const String &p_url) { got_response = false; body_len = -1; body.resize(0); - downloaded = 0; + downloaded.set(0); redirections = 0; String url_lower = url.to_lower(); @@ -159,9 +159,9 @@ Error HTTPRequest::request_raw(const String &p_url, const Vector<String> &p_cust requesting = true; - if (use_threads) { - thread_done = false; - thread_request_quit = false; + if (use_threads.is_set()) { + thread_done.clear(); + thread_request_quit.clear(); client->set_blocking_mode(true); thread.start(_thread_func, this); } else { @@ -186,7 +186,7 @@ void HTTPRequest::_thread_func(void *p_userdata) { if (err != OK) { hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray()); } else { - while (!hr->thread_request_quit) { + while (!hr->thread_request_quit.is_set()) { bool exit = hr->_update_connection(); if (exit) { break; @@ -195,7 +195,7 @@ void HTTPRequest::_thread_func(void *p_userdata) { } } - hr->thread_done = true; + hr->thread_done.set(); } void HTTPRequest::cancel_request() { @@ -205,10 +205,10 @@ void HTTPRequest::cancel_request() { return; } - if (!use_threads) { + if (!use_threads.is_set()) { set_process_internal(false); } else { - thread_request_quit = true; + thread_request_quit.set(); thread.wait_to_finish(); } @@ -236,7 +236,7 @@ bool HTTPRequest::_handle_response(bool *ret_value) { List<String> rheaders; client->get_response_headers(&rheaders); response_headers.resize(0); - downloaded = 0; + downloaded.set(0); for (List<String>::Element *E = rheaders.front(); E; E = E->next()) { response_headers.push_back(E->get()); } @@ -276,7 +276,7 @@ bool HTTPRequest::_handle_response(bool *ret_value) { got_response = false; body_len = -1; body.resize(0); - downloaded = 0; + downloaded.set(0); redirections = new_redirs; *ret_value = false; return true; @@ -389,7 +389,7 @@ bool HTTPRequest::_update_connection() { client->poll(); PackedByteArray chunk = client->read_response_body_chunk(); - downloaded += chunk.size(); + downloaded.add(chunk.size()); if (file) { const uint8_t *r = chunk.ptr(); @@ -402,13 +402,13 @@ bool HTTPRequest::_update_connection() { body.append_array(chunk); } - if (body_size_limit >= 0 && downloaded > body_size_limit) { + if (body_size_limit >= 0 && downloaded.get() > body_size_limit) { call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray()); return true; } if (body_len >= 0) { - if (downloaded == body_len) { + if (downloaded.get() == body_len) { call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body); return true; } @@ -478,7 +478,7 @@ void HTTPRequest::_request_done(int p_status, int p_code, const PackedStringArra void HTTPRequest::_notification(int p_what) { if (p_what == NOTIFICATION_INTERNAL_PROCESS) { - if (use_threads) { + if (use_threads.is_set()) { return; } bool done = _update_connection(); @@ -497,11 +497,11 @@ void HTTPRequest::_notification(int p_what) { void HTTPRequest::set_use_threads(bool p_use) { ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); - use_threads = p_use; + use_threads.set_to(p_use); } bool HTTPRequest::is_using_threads() const { - return use_threads; + return use_threads.is_set(); } void HTTPRequest::set_accept_gzip(bool p_gzip) { @@ -555,7 +555,7 @@ int HTTPRequest::get_max_redirects() const { } int HTTPRequest::get_downloaded_bytes() const { - return downloaded; + return downloaded.get(); } int HTTPRequest::get_body_size() const { diff --git a/scene/main/http_request.h b/scene/main/http_request.h index 5525ea7912..92b0ff28e9 100644 --- a/scene/main/http_request.h +++ b/scene/main/http_request.h @@ -34,6 +34,7 @@ #include "core/io/http_client.h" #include "core/os/file_access.h" #include "core/os/thread.h" +#include "core/templates/safe_refcount.h" #include "node.h" #include "scene/main/timer.h" @@ -74,7 +75,7 @@ private: bool request_sent = false; Ref<HTTPClient> client; PackedByteArray body; - volatile bool use_threads = false; + SafeFlag use_threads; bool accept_gzip = true; bool got_response = false; @@ -86,7 +87,7 @@ private: FileAccess *file = nullptr; int body_len = -1; - volatile int downloaded = 0; + SafeNumeric<int> downloaded; int body_size_limit = -1; int redirections = 0; @@ -107,8 +108,8 @@ private: bool has_header(const PackedStringArray &p_headers, const String &p_header_name); String get_header_value(const PackedStringArray &p_headers, const String &header_name); - volatile bool thread_done = false; - volatile bool thread_request_quit = false; + SafeFlag thread_done; + SafeFlag thread_request_quit; Thread thread; |