From c5a4d638c641056d0b023af05a59e6b430f31d8c Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Thu, 18 Oct 2018 22:35:11 +0200 Subject: [PATCH] Use NetSocket directly for ENet. Avoid PacketPeerUDP buffering. --- thirdparty/enet/godot.cpp | 64 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/thirdparty/enet/godot.cpp b/thirdparty/enet/godot.cpp index 6ba7cf0000f..73a09f9b1df 100644 --- a/thirdparty/enet/godot.cpp +++ b/thirdparty/enet/godot.cpp @@ -33,7 +33,7 @@ */ #include "core/io/ip.h" -#include "core/io/packet_peer_udp.h" +#include "core/io/net_socket.h" #include "core/os/os.h" // This must be last for windows to compile (tested with MinGW) @@ -90,6 +90,16 @@ int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLen return -1; } +ENetSocket enet_socket_create(ENetSocketType type) { + + NetSocket *socket = NetSocket::create(); + IP::Type ip_type = IP::TYPE_ANY; + socket->open(NetSocket::TYPE_UDP, ip_type); + socket->set_blocking_enabled(false); + + return socket; +} + int enet_socket_bind(ENetSocket socket, const ENetAddress *address) { IP_Address ip; @@ -99,23 +109,15 @@ int enet_socket_bind(ENetSocket socket, const ENetAddress *address) { ip.set_ipv6(address->host); } - PacketPeerUDP *sock = (PacketPeerUDP *)socket; - if (sock->listen(address->port, ip) != OK) { + NetSocket *sock = (NetSocket *)socket; + if (sock->bind(ip, address->port) != OK) { return -1; } return 0; } -ENetSocket enet_socket_create(ENetSocketType type) { - - PacketPeerUDP *socket = memnew(PacketPeerUDP); - socket->set_blocking_mode(false); - - return socket; -} - void enet_socket_destroy(ENetSocket socket) { - PacketPeerUDP *sock = (PacketPeerUDP *)socket; + NetSocket *sock = (NetSocket *)socket; sock->close(); memdelete(sock); } @@ -124,13 +126,12 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu ERR_FAIL_COND_V(address == NULL, -1); - PacketPeerUDP *sock = (PacketPeerUDP *)socket; + NetSocket *sock = (NetSocket *)socket; IP_Address dest; Error err; size_t i = 0; dest.set_ipv6(address->host); - sock->set_dest_address(dest, address->port); // Create a single packet. PoolVector out; @@ -148,7 +149,8 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu pos += buffers[i].dataLength; } - err = sock->put_packet((const uint8_t *)&w[0], size); + int sent = 0; + err = sock->sendto((const uint8_t *)&w[0], size, sent, dest, address->port); if (err != OK) { if (err == ERR_BUSY) { // Blocking call @@ -159,32 +161,36 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu return -1; } - return size; + return sent; } int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) { ERR_FAIL_COND_V(bufferCount != 1, -1); - PacketPeerUDP *sock = (PacketPeerUDP *)socket; + NetSocket *sock = (NetSocket *)socket; - int pc = sock->get_available_packet_count(); - if (pc < 1) { - return pc; - } + Error ret = sock->poll(NetSocket::POLL_TYPE_IN, 0); - const uint8_t *buffer; - int buffer_size; - Error err = sock->get_packet(&buffer, buffer_size); - if (err) + if (ret == ERR_BUSY) + return 0; + + if (ret != OK) return -1; - copymem(buffers[0].data, buffer, buffer_size); + int read; + IP_Address ip; - enet_address_set_ip(address, sock->get_packet_address().get_ipv6(), 16); - address->port = sock->get_packet_port(); + Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port); + if (err == ERR_BUSY) + return 0; - return buffer_size; + if (err != OK) + return -1; + + enet_address_set_ip(address, ip.get_ipv6(), 16); + + return read; } // Not implemented