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

[3.x] [Net] ENet poll optimizations, fragmented unreliable transfer. #53130

Merged
merged 2 commits into from
Dec 16, 2021
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
36 changes: 23 additions & 13 deletions modules/enet/networked_multiplayer_enet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,23 @@ void NetworkedMultiplayerENet::poll() {

_pop_current_packet();

if (!host || !active) { // Might be disconnected
return;
}

ENetEvent event;
/* Keep servicing until there are no available events left in queue. */
while (true) {
if (!host || !active) { // Might have been disconnected while emitting a notification
return;
}
int ret = enet_host_service(host, &event, 0);

int ret = enet_host_service(host, &event, 0);
if (ret < 0) {
ERR_FAIL_MSG("Enet host service error");
} else if (ret == 0) {
return; // No events
}

if (ret < 0) {
// Error, do something?
break;
} else if (ret == 0) {
break;
/* Keep servicing until there are no available events left in the queue. */
do {
if (!host || !active) { // Check again after every event
return;
}

switch (event.type) {
Expand Down Expand Up @@ -436,7 +439,7 @@ void NetworkedMultiplayerENet::poll() {
// Do nothing
} break;
}
}
} while (enet_host_check_events(host, &event) > 0);
}

bool NetworkedMultiplayerENet::is_server() const {
Expand Down Expand Up @@ -546,9 +549,10 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer
packet_flags = ENET_PACKET_FLAG_UNSEQUENCED;
}
channel = SYSCH_UNRELIABLE;
packet_flags |= ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
} break;
case TRANSFER_MODE_UNRELIABLE_ORDERED: {
packet_flags = 0;
packet_flags = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT;
channel = SYSCH_UNRELIABLE;
} break;
case TRANSFER_MODE_RELIABLE: {
Expand All @@ -561,6 +565,12 @@ Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer, int p_buffer
channel = transfer_channel;
}

#ifdef DEBUG_ENABLED
if ((packet_flags & ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT) && p_buffer_size + 8 > ENET_HOST_DEFAULT_MTU) {
WARN_PRINT_ONCE(vformat("Sending %d bytes unrealiably which is above the MTU (%d), this will result in higher packet loss", p_buffer_size + 8, host->mtu));
}
#endif

Map<int, ENetPeer *>::Element *E = nullptr;

if (target_peer != 0) {
Expand Down