summaryrefslogtreecommitdiff
path: root/core/io/stream_peer_tcp.cpp
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2018-09-12 18:25:28 +0200
committerGitHub <noreply@github.com>2018-09-12 18:25:28 +0200
commitcdc5264a3ef08c0e6805eb38d3f632ca21a5171e (patch)
tree12acf768a03838ee4721c1ededd2abd2f2793fca /core/io/stream_peer_tcp.cpp
parent06667b2fcfcf130a9b9e8974c1c7ac0c9ae1de0a (diff)
parent30327872e0d7989220a28a91a39d847dacca406d (diff)
Merge pull request #21692 from Faless/sockets_rebase
New NetSocket interface with common BSD/Win implementation
Diffstat (limited to 'core/io/stream_peer_tcp.cpp')
-rw-r--r--core/io/stream_peer_tcp.cpp297
1 files changed, 281 insertions, 16 deletions
diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp
index 54ebb3ae0d..484e160f0e 100644
--- a/core/io/stream_peer_tcp.cpp
+++ b/core/io/stream_peer_tcp.cpp
@@ -30,7 +30,280 @@
#include "stream_peer_tcp.h"
-StreamPeerTCP *(*StreamPeerTCP::_create)() = NULL;
+Error StreamPeerTCP::_poll_connection() {
+
+ ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED);
+
+ Error err = _sock->connect_to_host(peer_host, peer_port);
+
+ if (err == OK) {
+ status = STATUS_CONNECTED;
+ return OK;
+ } else if (err == ERR_BUSY) {
+ // Still trying to connect
+ return OK;
+ }
+
+ status = STATUS_ERROR;
+ return ERR_CONNECTION_ERROR;
+}
+
+void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint16_t p_port) {
+
+ _sock = p_sock;
+ _sock->set_blocking_enabled(false);
+
+ status = STATUS_CONNECTING;
+
+ peer_host = p_host;
+ peer_port = p_port;
+}
+
+Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, uint16_t p_port) {
+
+ ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
+ ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
+
+ Error err;
+ IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
+
+ err = _sock->open(NetSocket::TYPE_TCP, ip_type);
+ ERR_FAIL_COND_V(err != OK, FAILED);
+
+ _sock->set_blocking_enabled(false);
+
+ err = _sock->connect_to_host(p_host, p_port);
+
+ if (err != OK) {
+ if (err == ERR_BUSY) {
+ status = STATUS_CONNECTING;
+ } else {
+ ERR_PRINT("Connection to remote host failed!");
+ disconnect_from_host();
+ return FAILED;
+ }
+ }
+
+ status = STATUS_CONNECTED;
+ peer_host = p_host;
+ peer_port = p_port;
+
+ return OK;
+}
+
+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;
+
+ Error err;
+ int data_to_send = p_bytes;
+ const uint8_t *offset = p_data;
+ int total_sent = 0;
+
+ while (data_to_send) {
+
+ int sent_amount = 0;
+ err = _sock->send(offset, data_to_send, sent_amount);
+
+ if (err != OK) {
+
+ if (err != ERR_BUSY) {
+ disconnect_from_host();
+ return FAILED;
+ }
+
+ if (!p_block) {
+ r_sent = total_sent;
+ return OK;
+ }
+
+ // Block and wait for the socket to accept more data
+ err = _sock->poll(NetSocket::POLL_TYPE_OUT, -1);
+ if (err != OK) {
+ disconnect_from_host();
+ return FAILED;
+ }
+ } else {
+
+ data_to_send -= sent_amount;
+ offset += sent_amount;
+ total_sent += sent_amount;
+ }
+ }
+
+ r_sent = total_sent;
+
+ return OK;
+}
+
+Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
+
+ if (!is_connected_to_host()) {
+
+ 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;
+ r_received = 0;
+
+ while (to_read) {
+
+ int read = 0;
+ err = _sock->recv(p_buffer + total_read, to_read, read);
+
+ if (err != OK) {
+
+ if (err != ERR_BUSY) {
+ disconnect_from_host();
+ return FAILED;
+ }
+
+ if (!p_block) {
+ r_received = total_read;
+ return OK;
+ }
+
+ err = _sock->poll(NetSocket::POLL_TYPE_IN, -1);
+
+ if (err != OK) {
+ disconnect_from_host();
+ return FAILED;
+ }
+
+ } else if (read == 0) {
+
+ _sock->close();
+ status = STATUS_NONE;
+ peer_port = 0;
+ peer_host = IP_Address();
+ r_received = total_read;
+ return ERR_FILE_EOF;
+
+ } else {
+
+ to_read -= read;
+ total_read += read;
+ }
+ }
+
+ r_received = total_read;
+
+ return OK;
+}
+
+void StreamPeerTCP::set_no_delay(bool p_enabled) {
+
+ ERR_FAIL_COND(!is_connected_to_host());
+ _sock->set_tcp_no_delay_enabled(p_enabled);
+}
+
+bool StreamPeerTCP::is_connected_to_host() const {
+
+ if (status == STATUS_NONE || status == STATUS_ERROR) {
+
+ return false;
+ }
+
+ if (status != STATUS_CONNECTED) {
+ return true;
+ }
+
+ return _sock.is_valid() && _sock->is_open();
+}
+
+StreamPeerTCP::Status StreamPeerTCP::get_status() {
+
+ if (status == STATUS_CONNECTING) {
+ _poll_connection();
+ }
+
+ return status;
+}
+
+void StreamPeerTCP::disconnect_from_host() {
+
+ if (_sock.is_valid() && _sock->is_open())
+ _sock->close();
+
+ status = STATUS_NONE;
+ peer_host = IP_Address();
+ peer_port = 0;
+}
+
+Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) {
+
+ int total;
+ return write(p_data, p_bytes, total, true);
+}
+
+Error StreamPeerTCP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
+
+ return write(p_data, p_bytes, r_sent, false);
+}
+
+Error StreamPeerTCP::get_data(uint8_t *p_buffer, int p_bytes) {
+
+ int total;
+ return read(p_buffer, p_bytes, total, true);
+}
+
+Error StreamPeerTCP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
+
+ return read(p_buffer, p_bytes, r_received, false);
+}
+
+int StreamPeerTCP::get_available_bytes() const {
+
+ ERR_FAIL_COND_V(!_sock.is_valid(), -1);
+ return _sock->get_available_bytes();
+}
+
+IP_Address StreamPeerTCP::get_connected_host() const {
+
+ return peer_host;
+}
+
+uint16_t StreamPeerTCP::get_connected_port() const {
+
+ return peer_port;
+}
Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
@@ -62,23 +335,15 @@ void StreamPeerTCP::_bind_methods() {
BIND_ENUM_CONSTANT(STATUS_ERROR);
}
-Ref<StreamPeerTCP> StreamPeerTCP::create_ref() {
+StreamPeerTCP::StreamPeerTCP() {
- if (!_create)
- return Ref<StreamPeerTCP>();
- return Ref<StreamPeerTCP>(_create());
+ _sock = Ref<NetSocket>(NetSocket::create());
+ status = STATUS_NONE;
+ peer_host = IP_Address();
+ peer_port = 0;
}
-StreamPeerTCP *StreamPeerTCP::create() {
+StreamPeerTCP::~StreamPeerTCP() {
- if (!_create)
- return NULL;
- return _create();
+ disconnect_from_host();
}
-
-StreamPeerTCP::StreamPeerTCP() {
-}
-
-StreamPeerTCP::~StreamPeerTCP(){
-
-};