| 1 | /**************************************************************************/ |
| 2 | /* stream_peer_socket.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "stream_peer_socket.h" |
| 32 | #include "stream_peer_socket.compat.inc" |
| 33 | |
| 34 | Error StreamPeerSocket::poll() { |
| 35 | if (status == STATUS_CONNECTED) { |
| 36 | Error err; |
| 37 | err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); |
| 38 | if (err == OK) { |
| 39 | // FIN received |
| 40 | if (_sock->get_available_bytes() == 0) { |
| 41 | disconnect_from_host(); |
| 42 | return OK; |
| 43 | } |
| 44 | } |
| 45 | // Also poll write |
| 46 | err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0); |
| 47 | if (err != OK && err != ERR_BUSY) { |
| 48 | // Got an error |
| 49 | disconnect_from_host(); |
| 50 | status = STATUS_ERROR; |
| 51 | return err; |
| 52 | } |
| 53 | return OK; |
| 54 | } else if (status != STATUS_CONNECTING) { |
| 55 | return OK; |
| 56 | } |
| 57 | |
| 58 | Error err = _sock->connect_to_host(peer_address); |
| 59 | |
| 60 | if (err == OK) { |
| 61 | status = STATUS_CONNECTED; |
| 62 | return OK; |
| 63 | } else if (err == ERR_BUSY) { |
| 64 | // Check for connect timeout |
| 65 | if (OS::get_singleton()->get_ticks_msec() > timeout) { |
| 66 | disconnect_from_host(); |
| 67 | status = STATUS_ERROR; |
| 68 | return ERR_CONNECTION_ERROR; |
| 69 | } |
| 70 | // Still trying to connect |
| 71 | return OK; |
| 72 | } |
| 73 | |
| 74 | disconnect_from_host(); |
| 75 | status = STATUS_ERROR; |
| 76 | return ERR_CONNECTION_ERROR; |
| 77 | } |
| 78 | |
| 79 | Error StreamPeerSocket::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { |
| 80 | ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE); |
| 81 | |
| 82 | if (status != STATUS_CONNECTED) { |
| 83 | return FAILED; |
| 84 | } |
| 85 | |
| 86 | Error err; |
| 87 | int data_to_send = p_bytes; |
| 88 | const uint8_t *offset = p_data; |
| 89 | int total_sent = 0; |
| 90 | |
| 91 | while (data_to_send) { |
| 92 | int sent_amount = 0; |
| 93 | err = _sock->send(offset, data_to_send, sent_amount); |
| 94 | |
| 95 | if (err != OK) { |
| 96 | if (err != ERR_BUSY) { |
| 97 | disconnect_from_host(); |
| 98 | return FAILED; |
| 99 | } |
| 100 | |
| 101 | if (!p_block) { |
| 102 | r_sent = total_sent; |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | // Block and wait for the socket to accept more data |
| 107 | err = _sock->poll(NetSocket::POLL_TYPE_OUT, -1); |
| 108 | if (err != OK) { |
| 109 | disconnect_from_host(); |
| 110 | return FAILED; |
| 111 | } |
| 112 | } else { |
| 113 | data_to_send -= sent_amount; |
| 114 | offset += sent_amount; |
| 115 | total_sent += sent_amount; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | r_sent = total_sent; |
| 120 | |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | Error StreamPeerSocket::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { |
| 125 | if (status != STATUS_CONNECTED) { |
| 126 | return FAILED; |
| 127 | } |
| 128 | |
| 129 | Error err; |
| 130 | int to_read = p_bytes; |
| 131 | int total_read = 0; |
| 132 | r_received = 0; |
| 133 | |
| 134 | while (to_read) { |
| 135 | int read = 0; |
| 136 | err = _sock->recv(p_buffer + total_read, to_read, read); |
| 137 | |
| 138 | if (err != OK) { |
| 139 | if (err != ERR_BUSY) { |
| 140 | disconnect_from_host(); |
| 141 | return FAILED; |
| 142 | } |
| 143 | |
| 144 | if (!p_block) { |
| 145 | r_received = total_read; |
| 146 | return OK; |
| 147 | } |
| 148 | |
| 149 | err = _sock->poll(NetSocket::POLL_TYPE_IN, -1); |
| 150 | |
| 151 | if (err != OK) { |
| 152 | disconnect_from_host(); |
| 153 | return FAILED; |
| 154 | } |
| 155 | |
| 156 | } else if (read == 0) { |
| 157 | disconnect_from_host(); |
| 158 | r_received = total_read; |
| 159 | return ERR_FILE_EOF; |
| 160 | |
| 161 | } else { |
| 162 | to_read -= read; |
| 163 | total_read += read; |
| 164 | |
| 165 | if (!p_block) { |
| 166 | r_received = total_read; |
| 167 | return OK; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | r_received = total_read; |
| 173 | |
| 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | StreamPeerSocket::Status StreamPeerSocket::get_status() const { |
| 178 | return status; |
| 179 | } |
| 180 | |
| 181 | void StreamPeerSocket::disconnect_from_host() { |
| 182 | if (_sock.is_valid() && _sock->is_open()) { |
| 183 | _sock->close(); |
| 184 | } |
| 185 | |
| 186 | timeout = 0; |
| 187 | status = STATUS_NONE; |
| 188 | peer_address = NetSocket::Address(); |
| 189 | } |
| 190 | |
| 191 | Error StreamPeerSocket::wait(NetSocket::PollType p_type, int p_timeout) { |
| 192 | ERR_FAIL_COND_V(_sock.is_null() || !_sock->is_open(), ERR_UNAVAILABLE); |
| 193 | return _sock->poll(p_type, p_timeout); |
| 194 | } |
| 195 | |
| 196 | Error StreamPeerSocket::put_data(const uint8_t *p_data, int p_bytes) { |
| 197 | int total; |
| 198 | return write(p_data, p_bytes, total, true); |
| 199 | } |
| 200 | |
| 201 | Error StreamPeerSocket::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { |
| 202 | return write(p_data, p_bytes, r_sent, false); |
| 203 | } |
| 204 | |
| 205 | Error StreamPeerSocket::get_data(uint8_t *p_buffer, int p_bytes) { |
| 206 | int total; |
| 207 | return read(p_buffer, p_bytes, total, true); |
| 208 | } |
| 209 | |
| 210 | Error StreamPeerSocket::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { |
| 211 | return read(p_buffer, p_bytes, r_received, false); |
| 212 | } |
| 213 | |
| 214 | int StreamPeerSocket::get_available_bytes() const { |
| 215 | ERR_FAIL_COND_V(_sock.is_null(), -1); |
| 216 | return _sock->get_available_bytes(); |
| 217 | } |
| 218 | |
| 219 | void StreamPeerSocket::_bind_methods() { |
| 220 | ClassDB::bind_method(D_METHOD("poll"), &StreamPeerSocket::poll); |
| 221 | ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerSocket::get_status); |
| 222 | ClassDB::bind_method(D_METHOD("disconnect_from_host"), &StreamPeerSocket::disconnect_from_host); |
| 223 | |
| 224 | BIND_ENUM_CONSTANT(STATUS_NONE); |
| 225 | BIND_ENUM_CONSTANT(STATUS_CONNECTING); |
| 226 | BIND_ENUM_CONSTANT(STATUS_CONNECTED); |
| 227 | BIND_ENUM_CONSTANT(STATUS_ERROR); |
| 228 | } |
| 229 | |
| 230 | StreamPeerSocket::StreamPeerSocket() : |
| 231 | _sock(NetSocket::create()) { |
| 232 | } |
| 233 | |
| 234 | StreamPeerSocket::~StreamPeerSocket() { |
| 235 | disconnect_from_host(); |
| 236 | } |