Skip to content

Commit fb2b103

Browse files
committed
Fix propagation of errors from enet_host_service() in sendMessageEnet()
1 parent e8113f0 commit fb2b103

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/ControlStream.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,16 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
722722

723723
// Queue the packet to be sent
724724
err = enet_peer_send(peer, channelId, enetPacket);
725+
bool packetQueued = (err == 0);
725726

726727
// If there is no more data coming soon, send the packet now
727-
if (!moreData) {
728-
enet_host_service(client, NULL, 0);
728+
if (!moreData && packetQueued) {
729+
err = enet_host_service(client, NULL, 0);
729730

730731
// Wait until the packet is actually sent to provide backpressure on senders
731-
if (err == 0 && (flags & ENET_PACKET_FLAG_RELIABLE)) {
732+
if (flags & ENET_PACKET_FLAG_RELIABLE) {
732733
// Don't wait longer than 10 milliseconds to avoid blocking callers for too long
733-
for (int i = 0; i < 10; i++) {
734+
for (int i = 0; err >= 0 && i < 10; i++) {
734735
// Break on disconnected, acked/freed, or sent (pending ack).
735736
if (peer->state != ENET_PEER_STATE_CONNECTED || packetFreed || isPacketSentWaitingForAck(enetPacket)) {
736737
break;
@@ -742,10 +743,10 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
742743
PltLockMutex(&enetMutex);
743744

744745
// Try to send the packet again
745-
enet_host_service(client, NULL, 0);
746+
err = enet_host_service(client, NULL, 0);
746747
}
747748

748-
if (peer->state == ENET_PEER_STATE_CONNECTED && !packetFreed && !isPacketSentWaitingForAck(enetPacket)) {
749+
if (err >= 0 && peer->state == ENET_PEER_STATE_CONNECTED && !packetFreed && !isPacketSentWaitingForAck(enetPacket)) {
749750
Limelog("Control message took over 10 ms to send (net latency: %u ms | packet loss: %f%%)\n",
750751
peer->roundTripTime, peer->packetLoss / (float)ENET_PEER_PACKET_LOSS_SCALE);
751752
}
@@ -762,7 +763,9 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
762763

763764
if (err < 0) {
764765
Limelog("Failed to send ENet control packet\n");
765-
enet_packet_destroy(enetPacket);
766+
if (!packetQueued) {
767+
enet_packet_destroy(enetPacket);
768+
}
766769
return false;
767770
}
768771

0 commit comments

Comments
 (0)