2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-12-15 16:39:30 +01:00
|
|
|
/* stream_peer_tcp_winsock.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2018-01-01 14:40:08 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifdef WINDOWS_ENABLED
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
#include "stream_peer_tcp_winsock.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#include <winsock2.h>
|
2016-10-20 12:04:10 +02:00
|
|
|
#include <ws2tcpip.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
#include "drivers/unix/socket_helpers.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
int winsock_refcount = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
StreamPeerTCP *StreamPeerTCPWinsock::_create() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
return memnew(StreamPeerTCPWinsock);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
void StreamPeerTCPWinsock::make_default() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
StreamPeerTCP::_create = StreamPeerTCPWinsock::_create;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (winsock_refcount == 0) {
|
|
|
|
WSADATA data;
|
2017-03-05 16:44:50 +01:00
|
|
|
WSAStartup(MAKEWORD(2, 2), &data);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
++winsock_refcount;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
void StreamPeerTCPWinsock::cleanup() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
--winsock_refcount;
|
|
|
|
if (winsock_refcount == 0) {
|
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::_block(int p_sockfd, bool p_read, bool p_write) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
fd_set read, write;
|
|
|
|
FD_ZERO(&read);
|
|
|
|
FD_ZERO(&write);
|
|
|
|
|
|
|
|
if (p_read)
|
|
|
|
FD_SET(p_sockfd, &read);
|
|
|
|
if (p_write)
|
|
|
|
FD_SET(p_sockfd, &write);
|
|
|
|
|
|
|
|
int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever
|
|
|
|
return ret < 0 ? FAILED : OK;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::_poll_connection() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
|
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
struct sockaddr_storage their_addr;
|
2016-12-20 15:13:16 +01:00
|
|
|
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
if (err == WSAEISCONN) {
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-01-16 15:25:16 +01:00
|
|
|
if (err == WSAEINPROGRESS || err == WSAEALREADY) {
|
2016-12-08 19:59:16 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = STATUS_ERROR;
|
|
|
|
return ERR_CONNECTION_ERROR;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (status == STATUS_NONE || status == STATUS_ERROR) {
|
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (status != STATUS_CONNECTED) {
|
|
|
|
|
2017-01-18 19:04:37 +01:00
|
|
|
if (_poll_connection() != OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (status != STATUS_CONNECTED) {
|
|
|
|
r_sent = 0;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
int data_to_send = p_bytes;
|
|
|
|
const uint8_t *offset = p_data;
|
|
|
|
if (sockfd == -1) return FAILED;
|
|
|
|
int total_sent = 0;
|
|
|
|
|
|
|
|
while (data_to_send) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int sent_amount = send(sockfd, (const char *)offset, data_to_send, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (sent_amount == -1) {
|
|
|
|
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
|
2017-12-05 15:41:38 +01:00
|
|
|
perror("Nothing sent");
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2017-01-14 16:23:01 +01:00
|
|
|
ERR_PRINT("Server disconnected!\n");
|
2014-02-10 02:10:30 +01:00
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!p_block) {
|
|
|
|
r_sent = total_sent;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
_block(sockfd, false, true);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
data_to_send -= sent_amount;
|
|
|
|
offset += sent_amount;
|
|
|
|
total_sent += sent_amount;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
r_sent = total_sent;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
if (!is_connected_to_host()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (status != STATUS_CONNECTED) {
|
|
|
|
|
2017-01-18 19:04:37 +01:00
|
|
|
if (_poll_connection() != OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (status != STATUS_CONNECTED) {
|
|
|
|
r_received = 0;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
int to_read = p_bytes;
|
|
|
|
int total_read = 0;
|
|
|
|
|
|
|
|
while (to_read) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int read = recv(sockfd, (char *)p_buffer + total_read, to_read, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (read == -1) {
|
|
|
|
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
|
2017-12-05 15:41:38 +01:00
|
|
|
perror("Nothing read");
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2017-01-14 16:23:01 +01:00
|
|
|
ERR_PRINT("Server disconnected!\n");
|
2014-02-10 02:10:30 +01:00
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!p_block) {
|
|
|
|
|
|
|
|
r_received = total_read;
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
_block(sockfd, true, false);
|
|
|
|
} else if (read == 0) {
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_FILE_EOF;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
to_read -= read;
|
|
|
|
total_read += read;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
r_received = total_read;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::put_data(const uint8_t *p_data, int p_bytes) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int total;
|
|
|
|
return write(p_data, p_bytes, total, true);
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return write(p_data, p_bytes, r_sent, false);
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::get_data(uint8_t *p_buffer, int p_bytes) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int total;
|
|
|
|
return read(p_buffer, p_bytes, total, true);
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return read(p_buffer, p_bytes, r_received, false);
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
StreamPeerTCP::Status StreamPeerTCPWinsock::get_status() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (status == STATUS_CONNECTING) {
|
2017-01-18 19:04:37 +01:00
|
|
|
_poll_connection();
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return status;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
bool StreamPeerTCPWinsock::is_connected_to_host() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (status == STATUS_NONE || status == STATUS_ERROR) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (status != STATUS_CONNECTED) {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (sockfd != INVALID_SOCKET);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
void StreamPeerTCPWinsock::disconnect_from_host() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (sockfd != INVALID_SOCKET)
|
|
|
|
closesocket(sockfd);
|
2017-03-05 16:44:50 +01:00
|
|
|
sockfd = INVALID_SOCKET;
|
2016-12-20 15:13:16 +01:00
|
|
|
sock_type = IP::TYPE_NONE;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
status = STATUS_NONE;
|
|
|
|
|
|
|
|
peer_host = IP_Address();
|
|
|
|
peer_port = 0;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
void StreamPeerTCPWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
sockfd = p_sockfd;
|
2017-01-18 12:47:12 +01:00
|
|
|
sock_type = p_sock_type;
|
2014-02-10 02:10:30 +01:00
|
|
|
status = STATUS_CONNECTING;
|
|
|
|
peer_host = p_host;
|
|
|
|
peer_port = p_port;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
Error StreamPeerTCPWinsock::connect_to_host(const IP_Address &p_host, uint16_t p_port) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-12-20 15:13:16 +01:00
|
|
|
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
|
|
|
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sockfd == INVALID_SOCKET) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_PRINT("Socket creation failed!");
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2014-02-10 02:10:30 +01:00
|
|
|
//perror("socket");
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned long par = 1;
|
|
|
|
if (ioctlsocket(sockfd, FIONBIO, &par)) {
|
|
|
|
perror("setting non-block mode");
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2014-02-10 02:10:30 +01:00
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
struct sockaddr_storage their_addr;
|
2016-12-20 15:13:16 +01:00
|
|
|
size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
ERR_PRINT("Connection to remote host failed!");
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2014-02-10 02:10:30 +01:00
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
status = STATUS_CONNECTING;
|
|
|
|
} else {
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
};
|
|
|
|
|
|
|
|
peer_host = p_host;
|
|
|
|
peer_port = p_port;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2018-01-30 16:22:15 +01:00
|
|
|
void StreamPeerTCPWinsock::set_no_delay(bool p_enabled) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(!is_connected_to_host());
|
|
|
|
int flag = p_enabled ? 1 : 0;
|
2018-04-19 23:20:26 +02:00
|
|
|
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) != 0) {
|
|
|
|
ERR_PRINT("Unable to set TCP no delay option");
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
int StreamPeerTCPWinsock::get_available_bytes() const {
|
2015-12-13 16:53:29 +01:00
|
|
|
|
|
|
|
unsigned long len;
|
2017-03-05 16:44:50 +01:00
|
|
|
int ret = ioctlsocket(sockfd, FIONREAD, &len);
|
|
|
|
ERR_FAIL_COND_V(ret == -1, 0)
|
2015-12-13 16:53:29 +01:00
|
|
|
return len;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
IP_Address StreamPeerTCPWinsock::get_connected_host() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return peer_host;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
uint16_t StreamPeerTCPWinsock::get_connected_port() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return peer_port;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
StreamPeerTCPWinsock::StreamPeerTCPWinsock() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-12-20 15:13:16 +01:00
|
|
|
sock_type = IP::TYPE_NONE;
|
2014-02-10 02:10:30 +01:00
|
|
|
sockfd = INVALID_SOCKET;
|
|
|
|
status = STATUS_NONE;
|
|
|
|
peer_port = 0;
|
|
|
|
};
|
|
|
|
|
2017-12-15 16:39:30 +01:00
|
|
|
StreamPeerTCPWinsock::~StreamPeerTCPWinsock() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
disconnect_from_host();
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|