diff options
author | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2022-03-27 15:30:17 +0200 |
---|---|---|
committer | Fabio Alessandrelli <fabio.alessandrelli@gmail.com> | 2022-03-27 16:36:44 +0200 |
commit | 0e528676683a0df96751fd0daa018e0818baa0e2 (patch) | |
tree | a8e9e3707ea37a6431a6aac90b7c91a31ee8b567 /core/io | |
parent | a5eed70fa2edcf755d6abea2077232e38381449b (diff) |
[Net] Make StreamPeerTCP::_poll_connection explicit.
No longer hacked into `get_status` and renamed to `poll`.
The old `poll` (for *nix `poll`, win `select`) is now called `wait`.
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/http_client_tcp.cpp | 3 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.cpp | 76 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.h | 10 |
3 files changed, 34 insertions, 55 deletions
diff --git a/core/io/http_client_tcp.cpp b/core/io/http_client_tcp.cpp index f920799677..d983d86b99 100644 --- a/core/io/http_client_tcp.cpp +++ b/core/io/http_client_tcp.cpp @@ -264,6 +264,9 @@ void HTTPClientTCP::close() { } Error HTTPClientTCP::poll() { + if (tcp_connection.is_valid()) { + tcp_connection->poll(); + } switch (status) { case STATUS_RESOLVING: { ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG); diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 6d5784a8ab..6c3313b73b 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -32,8 +32,28 @@ #include "core/config/project_settings.h" -Error StreamPeerTCP::_poll_connection() { - ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED); +Error StreamPeerTCP::poll() { + if (status == STATUS_CONNECTED) { + Error err; + err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); + if (err == OK) { + // FIN received + if (_sock->get_available_bytes() == 0) { + disconnect_from_host(); + return OK; + } + } + // Also poll write + err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0); + if (err != OK && err != ERR_BUSY) { + // Got an error + disconnect_from_host(); + status = STATUS_ERROR; + return err; + } + } else if (status != STATUS_CONNECTING) { + return OK; + } Error err = _sock->connect_to_host(peer_host, peer_port); @@ -121,22 +141,7 @@ Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) { Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); - if (status == STATUS_NONE || status == STATUS_ERROR) { - return FAILED; - } - if (status != STATUS_CONNECTED) { - if (_poll_connection() != OK) { - return FAILED; - } - - if (status != STATUS_CONNECTED) { - r_sent = 0; - return OK; - } - } - - if (!_sock->is_open()) { return FAILED; } @@ -183,17 +188,6 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool return FAILED; } - if (status == STATUS_CONNECTING) { - if (_poll_connection() != OK) { - return FAILED; - } - - if (status != STATUS_CONNECTED) { - r_received = 0; - return OK; - } - } - Error err; int to_read = p_bytes; int total_read = 0; @@ -251,28 +245,7 @@ bool StreamPeerTCP::is_connected_to_host() const { return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING); } -StreamPeerTCP::Status StreamPeerTCP::get_status() { - if (status == STATUS_CONNECTING) { - _poll_connection(); - } else if (status == STATUS_CONNECTED) { - Error err; - err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); - if (err == OK) { - // FIN received - if (_sock->get_available_bytes() == 0) { - disconnect_from_host(); - return status; - } - } - // Also poll write - err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0); - if (err != OK && err != ERR_BUSY) { - // Got an error - disconnect_from_host(); - status = STATUS_ERROR; - } - } - +StreamPeerTCP::Status StreamPeerTCP::get_status() const { return status; } @@ -287,7 +260,7 @@ void StreamPeerTCP::disconnect_from_host() { peer_port = 0; } -Error StreamPeerTCP::poll(NetSocket::PollType p_type, int timeout) { +Error StreamPeerTCP::wait(NetSocket::PollType p_type, int timeout) { ERR_FAIL_COND_V(_sock.is_null() || !_sock->is_open(), ERR_UNAVAILABLE); return _sock->poll(p_type, timeout); } @@ -347,6 +320,7 @@ void StreamPeerTCP::_bind_methods() { ClassDB::bind_method(D_METHOD("bind", "port", "host"), &StreamPeerTCP::bind, DEFVAL("*")); ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect); ClassDB::bind_method(D_METHOD("is_connected_to_host"), &StreamPeerTCP::is_connected_to_host); + ClassDB::bind_method(D_METHOD("poll"), &StreamPeerTCP::poll); ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTCP::get_status); ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host); ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port); diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index f2c47b25cf..5fe1734faa 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -56,7 +56,6 @@ protected: uint16_t peer_port = 0; Error _connect(const String &p_address, int p_port); - Error _poll_connection(); Error write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block); Error read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block); @@ -74,12 +73,15 @@ public: void disconnect_from_host(); int get_available_bytes() const override; - Status get_status(); + Status get_status() const; void set_no_delay(bool p_enabled); - // Poll functions (wait or check for writable, readable) - Error poll(NetSocket::PollType p_type, int timeout = 0); + // Poll socket updating its state. + Error poll(); + + // Wait or check for writable, readable. + Error wait(NetSocket::PollType p_type, int timeout = 0); // Read/Write from StreamPeer Error put_data(const uint8_t *p_data, int p_bytes) override; |