summaryrefslogtreecommitdiff
path: root/thirdparty/enet
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2021-06-14 11:51:53 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2021-07-29 10:59:00 +0200
commit42a17775314424ba24974d072bb5f74bc4ff9467 (patch)
treec9ff8a615ff01be6514762f87293c83e243906d1 /thirdparty/enet
parent1e8bf86379ca818bf7208de5f04f986be95dc102 (diff)
[Net] Implement lower level ENet wrappers.
Diffstat (limited to 'thirdparty/enet')
-rw-r--r--thirdparty/enet/enet/godot_ext.h4
-rw-r--r--thirdparty/enet/godot.cpp35
2 files changed, 29 insertions, 10 deletions
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>((CryptoKey *)p_key), Ref<X509Certificate>((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>((CryptoKey *)p_key), Ref<X509Certificate>((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>((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>((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) {