summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-07-09 14:50:12 +0200
committerGitHub <noreply@github.com>2019-07-09 14:50:12 +0200
commit7f80c1dca8892cab4f4fe7a2daf6699755024f09 (patch)
tree0d2cd4984475be69e6153997a3524b1a285ae316
parent8d6a95347537c5e3084f269bdf98d1128b67b29b (diff)
parent5fe5c4e8f11b683dc60c6ec992ed89f93e898fba (diff)
Merge pull request #30315 from zaksnet/httprequest-add-timeout
Add optional timeout to HTTPRequest
-rw-r--r--scene/main/http_request.cpp37
-rw-r--r--scene/main/http_request.h13
2 files changed, 49 insertions, 1 deletions
diff --git a/scene/main/http_request.cpp b/scene/main/http_request.cpp
index 88b942ee45..05bd911014 100644
--- a/scene/main/http_request.cpp
+++ b/scene/main/http_request.cpp
@@ -96,6 +96,11 @@ Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_h
ERR_FAIL_V(ERR_BUSY);
}
+ if (timeout > 0) {
+ timer->stop();
+ timer->start(timeout);
+ }
+
method = p_method;
Error err = _parse_url(p_url);
@@ -153,6 +158,8 @@ void HTTPRequest::_thread_func(void *p_userdata) {
void HTTPRequest::cancel_request() {
+ timer->stop();
+
if (!requesting)
return;
@@ -479,6 +486,23 @@ int HTTPRequest::get_body_size() const {
return body_len;
}
+void HTTPRequest::set_timeout(int p_timeout) {
+
+ ERR_FAIL_COND(p_timeout < 0);
+ timeout = p_timeout;
+}
+
+int HTTPRequest::get_timeout() {
+
+ return timeout;
+}
+
+void HTTPRequest::_timeout() {
+
+ cancel_request();
+ call_deferred("_request_done", RESULT_TIMEOUT, 0, PoolStringArray(), PoolByteArray());
+}
+
void HTTPRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "ssl_validate_domain", "method", "request_data"), &HTTPRequest::request, DEFVAL(PoolStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
@@ -504,10 +528,16 @@ void HTTPRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("_redirect_request"), &HTTPRequest::_redirect_request);
ClassDB::bind_method(D_METHOD("_request_done"), &HTTPRequest::_request_done);
+ ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
+ ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
+
+ ClassDB::bind_method(D_METHOD("_timeout"), &HTTPRequest::_timeout);
+
ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000"), "set_body_size_limit", "get_body_size_limit");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "timeout", PROPERTY_HINT_RANGE, "0,86400"), "set_timeout", "get_timeout");
ADD_SIGNAL(MethodInfo("request_completed", PropertyInfo(Variant::INT, "result"), PropertyInfo(Variant::INT, "response_code"), PropertyInfo(Variant::POOL_STRING_ARRAY, "headers"), PropertyInfo(Variant::POOL_BYTE_ARRAY, "body")));
@@ -524,6 +554,7 @@ void HTTPRequest::_bind_methods() {
BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_CANT_OPEN);
BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_WRITE_ERROR);
BIND_ENUM_CONSTANT(RESULT_REDIRECT_LIMIT_REACHED);
+ BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
}
HTTPRequest::HTTPRequest() {
@@ -546,6 +577,12 @@ HTTPRequest::HTTPRequest() {
downloaded = 0;
body_size_limit = -1;
file = NULL;
+
+ timer = memnew(Timer);
+ timer->set_one_shot(true);
+ timer->connect("timeout", this, "_timeout");
+ add_child(timer);
+ timeout = 0;
}
HTTPRequest::~HTTPRequest() {
diff --git a/scene/main/http_request.h b/scene/main/http_request.h
index 2e58d579ba..f1f91235a6 100644
--- a/scene/main/http_request.h
+++ b/scene/main/http_request.h
@@ -35,6 +35,7 @@
#include "core/os/file_access.h"
#include "core/os/thread.h"
#include "node.h"
+#include "scene/main/timer.h"
class HTTPRequest : public Node {
@@ -53,7 +54,8 @@ public:
RESULT_REQUEST_FAILED,
RESULT_DOWNLOAD_FILE_CANT_OPEN,
RESULT_DOWNLOAD_FILE_WRITE_ERROR,
- RESULT_REDIRECT_LIMIT_REACHED
+ RESULT_REDIRECT_LIMIT_REACHED,
+ RESULT_TIMEOUT
};
@@ -92,6 +94,8 @@ private:
int max_redirects;
+ int timeout;
+
void _redirect_request(const String &p_new_url);
bool _handle_response(bool *ret_value);
@@ -128,6 +132,13 @@ public:
void set_max_redirects(int p_max);
int get_max_redirects() const;
+ Timer *timer;
+
+ void set_timeout(int p_timeout);
+ int get_timeout();
+
+ void _timeout();
+
int get_downloaded_bytes() const;
int get_body_size() const;