Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENet] Better handle disconnected peers in DTLS server #95067

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions thirdparty/enet/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,23 @@ class ENetDTLSServer : public ENetGodotSocket {

Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) {
String key = String(p_ip) + ":" + itos(p_port);
ERR_FAIL_COND_V(!peers.has(key), ERR_UNAVAILABLE);
if (unlikely(!peers.has(key))) {
// The peer might have been disconnected due to a DTLS error.
// We need to wait for it to time out, just mark the packet as sent.
r_sent = p_len;
return OK;
}
Ref<PacketPeerDTLS> peer = peers[key];
Error err = peer->put_packet(p_buffer, p_len);
if (err == OK) {
r_sent = p_len;
} else if (err == ERR_BUSY) {
r_sent = 0;
} else {
r_sent = -1;
// The peer might have been disconnected due to a DTLS error.
// We need to wait for it to time out, just mark the packet as sent.
r_sent = p_len;
return OK;
}
return err;
}
Expand All @@ -331,7 +339,7 @@ class ENetDTLSServer : public ENetGodotSocket {
Error err = ERR_BUSY;
// TODO this needs to be fair!

for (KeyValue<String, Ref<PacketPeerDTLS>> & E : peers) {
for (KeyValue<String, Ref<PacketPeerDTLS>> &E : peers) {
Ref<PacketPeerDTLS> peer = E.value;
peer->poll();

Expand All @@ -349,7 +357,8 @@ class ENetDTLSServer : public ENetGodotSocket {
if (err != OK || p_len < r_read) {
// Something wrong with this peer, removing it.
remove.push_back(E.key);
err = FAILED;
err = ERR_BUSY;
r_read = 0;
continue;
}

Expand Down Expand Up @@ -549,7 +558,7 @@ int enet_socket_receive(ENetSocket socket, ENetAddress *address, ENetBuffer *buf
return read;
}

int enet_socket_get_address (ENetSocket socket, ENetAddress * address) {
int enet_socket_get_address(ENetSocket socket, ENetAddress *address) {
IPAddress ip;
uint16_t port;
ENetGodotSocket *sock = (ENetGodotSocket *)socket;
Expand Down
Loading