mirror of
https://github.com/godotengine/godot.git
synced 2024-10-23 05:31:54 +00:00
[Net] Non-blocking request in HTTPClientTCP.
HTTPClientJavaScript already supports non-blocking requests.
This commit is contained in:
parent
6de5bafd2f
commit
ac4fb2996b
|
@ -197,20 +197,12 @@ Error HTTPClientTCP::request(Method p_method, const String &p_url, const Vector<
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
CharString cs = request.utf8();
|
CharString cs = request.utf8();
|
||||||
|
|
||||||
Vector<uint8_t> data;
|
request_buffer->clear();
|
||||||
data.resize(cs.length() + p_body_size);
|
request_buffer->put_data((const uint8_t *)cs.get_data(), cs.length());
|
||||||
memcpy(data.ptrw(), cs.get_data(), cs.length());
|
|
||||||
if (p_body_size > 0) {
|
if (p_body_size > 0) {
|
||||||
memcpy(data.ptrw() + cs.length(), p_body, p_body_size);
|
request_buffer->put_data(p_body, p_body_size);
|
||||||
}
|
|
||||||
|
|
||||||
err = connection->put_data(data.ptr(), data.size());
|
|
||||||
|
|
||||||
if (err) {
|
|
||||||
close();
|
|
||||||
status = STATUS_CONNECTION_ERROR;
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
request_buffer->seek(0);
|
||||||
|
|
||||||
status = STATUS_REQUESTING;
|
status = STATUS_REQUESTING;
|
||||||
head_request = p_method == METHOD_HEAD;
|
head_request = p_method == METHOD_HEAD;
|
||||||
|
@ -261,6 +253,7 @@ void HTTPClientTCP::close() {
|
||||||
ip_candidates.clear();
|
ip_candidates.clear();
|
||||||
response_headers.clear();
|
response_headers.clear();
|
||||||
response_str.clear();
|
response_str.clear();
|
||||||
|
request_buffer->clear();
|
||||||
body_size = -1;
|
body_size = -1;
|
||||||
body_left = 0;
|
body_left = 0;
|
||||||
chunk_left = 0;
|
chunk_left = 0;
|
||||||
|
@ -436,6 +429,30 @@ Error HTTPClientTCP::poll() {
|
||||||
return OK;
|
return OK;
|
||||||
} break;
|
} break;
|
||||||
case STATUS_REQUESTING: {
|
case STATUS_REQUESTING: {
|
||||||
|
if (request_buffer->get_available_bytes()) {
|
||||||
|
int avail = request_buffer->get_available_bytes();
|
||||||
|
int pos = request_buffer->get_position();
|
||||||
|
const Vector<uint8_t> data = request_buffer->get_data_array();
|
||||||
|
int wrote = 0;
|
||||||
|
Error err;
|
||||||
|
if (blocking) {
|
||||||
|
err = connection->put_data(data.ptr() + pos, avail);
|
||||||
|
wrote += avail;
|
||||||
|
} else {
|
||||||
|
err = connection->put_partial_data(data.ptr() + pos, avail, wrote);
|
||||||
|
}
|
||||||
|
if (err != OK) {
|
||||||
|
close();
|
||||||
|
status = STATUS_CONNECTION_ERROR;
|
||||||
|
return ERR_CONNECTION_ERROR;
|
||||||
|
}
|
||||||
|
pos += wrote;
|
||||||
|
request_buffer->seek(pos);
|
||||||
|
if (avail - wrote > 0) {
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
request_buffer->clear();
|
||||||
|
}
|
||||||
while (true) {
|
while (true) {
|
||||||
uint8_t byte;
|
uint8_t byte;
|
||||||
int rec = 0;
|
int rec = 0;
|
||||||
|
@ -763,6 +780,7 @@ void HTTPClientTCP::set_https_proxy(const String &p_host, int p_port) {
|
||||||
|
|
||||||
HTTPClientTCP::HTTPClientTCP() {
|
HTTPClientTCP::HTTPClientTCP() {
|
||||||
tcp_connection.instantiate();
|
tcp_connection.instantiate();
|
||||||
|
request_buffer.instantiate();
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPClient *(*HTTPClient::_create)() = HTTPClientTCP::_create_func;
|
HTTPClient *(*HTTPClient::_create)() = HTTPClientTCP::_create_func;
|
||||||
|
|
|
@ -62,6 +62,7 @@ private:
|
||||||
int64_t body_left = 0;
|
int64_t body_left = 0;
|
||||||
bool read_until_eof = false;
|
bool read_until_eof = false;
|
||||||
|
|
||||||
|
Ref<StreamPeerBuffer> request_buffer;
|
||||||
Ref<StreamPeerTCP> tcp_connection;
|
Ref<StreamPeerTCP> tcp_connection;
|
||||||
Ref<StreamPeer> connection;
|
Ref<StreamPeer> connection;
|
||||||
Ref<HTTPClientTCP> proxy_client; // Negotiate with proxy server.
|
Ref<HTTPClientTCP> proxy_client; // Negotiate with proxy server.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user