ENetGodot: Apply clang-format and style guide

This commit is contained in:
Rémi Verschelde 2020-07-21 11:38:16 +02:00
parent fa3ac7deae
commit 896e7193d8

View File

@ -44,15 +44,14 @@
/// Abstract ENet interface for UDP/DTLS.
class ENetGodotSocket {
public:
virtual Error bind(IP_Address p_ip, uint16_t p_port) = 0;
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) = 0;
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) = 0;
virtual int set_option(ENetSocketOption p_option, int p_value) = 0;
virtual void close() = 0;
virtual void set_refuse_new_connections(bool p_refuse) { /* Only used by dtls server */ }
virtual ~ENetGodotSocket(){};
virtual void set_refuse_new_connections(bool p_enable) {} /* Only used by dtls server */
virtual ~ENetGodotSocket() {}
};
class ENetDTLSClient;
@ -60,21 +59,19 @@ class ENetDTLSServer;
/// NetSocket interface
class ENetUDP : public ENetGodotSocket {
friend class ENetDTLSClient;
friend class ENetDTLSServer;
private:
Ref<NetSocket> sock;
IP_Address address;
uint16_t port;
bool bound;
uint16_t port = 0;
bool bound = false;
public:
ENetUDP() {
sock = Ref<NetSocket>(NetSocket::create());
IP::Type ip_type = IP::TYPE_ANY;
bound = false;
sock->open(NetSocket::TYPE_UDP, ip_type);
}
@ -95,8 +92,9 @@ public:
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) {
Error err = sock->poll(NetSocket::POLL_TYPE_IN, 0);
if (err != OK)
if (err != OK) {
return err;
}
return sock->recvfrom(p_buffer, p_len, r_read, r_ip, r_port);
}
@ -149,11 +147,10 @@ public:
/// DTLS Client ENet interface
class ENetDTLSClient : public ENetGodotSocket {
bool connected;
bool connected = false;
Ref<PacketPeerUDP> udp;
Ref<PacketPeerDTLS> dtls;
bool verify;
bool verify = false;
String for_hostname;
Ref<X509Certificate> cert;
@ -168,7 +165,6 @@ public:
if (p_base->bound) {
bind(p_base->address, p_base->port);
}
connected = false;
}
~ENetDTLSClient() {
@ -186,25 +182,29 @@ public:
connected = true;
}
dtls->poll();
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING)
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
return ERR_BUSY;
else if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED)
} else if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
return FAILED;
}
r_sent = p_len;
return dtls->put_packet(p_buffer, p_len);
}
Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) {
dtls->poll();
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING)
if (dtls->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
return ERR_BUSY;
if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED)
}
if (dtls->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
return FAILED;
}
int pc = dtls->get_available_packet_count();
if (pc == 0)
if (pc == 0) {
return ERR_BUSY;
else if (pc < 0)
} else if (pc < 0) {
return FAILED;
}
const uint8_t *buffer;
Error err = dtls->get_packet(&buffer, r_read);
@ -229,15 +229,13 @@ public:
/// DTLSServer - ENet interface
class ENetDTLSServer : public ENetGodotSocket {
Ref<DTLSServer> server;
Ref<UDPServer> udp_server;
Map<String, Ref<PacketPeerDTLS> > peers;
int last_service;
Map<String, Ref<PacketPeerDTLS>> peers;
int last_service = 0;
public:
ENetDTLSServer(ENetUDP *p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert) {
last_service = 0;
udp_server.instance();
p_base->close();
if (p_base->bound) {
@ -264,12 +262,13 @@ public:
ERR_FAIL_COND_V(!peers.has(key), ERR_UNAVAILABLE);
Ref<PacketPeerDTLS> peer = peers[key];
Error err = peer->put_packet(p_buffer, p_len);
if (err == OK)
if (err == OK) {
r_sent = p_len;
else if (err == ERR_BUSY)
} else if (err == ERR_BUSY) {
r_sent = 0;
else
} else {
r_sent = -1;
}
return err;
}
@ -291,13 +290,13 @@ public:
List<String> remove;
Error err = ERR_BUSY;
// TODO this needs to be fair!
for (Map<String, Ref<PacketPeerDTLS> >::Element *E = peers.front(); E; E = E->next()) {
for (Map<String, Ref<PacketPeerDTLS>>::Element *E = peers.front(); E; E = E->next()) {
Ref<PacketPeerDTLS> peer = E->get();
peer->poll();
if (peer->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING)
if (peer->get_status() == PacketPeerDTLS::STATUS_HANDSHAKING) {
continue;
else if (peer->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
} else if (peer->get_status() != PacketPeerDTLS::STATUS_CONNECTED) {
// Peer disconnected, removing it.
remove.push_back(E->key());
continue;
@ -336,7 +335,7 @@ public:
}
void close() {
for (Map<String, Ref<PacketPeerDTLS> >::Element *E = peers.front(); E; E = E->next()) {
for (Map<String, Ref<PacketPeerDTLS>>::Element *E = peers.front(); E; E = E->next()) {
E->get()->disconnect_from_peer();
}
peers.clear();
@ -348,7 +347,6 @@ public:
static enet_uint32 timeBase = 0;
int enet_initialize(void) {
return 0;
}
@ -356,22 +354,18 @@ void enet_deinitialize(void) {
}
enet_uint32 enet_host_random_seed(void) {
return (enet_uint32)OS::get_singleton()->get_unix_time();
}
enet_uint32 enet_time_get(void) {
return OS::get_singleton()->get_ticks_msec() - timeBase;
}
void enet_time_set(enet_uint32 newTimeBase) {
timeBase = OS::get_singleton()->get_ticks_msec() - newTimeBase;
}
int enet_address_set_host(ENetAddress *address, const char *name) {
IP_Address ip = IP::get_singleton()->resolve_hostname(name);
ERR_FAIL_COND_V(!ip.is_valid(), -1);
@ -380,24 +374,20 @@ int enet_address_set_host(ENetAddress *address, const char *name) {
}
void enet_address_set_ip(ENetAddress *address, const uint8_t *ip, size_t size) {
int len = size > 16 ? 16 : size;
memset(address->host, 0, 16);
memcpy(address->host, ip, len);
}
int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength) {
return -1;
}
int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength) {
return -1;
}
ENetSocket enet_socket_create(ENetSocketType type) {
ENetUDP *socket = memnew(ENetUDP);
return socket;
@ -421,7 +411,6 @@ void enet_host_refuse_new_connections(ENetHost *host, int p_refuse) {
}
int enet_socket_bind(ENetSocket socket, const ENetAddress *address) {
IP_Address ip;
if (address->wildcard) {
ip = IP_Address("*");
@ -443,8 +432,7 @@ void enet_socket_destroy(ENetSocket socket) {
}
int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBuffer *buffers, size_t bufferCount) {
ERR_FAIL_COND_V(address == NULL, -1);
ERR_FAIL_COND_V(address == nullptr, -1);
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
IP_Address dest;
@ -455,7 +443,7 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
// Create a single packet.
Vector<uint8_t> out;
uint8_t* w;
uint8_t *w;
int size = 0;
int pos = 0;
for (i = 0; i < bufferCount; i++) {
@ -472,7 +460,6 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
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
return 0;
}
@ -485,7 +472,6 @@ int enet_socket_send(ENetSocket socket, const ENetAddress *address, const ENetBu
}
int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buffers, size_t bufferCount) {
ERR_FAIL_COND_V(bufferCount != 1, -1);
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
@ -494,11 +480,13 @@ int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buf
IP_Address ip;
Error err = sock->recvfrom((uint8_t *)buffers[0].data, buffers[0].dataLength, read, ip, address->port);
if (err == ERR_BUSY)
if (err == ERR_BUSY) {
return 0;
}
if (err != OK)
if (err != OK) {
return -1;
}
enet_address_set_ip(address, ip.get_ipv6(), 16);
@ -507,47 +495,38 @@ int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buf
// Not implemented
int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout) {
return 0; // do we need this function?
}
int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
return -1; // do we need this function?
}
int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout) {
return -1;
}
int enet_socket_listen(ENetSocket socket, int backlog) {
return -1;
}
int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value) {
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
return sock->set_option(option, value);
}
int enet_socket_get_option(ENetSocket socket, ENetSocketOption option, int *value) {
return -1;
}
int enet_socket_connect(ENetSocket socket, const ENetAddress *address) {
return -1;
}
ENetSocket enet_socket_accept(ENetSocket socket, ENetAddress *address) {
return NULL;
return nullptr;
}
int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how) {
return -1;
}