[Net] Implement lower level ENet wrappers.

This commit is contained in:
Fabio Alessandrelli
2021-06-14 11:51:53 +02:00
parent 1e8bf86379
commit 42a1777531
10 changed files with 1487 additions and 10 deletions

View File

@ -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__

View File

@ -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) {