Unify PacketPeerUDP using NetSocket

This commit is contained in:
Fabio Alessandrelli 2018-09-02 05:32:12 +02:00
parent b4e3be7519
commit 1b99806b47
11 changed files with 223 additions and 832 deletions

View file

@ -32,8 +32,6 @@
#include "core/io/ip.h"
PacketPeerUDP *(*PacketPeerUDP::_create)() = NULL;
void PacketPeerUDP::set_blocking_mode(bool p_enable) {
blocking = p_enable;
@ -59,6 +57,177 @@ Error PacketPeerUDP::_set_dest_address(const String &p_address, int p_port) {
return OK;
}
int PacketPeerUDP::get_available_packet_count() const {
// TODO we should deprecate this, and expose poll instead!
Error err = const_cast<PacketPeerUDP *>(this)->_poll();
if (err != OK)
return -1;
return queue_count;
}
Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
Error err = _poll();
if (err != OK)
return err;
if (queue_count == 0)
return ERR_UNAVAILABLE;
uint32_t size = 0;
uint8_t ipv6[16];
rb.read(ipv6, 16, true);
packet_ip.set_ipv6(ipv6);
rb.read((uint8_t *)&packet_port, 4, true);
rb.read((uint8_t *)&size, 4, true);
rb.read(packet_buffer, size, true);
--queue_count;
*r_buffer = packet_buffer;
r_buffer_size = size;
return OK;
}
Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
Error err;
int sent = -1;
if (!_sock->is_open()) {
IP::Type ip_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
err = _sock->open(NetSocket::TYPE_UDP, ip_type);
ERR_FAIL_COND_V(err != OK, err);
_sock->set_blocking_enabled(false);
}
do {
err = _sock->sendto(p_buffer, p_buffer_size, sent, peer_addr, peer_port);
if (err != OK) {
if (err != ERR_BUSY)
return FAILED;
else if (!blocking)
return ERR_BUSY;
// Keep trying to send full packet
continue;
}
return OK;
} while (sent != p_buffer_size);
return OK;
}
int PacketPeerUDP::get_max_packet_size() const {
return 512; // uhm maybe not
}
Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) {
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_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
Error err;
IP::Type ip_type = IP::TYPE_ANY;
if (p_bind_address.is_valid())
ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
err = _sock->open(NetSocket::TYPE_UDP, ip_type);
if (err != OK)
return ERR_CANT_CREATE;
_sock->set_blocking_enabled(false);
_sock->set_reuse_address_enabled(true);
err = _sock->bind(p_bind_address, p_port);
if (err != OK) {
_sock->close();
return err;
}
rb.resize(nearest_shift(p_recv_buffer_size));
return OK;
}
void PacketPeerUDP::close() {
if (_sock.is_valid())
_sock->close();
rb.resize(16);
queue_count = 0;
}
Error PacketPeerUDP::wait() {
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
return _sock->poll(NetSocket::POLL_TYPE_IN, -1);
}
Error PacketPeerUDP::_poll() {
ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
if (!_sock->is_open()) {
return FAILED;
}
Error err;
int read;
IP_Address ip;
uint16_t port;
while (true) {
err = _sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, ip, port);
if (err != OK) {
if (err == ERR_BUSY)
break;
return FAILED;
}
if (rb.space_left() < read + 24) {
#ifdef TOOLS_ENABLED
WARN_PRINTS("Buffer full, dropping packets!");
#endif
continue;
}
uint32_t port32 = port;
rb.write(ip.get_ipv6(), 16);
rb.write((uint8_t *)&port32, 4);
rb.write((uint8_t *)&read, 4);
rb.write(recv_buffer, read);
++queue_count;
}
return OK;
}
bool PacketPeerUDP::is_listening() const {
return _sock.is_valid() && _sock->is_open();
}
IP_Address PacketPeerUDP::get_packet_address() const {
return packet_ip;
}
int PacketPeerUDP::get_packet_port() const {
return packet_port;
}
void PacketPeerUDP::set_dest_address(const IP_Address &p_address, int p_port) {
peer_addr = p_address;
peer_port = p_port;
}
void PacketPeerUDP::_bind_methods() {
ClassDB::bind_method(D_METHOD("listen", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::listen, DEFVAL("*"), DEFVAL(65536));
@ -66,26 +235,21 @@ void PacketPeerUDP::_bind_methods() {
ClassDB::bind_method(D_METHOD("wait"), &PacketPeerUDP::wait);
ClassDB::bind_method(D_METHOD("is_listening"), &PacketPeerUDP::is_listening);
ClassDB::bind_method(D_METHOD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip);
//ClassDB::bind_method(D_METHOD("get_packet_address"),&PacketPeerUDP::_get_packet_address);
ClassDB::bind_method(D_METHOD("get_packet_port"), &PacketPeerUDP::get_packet_port);
ClassDB::bind_method(D_METHOD("set_dest_address", "host", "port"), &PacketPeerUDP::_set_dest_address);
}
Ref<PacketPeerUDP> PacketPeerUDP::create_ref() {
if (!_create)
return Ref<PacketPeerUDP>();
return Ref<PacketPeerUDP>(_create());
}
PacketPeerUDP *PacketPeerUDP::create() {
if (!_create)
return NULL;
return _create();
}
PacketPeerUDP::PacketPeerUDP() {
_sock = Ref<NetSocket>(NetSocket::create());
blocking = true;
packet_port = 0;
queue_count = 0;
peer_port = 0;
rb.resize(16);
}
PacketPeerUDP::~PacketPeerUDP() {
close();
}

View file

@ -32,36 +32,54 @@
#define PACKET_PEER_UDP_H
#include "core/io/ip.h"
#include "core/io/net_socket.h"
#include "core/io/packet_peer.h"
class PacketPeerUDP : public PacketPeer {
GDCLASS(PacketPeerUDP, PacketPeer);
protected:
bool blocking;
enum {
PACKET_BUFFER_SIZE = 65536
};
RingBuffer<uint8_t> rb;
uint8_t recv_buffer[PACKET_BUFFER_SIZE];
uint8_t packet_buffer[PACKET_BUFFER_SIZE];
IP_Address packet_ip;
int packet_port;
int queue_count;
IP_Address peer_addr;
int peer_port;
bool blocking;
Ref<NetSocket> _sock;
static PacketPeerUDP *(*_create)();
static void _bind_methods();
String _get_packet_ip() const;
Error _set_dest_address(const String &p_address, int p_port);
Error _poll();
public:
void set_blocking_mode(bool p_enable);
virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536) = 0;
virtual void close() = 0;
virtual Error wait() = 0;
virtual bool is_listening() const = 0;
virtual IP_Address get_packet_address() const = 0;
virtual int get_packet_port() const = 0;
virtual void set_dest_address(const IP_Address &p_address, int p_port) = 0;
Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
void close();
Error wait();
bool is_listening() const;
IP_Address get_packet_address() const;
int get_packet_port() const;
void set_dest_address(const IP_Address &p_address, int p_port);
static Ref<PacketPeerUDP> create_ref();
static PacketPeerUDP *create();
Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
int get_available_packet_count() const;
int get_max_packet_size() const;
PacketPeerUDP();
~PacketPeerUDP();
};
#endif // PACKET_PEER_UDP_H

View file

@ -148,7 +148,7 @@ void register_core_types() {
ClassDB::register_class<StreamPeerBuffer>();
ClassDB::register_custom_instance_class<StreamPeerTCP>();
ClassDB::register_custom_instance_class<TCP_Server>();
ClassDB::register_custom_instance_class<PacketPeerUDP>();
ClassDB::register_class<PacketPeerUDP>();
ClassDB::register_custom_instance_class<StreamPeerSSL>();
ClassDB::register_virtual_class<IP>();
ClassDB::register_virtual_class<PacketPeer>();

View file

@ -37,7 +37,7 @@
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "drivers/unix/mutex_posix.h"
#include "drivers/unix/packet_peer_udp_posix.h"
#include "drivers/unix/net_socket_posix.h"
#include "drivers/unix/rw_lock_posix.h"
#include "drivers/unix/semaphore_posix.h"
#include "drivers/unix/stream_peer_tcp_posix.h"
@ -124,9 +124,9 @@ void OS_Unix::initialize_core() {
DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM);
#ifndef NO_NETWORK
NetSocketPosix::make_default();
TCPServerPosix::make_default();
StreamPeerTCPPosix::make_default();
PacketPeerUDPPosix::make_default();
IP_Unix::make_default();
#endif

View file

@ -1,318 +0,0 @@
/*************************************************************************/
/* packet_peer_udp_posix.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
#include "packet_peer_udp_posix.h"
#ifdef UNIX_ENABLED
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#ifndef NO_FCNTL
#ifdef __HAIKU__
#include <fcntl.h>
#else
#include <sys/fcntl.h>
#endif
#else
#include <sys/ioctl.h>
#endif
#ifdef JAVASCRIPT_ENABLED
#include <arpa/inet.h>
#endif
#include "drivers/unix/socket_helpers.h"
int PacketPeerUDPPosix::get_available_packet_count() const {
Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
if (err != OK)
return -1;
return queue_count;
}
Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false);
if (err != OK)
return err;
if (queue_count == 0)
return ERR_UNAVAILABLE;
uint32_t size = 0;
uint8_t type = IP::TYPE_NONE;
rb.read(&type, 1, true);
if (type == IP::TYPE_IPV4) {
uint8_t ip[4];
rb.read(ip, 4, true);
packet_ip.set_ipv4(ip);
} else {
uint8_t ipv6[16];
rb.read(ipv6, 16, true);
packet_ip.set_ipv6(ipv6);
};
rb.read((uint8_t *)&packet_port, 4, true);
rb.read((uint8_t *)&size, 4, true);
rb.read(packet_buffer, size, true);
--queue_count;
*r_buffer = packet_buffer;
r_buffer_size = size;
return OK;
}
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
if (sock_type == IP::TYPE_NONE)
sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
int sock = _get_socket();
ERR_FAIL_COND_V(sock == -1, FAILED);
struct sockaddr_storage addr;
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
errno = 0;
int err;
_set_sock_blocking(blocking);
while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) {
if (errno != EAGAIN) {
return FAILED;
} else if (!blocking) {
return ERR_UNAVAILABLE;
}
}
return OK;
}
int PacketPeerUDPPosix::get_max_packet_size() const {
return 512; // uhm maybe not
}
Error PacketPeerUDPPosix::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) {
ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
#ifdef __OpenBSD__
sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
#else
sock_type = IP::TYPE_ANY;
#endif
if (p_bind_address.is_valid())
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
int sock = _get_socket();
if (sock == -1)
return ERR_CANT_CREATE;
sockaddr_storage addr = { 0 };
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
close();
return ERR_UNAVAILABLE;
}
rb.resize(nearest_shift(p_recv_buffer_size));
return OK;
}
void PacketPeerUDPPosix::close() {
if (sockfd != -1)
::close(sockfd);
sockfd = -1;
sock_type = IP::TYPE_NONE;
sock_blocking = true;
rb.resize(16);
queue_count = 0;
}
Error PacketPeerUDPPosix::wait() {
return _poll(true);
}
Error PacketPeerUDPPosix::_poll(bool p_block) {
if (sockfd == -1) {
return FAILED;
}
_set_sock_blocking(p_block);
struct sockaddr_storage from = { 0 };
socklen_t len = sizeof(struct sockaddr_storage);
int ret;
while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {
uint32_t port = 0;
if (from.ss_family == AF_INET) {
uint8_t type = (uint8_t)IP::TYPE_IPV4;
rb.write(&type, 1);
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
rb.write((uint8_t *)&sin_from->sin_addr, 4);
port = ntohs(sin_from->sin_port);
} else if (from.ss_family == AF_INET6) {
uint8_t type = (uint8_t)IP::TYPE_IPV6;
rb.write(&type, 1);
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
rb.write((uint8_t *)&s6_from->sin6_addr, 16);
port = ntohs(s6_from->sin6_port);
} else {
// WARN_PRINT("Ignoring packet with unknown address family");
uint8_t type = (uint8_t)IP::TYPE_NONE;
rb.write(&type, 1);
};
rb.write((uint8_t *)&port, 4);
rb.write((uint8_t *)&ret, 4);
rb.write(recv_buffer, ret);
len = sizeof(struct sockaddr_storage);
++queue_count;
if (p_block)
break;
};
// TODO: Should ECONNRESET be handled here?
if (ret == 0 || (ret == -1 && errno != EAGAIN)) {
close();
return FAILED;
};
return OK;
}
bool PacketPeerUDPPosix::is_listening() const {
return sockfd != -1;
}
IP_Address PacketPeerUDPPosix::get_packet_address() const {
return packet_ip;
}
int PacketPeerUDPPosix::get_packet_port() const {
return packet_port;
}
int PacketPeerUDPPosix::_get_socket() {
ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
if (sockfd != -1)
return sockfd;
sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd != -1)
_set_sock_blocking(false);
return sockfd;
}
void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) {
if (sock_blocking == p_blocking)
return;
sock_blocking = p_blocking;
#ifndef NO_FCNTL
int opts = fcntl(sockfd, F_GETFL);
int ret = 0;
if (sock_blocking)
ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK);
else
ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK);
if (ret == -1)
perror("setting non-block mode");
#else
int bval = sock_blocking ? 0 : 1;
if (ioctl(sockfd, FIONBIO, &bval) == -1)
perror("setting non-block mode");
#endif
}
void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) {
peer_addr = p_address;
peer_port = p_port;
}
PacketPeerUDP *PacketPeerUDPPosix::_create() {
return memnew(PacketPeerUDPPosix);
};
void PacketPeerUDPPosix::make_default() {
PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
};
PacketPeerUDPPosix::PacketPeerUDPPosix() {
blocking = true;
sock_blocking = true;
sockfd = -1;
packet_port = 0;
queue_count = 0;
peer_port = 0;
sock_type = IP::TYPE_NONE;
rb.resize(16);
}
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
close();
}
#endif

View file

@ -1,88 +0,0 @@
/*************************************************************************/
/* packet_peer_udp_posix.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
#ifndef PACKET_PEER_UDP_POSIX_H
#define PACKET_PEER_UDP_POSIX_H
#ifdef UNIX_ENABLED
#include "core/io/packet_peer_udp.h"
#include "core/ring_buffer.h"
class PacketPeerUDPPosix : public PacketPeerUDP {
enum {
PACKET_BUFFER_SIZE = 65536
};
RingBuffer<uint8_t> rb;
uint8_t recv_buffer[PACKET_BUFFER_SIZE];
uint8_t packet_buffer[PACKET_BUFFER_SIZE];
IP_Address packet_ip;
int packet_port;
int queue_count;
int sockfd;
bool sock_blocking;
IP::Type sock_type;
IP_Address peer_addr;
int peer_port;
_FORCE_INLINE_ int _get_socket();
static PacketPeerUDP *_create();
void _set_sock_blocking(bool p_blocking);
virtual Error _poll(bool p_block);
public:
virtual int get_available_packet_count() const;
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
virtual int get_max_packet_size() const;
virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
virtual void close();
virtual Error wait();
virtual bool is_listening() const;
virtual IP_Address get_packet_address() const;
virtual int get_packet_port() const;
virtual void set_dest_address(const IP_Address &p_address, int p_port);
static void make_default();
PacketPeerUDPPosix();
~PacketPeerUDPPosix();
};
#endif // PACKET_PEER_UDP_POSIX_H
#endif

View file

@ -1,300 +0,0 @@
/*************************************************************************/
/* packet_peer_udp_winsock.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
#ifdef WINDOWS_ENABLED
#include "packet_peer_udp_winsock.h"
#include <winsock2.h>
#include <ws2tcpip.h>
// Must be included after Windows headers or hell breaks loose
#include "drivers/unix/socket_helpers.h"
int PacketPeerUDPWinsock::get_available_packet_count() const {
Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false);
if (err != OK)
return -1;
return queue_count;
}
Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false);
if (err != OK)
return err;
if (queue_count == 0)
return ERR_UNAVAILABLE;
uint32_t size;
uint8_t type;
rb.read(&type, 1, true);
if (type == IP::TYPE_IPV4) {
uint8_t ip[4];
rb.read(ip, 4, true);
packet_ip.set_ipv4(ip);
} else {
uint8_t ip[16];
rb.read(ip, 16, true);
packet_ip.set_ipv6(ip);
};
rb.read((uint8_t *)&packet_port, 4, true);
rb.read((uint8_t *)&size, 4, true);
rb.read(packet_buffer, size, true);
--queue_count;
*r_buffer = packet_buffer;
r_buffer_size = size;
return OK;
}
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
if (sock_type == IP::TYPE_NONE)
sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
int sock = _get_socket();
ERR_FAIL_COND_V(sock == -1, FAILED);
struct sockaddr_storage addr;
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
_set_sock_blocking(blocking);
errno = 0;
int err;
while ((err = sendto(sock, (const char *)p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return FAILED;
} else if (!blocking) {
return ERR_UNAVAILABLE;
}
}
return OK;
}
int PacketPeerUDPWinsock::get_max_packet_size() const {
return 512; // uhm maybe not
}
void PacketPeerUDPWinsock::_set_sock_blocking(bool p_blocking) {
if (sock_blocking == p_blocking)
return;
sock_blocking = p_blocking;
unsigned long par = sock_blocking ? 0 : 1;
if (ioctlsocket(sockfd, FIONBIO, &par)) {
perror("setting non-block mode");
//close();
//return -1;
};
}
Error PacketPeerUDPWinsock::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) {
ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
sock_type = IP::TYPE_ANY;
if (p_bind_address.is_valid())
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
int sock = _get_socket();
if (sock == -1)
return ERR_CANT_CREATE;
struct sockaddr_storage addr = { 0 };
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
close();
return ERR_UNAVAILABLE;
}
printf("UDP Connection listening on port %i\n", p_port);
rb.resize(nearest_shift(p_recv_buffer_size));
return OK;
}
void PacketPeerUDPWinsock::close() {
if (sockfd != -1)
::closesocket(sockfd);
sockfd = -1;
sock_type = IP::TYPE_NONE;
sock_blocking = true;
rb.resize(16);
queue_count = 0;
}
Error PacketPeerUDPWinsock::wait() {
return _poll(true);
}
Error PacketPeerUDPWinsock::_poll(bool p_wait) {
if (sockfd == -1) {
return FAILED;
}
_set_sock_blocking(p_wait);
struct sockaddr_storage from = { 0 };
int len = sizeof(struct sockaddr_storage);
int ret;
while ((ret = recvfrom(sockfd, (char *)recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {
uint32_t port = 0;
if (from.ss_family == AF_INET) {
uint8_t type = (uint8_t)IP::TYPE_IPV4;
rb.write(&type, 1);
struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
rb.write((uint8_t *)&sin_from->sin_addr, 4);
port = ntohs(sin_from->sin_port);
} else if (from.ss_family == AF_INET6) {
uint8_t type = (uint8_t)IP::TYPE_IPV6;
rb.write(&type, 1);
struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
rb.write((uint8_t *)&s6_from->sin6_addr, 16);
port = ntohs(s6_from->sin6_port);
} else {
// WARN_PRINT("Ignoring packet with unknown address family");
uint8_t type = (uint8_t)IP::TYPE_NONE;
rb.write(&type, 1);
};
rb.write((uint8_t *)&port, 4);
rb.write((uint8_t *)&ret, 4);
rb.write(recv_buffer, ret);
len = sizeof(struct sockaddr_storage);
++queue_count;
if (p_wait)
break;
};
if (ret == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
// Expected when doing non-blocking sockets, retry later.
} else if (error == WSAECONNRESET) {
// If the remote target does not accept messages, this error may occur, but is harmless.
// Once the remote target gets available, this message will disappear for new messages.
} else {
close();
return FAILED;
}
}
if (ret == 0) {
close();
return FAILED;
};
return OK;
}
bool PacketPeerUDPWinsock::is_listening() const {
return sockfd != -1;
}
IP_Address PacketPeerUDPWinsock::get_packet_address() const {
return packet_ip;
}
int PacketPeerUDPWinsock::get_packet_port() const {
return packet_port;
}
int PacketPeerUDPWinsock::_get_socket() {
ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
if (sockfd != -1)
return sockfd;
sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd != -1)
_set_sock_blocking(false);
return sockfd;
}
void PacketPeerUDPWinsock::set_dest_address(const IP_Address &p_address, int p_port) {
peer_addr = p_address;
peer_port = p_port;
}
void PacketPeerUDPWinsock::make_default() {
PacketPeerUDP::_create = PacketPeerUDPWinsock::_create;
};
PacketPeerUDP *PacketPeerUDPWinsock::_create() {
return memnew(PacketPeerUDPWinsock);
};
PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
blocking = true;
sock_blocking = true;
sockfd = -1;
packet_port = 0;
queue_count = 0;
peer_port = 0;
sock_type = IP::TYPE_NONE;
rb.resize(16);
}
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
close();
}
#endif

View file

@ -1,89 +0,0 @@
/*************************************************************************/
/* packet_peer_udp_winsock.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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. */
/*************************************************************************/
#ifdef WINDOWS_ENABLED
#ifndef PACKET_PEER_UDP_WINSOCK_H
#define PACKET_PEER_UDP_WINSOCK_H
#include "core/io/packet_peer_udp.h"
#include "core/ring_buffer.h"
class PacketPeerUDPWinsock : public PacketPeerUDP {
enum {
PACKET_BUFFER_SIZE = 65536
};
RingBuffer<uint8_t> rb;
uint8_t recv_buffer[PACKET_BUFFER_SIZE];
uint8_t packet_buffer[PACKET_BUFFER_SIZE];
IP_Address packet_ip;
int packet_port;
int queue_count;
int sockfd;
bool sock_blocking;
IP::Type sock_type;
IP_Address peer_addr;
int peer_port;
_FORCE_INLINE_ int _get_socket();
static PacketPeerUDP *_create();
void _set_sock_blocking(bool p_blocking);
Error _poll(bool p_wait);
public:
virtual int get_available_packet_count() const;
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
virtual int get_max_packet_size() const;
virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
virtual void close();
virtual Error wait();
virtual bool is_listening() const;
virtual IP_Address get_packet_address() const;
virtual int get_packet_port() const;
virtual void set_dest_address(const IP_Address &p_address, int p_port);
static void make_default();
PacketPeerUDPWinsock();
~PacketPeerUDPWinsock();
};
#endif // PACKET_PEER_UDP_WINSOCK_H
#endif

View file

@ -28,6 +28,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
// Must include Winsock before windows.h (included by os_uwp.h)
#include "drivers/unix/net_socket_posix.h"
#include "os_uwp.h"
#include "core/io/marshalls.h"
@ -38,7 +41,6 @@
#include "drivers/windows/dir_access_windows.h"
#include "drivers/windows/file_access_windows.h"
#include "drivers/windows/mutex_windows.h"
#include "drivers/windows/packet_peer_udp_winsock.h"
#include "drivers/windows/rw_lock_windows.h"
#include "drivers/windows/semaphore_windows.h"
#include "drivers/windows/stream_peer_tcp_winsock.h"
@ -151,9 +153,9 @@ void OSUWP::initialize_core() {
DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_FILESYSTEM);
NetSocketPosix::make_default();
TCPServerWinsock::make_default();
StreamPeerTCPWinsock::make_default();
PacketPeerUDPWinsock::make_default();
// We need to know how often the clock is updated
if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second))

