From 42a17775314424ba24974d072bb5f74bc4ff9467 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 14 Jun 2021 11:51:53 +0200 Subject: [Net] Implement lower level ENet wrappers. --- thirdparty/enet/enet/godot_ext.h | 4 ++-- thirdparty/enet/godot.cpp | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) (limited to 'thirdparty/enet') diff --git a/thirdparty/enet/enet/godot_ext.h b/thirdparty/enet/enet/godot_ext.h index 84a23b1c85..648f3d2f24 100644 --- a/thirdparty/enet/enet/godot_ext.h +++ b/thirdparty/enet/enet/godot_ext.h @@ -11,8 +11,8 @@ */ ENET_API void enet_address_set_ip(ENetAddress * address, const uint8_t * ip, size_t size); -ENET_API void enet_host_dtls_server_setup (ENetHost *, void *, void *); -ENET_API void enet_host_dtls_client_setup (ENetHost *, void *, uint8_t, const char *); +ENET_API int enet_host_dtls_server_setup (ENetHost *, void *, void *); +ENET_API int enet_host_dtls_client_setup (ENetHost *, void *, uint8_t, const char *); ENET_API void enet_host_refuse_new_connections (ENetHost *, int); #endif // __ENET_GODOT_EXT_H__ diff --git a/thirdparty/enet/godot.cpp b/thirdparty/enet/godot.cpp index 3e353d7eec..d0b381ea51 100644 --- a/thirdparty/enet/godot.cpp +++ b/thirdparty/enet/godot.cpp @@ -52,6 +52,7 @@ public: virtual int set_option(ENetSocketOption p_option, int p_value) = 0; virtual void close() = 0; virtual void set_refuse_new_connections(bool p_enable) {} /* Only used by dtls server */ + virtual bool can_upgrade() { return false; } /* Only true in ENetUDP */ virtual ~ENetGodotSocket() {} }; @@ -79,6 +80,10 @@ public: sock->close(); } + bool can_upgrade() { + return true; + } + Error bind(IPAddress p_ip, uint16_t p_port) { local_address = p_ip; bound = true; @@ -86,7 +91,11 @@ public: } Error get_socket_address(IPAddress *r_ip, uint16_t *r_port) { - return sock->get_socket_address(r_ip, r_port); + Error err = sock->get_socket_address(r_ip, r_port); + if (bound) { + *r_ip = local_address; + } + return err; } Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) { @@ -195,7 +204,9 @@ public: Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) { if (!connected) { udp->connect_to_host(p_ip, p_port); - dtls->connect_to_peer(udp, verify, for_hostname, cert); + if (dtls->connect_to_peer(udp, verify, for_hostname, cert)) { + return FAILED; + } connected = true; } dtls->poll(); @@ -424,16 +435,24 @@ ENetSocket enet_socket_create(ENetSocketType type) { return socket; } -void enet_host_dtls_server_setup(ENetHost *host, void *p_key, void *p_cert) { - ENetUDP *sock = (ENetUDP *)host->socket; - host->socket = memnew(ENetDTLSServer(sock, Ref((CryptoKey *)p_key), Ref((X509Certificate *)p_cert))); +int enet_host_dtls_server_setup(ENetHost *host, void *p_key, void *p_cert) { + ENetGodotSocket *sock = (ENetGodotSocket *)host->socket; + if (!sock->can_upgrade()) { + return -1; + } + host->socket = memnew(ENetDTLSServer((ENetUDP *)sock, Ref((CryptoKey *)p_key), Ref((X509Certificate *)p_cert))); memdelete(sock); + return 0; } -void enet_host_dtls_client_setup(ENetHost *host, void *p_cert, uint8_t p_verify, const char *p_for_hostname) { - ENetUDP *sock = (ENetUDP *)host->socket; - host->socket = memnew(ENetDTLSClient(sock, Ref((X509Certificate *)p_cert), p_verify, String(p_for_hostname))); +int enet_host_dtls_client_setup(ENetHost *host, void *p_cert, uint8_t p_verify, const char *p_for_hostname) { + ENetGodotSocket *sock = (ENetGodotSocket *)host->socket; + if (!sock->can_upgrade()) { + return -1; + } + host->socket = memnew(ENetDTLSClient((ENetUDP *)sock, Ref((X509Certificate *)p_cert), p_verify, String::utf8(p_for_hostname))); memdelete(sock); + return 0; } void enet_host_refuse_new_connections(ENetHost *host, int p_refuse) { -- cgit v1.2.3