summaryrefslogtreecommitdiff
path: root/core/io/stream_peer_tcp.cpp
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-05-14 23:09:03 +0200
committerGitHub <noreply@github.com>2020-05-14 23:09:03 +0200
commit00949f0c5fcc6a4f8382a4a97d5591fd9ec380f8 (patch)
tree2b1c31f45add24085b64425ce440f577424c16a1 /core/io/stream_peer_tcp.cpp
parent5046f666a1181675b39f156c38346525dc1c444e (diff)
parent0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 (diff)
Merge pull request #38738 from akien-mga/cause-we-never-go-out-of-style
Style: Remove new line at block start, enforce line between functions, enforce braces in if and loop blocks
Diffstat (limited to 'core/io/stream_peer_tcp.cpp')
-rw-r--r--core/io/stream_peer_tcp.cpp41
1 files changed, 6 insertions, 35 deletions
diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp
index 6218b98758..cce728c30a 100644
--- a/core/io/stream_peer_tcp.cpp
+++ b/core/io/stream_peer_tcp.cpp
@@ -33,7 +33,6 @@
#include "core/project_settings.h"
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);
@@ -58,7 +57,6 @@ Error StreamPeerTCP::_poll_connection() {
}
void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint16_t p_port) {
-
_sock = p_sock;
_sock->set_blocking_enabled(false);
@@ -70,7 +68,6 @@ void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint
}
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);
@@ -103,18 +100,14 @@ Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, uint16_t 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;
}
@@ -124,8 +117,9 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool
}
}
- if (!_sock->is_open())
+ if (!_sock->is_open()) {
return FAILED;
+ }
Error err;
int data_to_send = p_bytes;
@@ -133,12 +127,10 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool
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;
@@ -156,7 +148,6 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool
return FAILED;
}
} else {
-
data_to_send -= sent_amount;
offset += sent_amount;
total_sent += sent_amount;
@@ -169,16 +160,12 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool
}
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;
}
@@ -194,12 +181,10 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool
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;
@@ -218,13 +203,11 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool
}
} else if (read == 0) {
-
disconnect_from_host();
r_received = total_read;
return ERR_FILE_EOF;
} else {
-
to_read -= read;
total_read += read;
@@ -241,18 +224,15 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool
}
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 {
-
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) {
@@ -278,9 +258,9 @@ StreamPeerTCP::Status StreamPeerTCP::get_status() {
}
void StreamPeerTCP::disconnect_from_host() {
-
- if (_sock.is_valid() && _sock->is_open())
+ if (_sock.is_valid() && _sock->is_open()) {
_sock->close();
+ }
timeout = 0;
status = STATUS_NONE;
@@ -294,59 +274,51 @@ Error StreamPeerTCP::poll(NetSocket::PollType p_type, int timeout) {
}
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) {
-
IP_Address ip;
if (p_address.is_valid_ip_address()) {
ip = p_address;
} else {
ip = IP::get_singleton()->resolve_hostname(p_address);
- if (!ip.is_valid())
+ if (!ip.is_valid()) {
return ERR_CANT_RESOLVE;
+ }
}
return connect_to_host(ip, p_port);
}
void StreamPeerTCP::_bind_methods() {
-
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("get_status"), &StreamPeerTCP::get_status);
@@ -366,6 +338,5 @@ StreamPeerTCP::StreamPeerTCP() :
}
StreamPeerTCP::~StreamPeerTCP() {
-
disconnect_from_host();
}