Browse Source

Merge pull request #33862 from Faless/net/http_request_chunk_size

Add download_chunk_size property to HTTPRequest.
Rémi Verschelde 5 years ago
parent
commit
967cc2c014

+ 6 - 0
core/io/http_client.cpp

@@ -715,6 +715,10 @@ void HTTPClient::set_read_chunk_size(int p_size) {
 	read_chunk_size = p_size;
 }
 
+int HTTPClient::get_read_chunk_size() const {
+	return read_chunk_size;
+}
+
 HTTPClient::HTTPClient() {
 
 	tcp_connection.instance();
@@ -818,6 +822,7 @@ void HTTPClient::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_response_body_length"), &HTTPClient::get_response_body_length);
 	ClassDB::bind_method(D_METHOD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk);
 	ClassDB::bind_method(D_METHOD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size);
+	ClassDB::bind_method(D_METHOD("get_read_chunk_size"), &HTTPClient::get_read_chunk_size);
 
 	ClassDB::bind_method(D_METHOD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode);
 	ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled);
@@ -829,6 +834,7 @@ void HTTPClient::_bind_methods() {
 
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", 0), "set_connection", "get_connection");
+	ADD_PROPERTY(PropertyInfo(Variant::INT, "read_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_read_chunk_size", "get_read_chunk_size");
 
 	BIND_ENUM_CONSTANT(METHOD_GET);
 	BIND_ENUM_CONSTANT(METHOD_HEAD);

+ 1 - 0
core/io/http_client.h

@@ -220,6 +220,7 @@ public:
 	bool is_blocking_mode_enabled() const;
 
 	void set_read_chunk_size(int p_size);
+	int get_read_chunk_size() const;
 
 	Error poll();
 

+ 3 - 9
doc/classes/HTTPClient.xml

@@ -170,15 +170,6 @@
 				Sends the body data raw, as a byte array and does not encode it in any way.
 			</description>
 		</method>
-		<method name="set_read_chunk_size">
-			<return type="void">
-			</return>
-			<argument index="0" name="bytes" type="int">
-			</argument>
-			<description>
-				Sets the size of the buffer used and maximum bytes to read per iteration. See [method read_response_body_chunk].
-			</description>
-		</method>
 	</methods>
 	<members>
 		<member name="blocking_mode_enabled" type="bool" setter="set_blocking_mode" getter="is_blocking_mode_enabled" default="false">
@@ -187,6 +178,9 @@
 		<member name="connection" type="StreamPeer" setter="set_connection" getter="get_connection">
 			The connection to use for this client.
 		</member>
+		<member name="read_chunk_size" type="int" setter="set_read_chunk_size" getter="get_read_chunk_size" default="4096">
+			The size of the buffer used and maximum bytes to read per iteration. See [method read_response_body_chunk].
+		</member>
 	</members>
 	<constants>
 		<constant name="METHOD_GET" value="0" enum="Method">

+ 4 - 0
doc/classes/HTTPRequest.xml

@@ -93,6 +93,10 @@
 		<member name="body_size_limit" type="int" setter="set_body_size_limit" getter="get_body_size_limit" default="-1">
 			Maximum allowed size for response bodies.
 		</member>
+		<member name="download_chunk_size" type="int" setter="set_download_chunk_size" getter="get_download_chunk_size" default="4096">
+			The size of the buffer used and maximum bytes to read per iteration. See [member HTTPClient.read_chunk_size].
+			Set this to a higher value (e.g. 65536 for 64 KiB) when downloading large files to achieve better speeds at the cost of memory.
+		</member>
 		<member name="download_file" type="String" setter="set_download_file" getter="get_download_file" default="&quot;&quot;">
 			The file to download into. Will output any received file into it.
 		</member>

+ 4 - 0
platform/javascript/http_client_javascript.cpp

@@ -211,6 +211,10 @@ void HTTPClient::set_read_chunk_size(int p_size) {
 	read_limit = p_size;
 }
 
+int HTTPClient::get_read_chunk_size() const {
+	return read_limit;
+}
+
 Error HTTPClient::poll() {
 
 	switch (status) {

+ 16 - 0
scene/main/http_request.cpp

@@ -457,6 +457,18 @@ String HTTPRequest::get_download_file() const {
 
 	return download_to_file;
 }
+
+void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
+
+	ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
+
+	client->set_read_chunk_size(p_chunk_size);
+}
+
+int HTTPRequest::get_download_chunk_size() const {
+	return client->get_read_chunk_size();
+}
+
 HTTPClient::Status HTTPRequest::get_http_client_status() const {
 	return client->get_status();
 }
@@ -524,9 +536,13 @@ void HTTPRequest::_bind_methods() {
 	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("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size);
+	ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
+
 	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::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_download_chunk_size", "get_download_chunk_size");
 	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");

+ 3 - 0
scene/main/http_request.h

@@ -126,6 +126,9 @@ public:
 	void set_download_file(const String &p_file);
 	String get_download_file() const;
 
+	void set_download_chunk_size(int p_chunk_size);
+	int get_download_chunk_size() const;
+
 	void set_body_size_limit(int p_bytes);
 	int get_body_size_limit() const;