Add thread support to HTTPRequest, changed assetlib to use it.

This commit is contained in:
Juan Linietsky 2016-07-24 16:09:43 -03:00
parent 85f365bbff
commit ab93fd1af9
3 changed files with 102 additions and 26 deletions

View File

@ -105,6 +105,9 @@ Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_h
} }
Error err = _parse_url(p_url); Error err = _parse_url(p_url);
if (err)
return err;
validate_ssl=p_ssl_validate_domain; validate_ssl=p_ssl_validate_domain;
bool has_user_agent=false; bool has_user_agent=false;
@ -127,19 +130,52 @@ Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_h
headers.push_back("Accept: */*"); headers.push_back("Accept: */*");
} }
requesting=true;
err = _request(); if (use_threads) {
thread_done=false;
thread_request_quit=false;
client->set_blocking_mode(true);
thread=Thread::create(_thread_func,this);
} else {
client->set_blocking_mode(false);
err = _request();
if (err!=OK) {
call_deferred("_request_done",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
return ERR_CANT_CONNECT;
}
if (err==OK) {
set_process(true); set_process(true);
requesting=true;
} }
return err; return OK;
} }
void HTTPRequest::_thread_func(void *p_userdata) {
HTTPRequest *hr = (HTTPRequest*)p_userdata;
Error err = hr->_request();
if (err!=OK) {
hr->call_deferred("_request_done",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
} else {
while(!hr->thread_request_quit) {
bool exit = hr->_update_connection();
if (exit)
break;
OS::get_singleton()->delay_usec(1);
}
}
hr->thread_done=true;
}
void HTTPRequest::cancel_request() { void HTTPRequest::cancel_request() {
if (!requesting) if (!requesting)
@ -147,6 +183,11 @@ void HTTPRequest::cancel_request() {
if (!use_threads) { if (!use_threads) {
set_process(false); set_process(false);
} else {
thread_request_quit=true;
Thread::wait_to_finish(thread);
memdelete(thread);
thread=NULL;
} }
if (file) { if (file) {
@ -167,7 +208,7 @@ void HTTPRequest::cancel_request() {
bool HTTPRequest::_handle_response(bool *ret_value) { bool HTTPRequest::_handle_response(bool *ret_value) {
if (!client->has_response()) { if (!client->has_response()) {
call_deferred("emit_signal","request_completed",RESULT_NO_RESPONSE,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_NO_RESPONSE,0,StringArray(),ByteArray());
*ret_value=true; *ret_value=true;
return true; return true;
} }
@ -187,7 +228,7 @@ bool HTTPRequest::_handle_response(bool *ret_value) {
//redirect //redirect
if (max_redirects>=0 && redirections>=max_redirects) { if (max_redirects>=0 && redirections>=max_redirects) {
call_deferred("emit_signal","request_completed",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray());
*ret_value=true; *ret_value=true;
return true; return true;
} }
@ -239,7 +280,8 @@ bool HTTPRequest::_update_connection() {
switch( client->get_status() ) { switch( client->get_status() ) {
case HTTPClient::STATUS_DISCONNECTED: { case HTTPClient::STATUS_DISCONNECTED: {
return true; //end it, since it's doing something call_deferred("_request_done",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
return true; //end it, since it's doing something
} break; } break;
case HTTPClient::STATUS_RESOLVING: { case HTTPClient::STATUS_RESOLVING: {
client->poll(); client->poll();
@ -247,7 +289,7 @@ bool HTTPRequest::_update_connection() {
return false; return false;
} break; } break;
case HTTPClient::STATUS_CANT_RESOLVE: { case HTTPClient::STATUS_CANT_RESOLVE: {
call_deferred("emit_signal","request_completed",RESULT_CANT_RESOLVE,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_CANT_RESOLVE,0,StringArray(),ByteArray());
return true; return true;
} break; } break;
@ -258,7 +300,7 @@ bool HTTPRequest::_update_connection() {
} break; //connecting to ip } break; //connecting to ip
case HTTPClient::STATUS_CANT_CONNECT: { case HTTPClient::STATUS_CANT_CONNECT: {
call_deferred("emit_signal","request_completed",RESULT_CANT_CONNECT,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
return true; return true;
} break; } break;
@ -276,17 +318,17 @@ bool HTTPRequest::_update_connection() {
return ret_value; return ret_value;
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_SUCCESS,response_code,response_headers,ByteArray());
return true; return true;
} }
if (got_response && body_len<0) { if (got_response && body_len<0) {
//chunked transfer is done //chunked transfer is done
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body); call_deferred("_request_done",RESULT_SUCCESS,response_code,response_headers,body);
return true; return true;
} }
call_deferred("emit_signal","request_completed",RESULT_CHUNKED_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_CHUNKED_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
return true; return true;
//request migh have been done //request migh have been done
} else { } else {
@ -294,7 +336,7 @@ bool HTTPRequest::_update_connection() {
Error err = client->request(HTTPClient::METHOD_GET,request_string,headers); Error err = client->request(HTTPClient::METHOD_GET,request_string,headers);
if (err!=OK) { if (err!=OK) {
call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
return true; return true;
} }
@ -320,7 +362,7 @@ bool HTTPRequest::_update_connection() {
if (!client->is_response_chunked() && client->get_response_body_length()==0) { if (!client->is_response_chunked() && client->get_response_body_length()==0) {
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_SUCCESS,response_code,response_headers,ByteArray());
return true; return true;
} }
@ -331,7 +373,7 @@ bool HTTPRequest::_update_connection() {
body_len=client->get_response_body_length(); body_len=client->get_response_body_length();
if (body_size_limit>=0 && body_len>body_size_limit) { if (body_size_limit>=0 && body_len>body_size_limit) {
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
return true; return true;
} }
} }
@ -340,7 +382,8 @@ bool HTTPRequest::_update_connection() {
file=FileAccess::open(download_to_file,FileAccess::WRITE); file=FileAccess::open(download_to_file,FileAccess::WRITE);
if (!file) { if (!file) {
call_deferred("emit_signal","request_completed",RESULT_DOWNLOAD_FILE_CANT_OPEN,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_DOWNLOAD_FILE_CANT_OPEN,response_code,response_headers,ByteArray());
return true;
} }
} }
} }
@ -356,7 +399,7 @@ bool HTTPRequest::_update_connection() {
ByteArray::Read r=chunk.read(); ByteArray::Read r=chunk.read();
file->store_buffer(r.ptr(),chunk.size()); file->store_buffer(r.ptr(),chunk.size());
if (file->get_error()!=OK) { if (file->get_error()!=OK) {
call_deferred("emit_signal","request_completed",RESULT_DOWNLOAD_FILE_WRITE_ERROR,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_DOWNLOAD_FILE_WRITE_ERROR,response_code,response_headers,ByteArray());
return true; return true;
} }
} else { } else {
@ -364,18 +407,18 @@ bool HTTPRequest::_update_connection() {
} }
if (body_size_limit>=0 && downloaded>body_size_limit) { if (body_size_limit>=0 && downloaded>body_size_limit) {
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
return true; return true;
} }
if (body_len>=0) { if (body_len>=0) {
if (downloaded==body_len) { if (downloaded==body_len) {
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body); call_deferred("_request_done",RESULT_SUCCESS,response_code,response_headers,body);
return true; return true;
} }
/*if (body.size()>=body_len) { /*if (body.size()>=body_len) {
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray()); call_deferred("_request_done",RESULT_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
return true; return true;
}*/ }*/
} }
@ -384,11 +427,11 @@ bool HTTPRequest::_update_connection() {
} break; // request resulted in body: { } break which must be read } break; // request resulted in body: { } break which must be read
case HTTPClient::STATUS_CONNECTION_ERROR: { case HTTPClient::STATUS_CONNECTION_ERROR: {
call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
return true; return true;
} break; } break;
case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: { case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
call_deferred("emit_signal","request_completed",RESULT_SSL_HANDSHAKE_ERROR,0,StringArray(),ByteArray()); call_deferred("_request_done",RESULT_SSL_HANDSHAKE_ERROR,0,StringArray(),ByteArray());
return true; return true;
} break; } break;
@ -397,17 +440,35 @@ bool HTTPRequest::_update_connection() {
ERR_FAIL_V(false); ERR_FAIL_V(false);
} }
void HTTPRequest::_request_done(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data) {
cancel_request();
emit_signal("request_completed",p_status,p_code,headers,p_data);
}
void HTTPRequest::_notification(int p_what) { void HTTPRequest::_notification(int p_what) {
if (p_what==NOTIFICATION_PROCESS) { if (p_what==NOTIFICATION_PROCESS) {
if (use_threads)
return;
bool done = _update_connection(); bool done = _update_connection();
if (done) { if (done) {
set_process(false); set_process(false);
//cancel_request(); called from _request done now
}
}
if (p_what==NOTIFICATION_EXIT_TREE) {
if (requesting) {
cancel_request(); cancel_request();
} }
} }
} }
void HTTPRequest::set_use_threads(bool p_use) { void HTTPRequest::set_use_threads(bool p_use) {
@ -491,6 +552,7 @@ void HTTPRequest::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_body_size"),&HTTPRequest::get_body_size); ObjectTypeDB::bind_method(_MD("get_body_size"),&HTTPRequest::get_body_size);
ObjectTypeDB::bind_method(_MD("_redirect_request"),&HTTPRequest::_redirect_request); ObjectTypeDB::bind_method(_MD("_redirect_request"),&HTTPRequest::_redirect_request);
ObjectTypeDB::bind_method(_MD("_request_done"),&HTTPRequest::_request_done);
ADD_PROPERTY(PropertyInfo(Variant::BOOL,"use_threads"),_SCS("set_use_threads"),_SCS("is_using_threads")); ADD_PROPERTY(PropertyInfo(Variant::BOOL,"use_threads"),_SCS("set_use_threads"),_SCS("is_using_threads"));
ADD_PROPERTY(PropertyInfo(Variant::INT,"body_size_limit",PROPERTY_HINT_RANGE,"-1,2000000000"),_SCS("set_body_size_limit"),_SCS("get_body_size_limit")); ADD_PROPERTY(PropertyInfo(Variant::INT,"body_size_limit",PROPERTY_HINT_RANGE,"-1,2000000000"),_SCS("set_body_size_limit"),_SCS("get_body_size_limit"));
@ -517,6 +579,7 @@ void HTTPRequest::_bind_methods() {
HTTPRequest::HTTPRequest() HTTPRequest::HTTPRequest()
{ {
thread=NULL;
port=80; port=80;
redirections=0; redirections=0;
@ -530,6 +593,7 @@ HTTPRequest::HTTPRequest()
requesting=false; requesting=false;
client.instance(); client.instance();
use_threads=false; use_threads=false;
thread_done=false;
body_size_limit=-1; body_size_limit=-1;
file=NULL; file=NULL;
status=HTTPClient::STATUS_DISCONNECTED; status=HTTPClient::STATUS_DISCONNECTED;

View File

@ -32,6 +32,7 @@
#include "node.h" #include "node.h"
#include "io/http_client.h" #include "io/http_client.h"
#include "os/file_access.h" #include "os/file_access.h"
#include "os/thread.h"
class HTTPRequest : public Node { class HTTPRequest : public Node {
@ -69,7 +70,7 @@ private:
bool request_sent; bool request_sent;
Ref<HTTPClient> client; Ref<HTTPClient> client;
ByteArray body; ByteArray body;
bool use_threads; volatile bool use_threads;
bool got_response; bool got_response;
int response_code; int response_code;
@ -80,7 +81,7 @@ private:
FileAccess *file; FileAccess *file;
int body_len; int body_len;
int downloaded; volatile int downloaded;
int body_size_limit; int body_size_limit;
int redirections; int redirections;
@ -93,11 +94,19 @@ private:
void _redirect_request(const String& p_new_url); void _redirect_request(const String& p_new_url);
bool _handle_response(bool *ret_value); bool _handle_response(bool *ret_value);
Error _parse_url(const String& p_url); Error _parse_url(const String& p_url);
Error _request(); Error _request();
volatile bool thread_done;
volatile bool thread_request_quit;
Thread *thread;
void _request_done(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data);
static void _thread_func(void *p_userdata);
protected: protected:

View File

@ -134,13 +134,13 @@ EditorAssetLibraryItem::EditorAssetLibraryItem() {
category = memnew( LinkButton ); category = memnew( LinkButton );
category->set_text("Editor Tools"); category->set_text("Editor Tools");
category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
title->connect("pressed",this,"_category_clicked"); category->connect("pressed",this,"_category_clicked");
vb->add_child(category); vb->add_child(category);
author = memnew( LinkButton ); author = memnew( LinkButton );
author->set_text("Johny Tolengo"); author->set_text("Johny Tolengo");
author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER); author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
title->connect("pressed",this,"_author_clicked"); author->connect("pressed",this,"_author_clicked");
vb->add_child(author); vb->add_child(author);
HBoxContainer *rating_hb = memnew( HBoxContainer ); HBoxContainer *rating_hb = memnew( HBoxContainer );
@ -461,6 +461,7 @@ void EditorAssetLibraryItemDownload::_install() {
void EditorAssetLibraryItemDownload::_make_request() { void EditorAssetLibraryItemDownload::_make_request() {
download->cancel_request(); download->cancel_request();
download->set_download_file(EditorSettings::get_singleton()->get_settings_path().plus_file("tmp").plus_file("tmp_asset_"+itos(asset_id))+".zip"); download->set_download_file(EditorSettings::get_singleton()->get_settings_path().plus_file("tmp").plus_file("tmp_asset_"+itos(asset_id))+".zip");
Error err = download->request(host); Error err = download->request(host);
if(err!=OK) { if(err!=OK) {
status->set_text("Error making request"); status->set_text("Error making request");
@ -1011,6 +1012,7 @@ void EditorAssetLibrary::_api_request(const String& p_request, RequestType p_req
if (requesting!=REQUESTING_NONE) { if (requesting!=REQUESTING_NONE) {
request->cancel_request(); request->cancel_request();
} }
requesting=p_request_type; requesting=p_request_type;
error_hb->hide(); error_hb->hide();
@ -1469,6 +1471,7 @@ EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
request = memnew( HTTPRequest ); request = memnew( HTTPRequest );
add_child(request); add_child(request);
request->set_use_threads(EDITOR_DEF("asset_library/use_threads",true));
request->connect("request_completed",this,"_http_request_completed"); request->connect("request_completed",this,"_http_request_completed");
last_queue_id=0; last_queue_id=0;