Use NetSocket directly for ENet.
Avoid PacketPeerUDP buffering.
This commit is contained in:
parent
64f649a80c
commit
c5a4d638c6
64
thirdparty/enet/godot.cpp
vendored
64
thirdparty/enet/godot.cpp
vendored
@ -33,7 +33,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "core/io/ip.h"
|
#include "core/io/ip.h"
|
||||||
#include "core/io/packet_peer_udp.h"
|
#include "core/io/net_socket.h"
|
||||||
#include "core/os/os.h"
|
#include "core/os/os.h"
|
||||||
|
|
||||||
// This must be last for windows to compile (tested with MinGW)
|
// 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;
|
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) {
|
int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
|
||||||
|
|
||||||
IP_Address ip;
|
IP_Address ip;
|
||||||
@ -99,23 +109,15 @@ int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
|
|||||||
ip.set_ipv6(address->host);
|
ip.set_ipv6(address->host);
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketPeerUDP *sock = (PacketPeerUDP *)socket;
|
NetSocket *sock = (NetSocket *)socket;
|
||||||
if (sock->listen(address->port, ip) != OK) {
|
if (sock->bind(ip, address->port) != OK) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
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) {
|
void enet_socket_destroy(ENetSocket socket) {
|
||||||
PacketPeerUDP *sock = (PacketPeerUDP *)socket;
|
NetSocket *sock = (NetSocket *)socket;
|
||||||
sock->close();
|
sock->close();
|
||||||
memdelete(sock);
|
memdelete(sock);
|
||||||
}
|
}
|
||||||
@ -124,13 +126,12 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
|
|||||||
|
|
||||||
ERR_FAIL_COND_V(address == NULL, -1);
|
ERR_FAIL_COND_V(address == NULL, -1);
|
||||||
|
|
||||||
PacketPeerUDP *sock = (PacketPeerUDP *)socket;
|
NetSocket *sock = (NetSocket *)socket;
|
||||||
IP_Address dest;
|
IP_Address dest;
|
||||||
Error err;
|
Error err;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
||||||
dest.set_ipv6(address->host);
|
dest.set_ipv6(address->host);
|
||||||
sock->set_dest_address(dest, address->port);
|
|
||||||
|
|
||||||
// Create a single packet.
|
// Create a single packet.
|
||||||
PoolVector<uint8_t> out;
|
PoolVector<uint8_t> out;
|
||||||
@ -148,7 +149,8 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
|
|||||||
pos += buffers[i].dataLength;
|
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 != OK) {
|
||||||
|
|
||||||
if (err == ERR_BUSY) { // Blocking call
|
if (err == ERR_BUSY) { // Blocking call
|
||||||
@ -159,32 +161,36 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
|
int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
|
||||||
|
|
||||||
ERR_FAIL_COND_V(bufferCount != 1, -1);
|
ERR_FAIL_COND_V(bufferCount != 1, -1);
|
||||||
|
|
||||||
PacketPeerUDP *sock = (PacketPeerUDP *)socket;
|
NetSocket *sock = (NetSocket *)socket;
|
||||||
|
|
||||||
int pc = sock->get_available_packet_count();
|
Error ret = sock->poll(NetSocket::POLL_TYPE_IN, 0);
|
||||||
if (pc < 1) {
|
|
||||||
return pc;
|
|
||||||
}
|
|
||||||
|
|
||||||
const uint8_t *buffer;
|
if (ret == ERR_BUSY)
|
||||||
int buffer_size;
|
return 0;
|
||||||
Error err = sock->get_packet(&buffer, buffer_size);
|
|
||||||
if (err)
|
if (ret != OK)
|
||||||
return -1;
|
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);
|
Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
|
||||||
address->port = sock->get_packet_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
|
// Not implemented
|
||||||
|
Loading…
Reference in New Issue
Block a user