View file

@ -28,6 +28,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
// Must include Winsock before windows.h (included by os_windows.h)
#include "drivers/unix/net_socket_posix.h"
#include "os_windows.h"
#include "core/io/marshalls.h"
@ -37,7 +40,6 @@
#include "drivers/windows/dir_access_windows.h"
#include "drivers/windows/file_access_windows.h"
#include "drivers/windows/mutex_windows.h"
#include "drivers/windows/packet_peer_udp_winsock.h"
#include "drivers/windows/rw_lock_windows.h"
#include "drivers/windows/semaphore_windows.h"
#include "drivers/windows/stream_peer_tcp_winsock.h"
@ -219,9 +221,9 @@ void OS_Windows::initialize_core() {
DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_USERDATA);
DirAccess::make_default<DirAccessWindows>(DirAccess::ACCESS_FILESYSTEM);
NetSocketPosix::make_default();
TCPServerWinsock::make_default();
StreamPeerTCPWinsock::make_default();
PacketPeerUDPWinsock::make_default();
// We need to know how often the clock is updated
if (!QueryPerformanceFrequency((LARGE_INTEGER *)&ticks_per_second))

View file

@ -108,7 +108,7 @@ int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
ENetSocket enet_socket_create(ENetSocketType type) {
PacketPeerUDP *socket = PacketPeerUDP::create();
PacketPeerUDP *socket = memnew(PacketPeerUDP);
socket->set_blocking_mode(false);
return socket;
@ -151,7 +151,7 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
err = sock->put_packet((const uint8_t *)&w[0], size);
if (err != OK) {
if (err == ERR_UNAVAILABLE) { // blocking call
if (err == ERR_BUSY) { // Blocking call
return 0;
}