@@ -722,15 +722,16 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
722
722
723
723
// Queue the packet to be sent
724
724
err = enet_peer_send (peer , channelId , enetPacket );
725
+ bool packetQueued = (err == 0 );
725
726
726
727
// 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 );
729
730
730
731
// 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 ) {
732
733
// 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 ++ ) {
734
735
// Break on disconnected, acked/freed, or sent (pending ack).
735
736
if (peer -> state != ENET_PEER_STATE_CONNECTED || packetFreed || isPacketSentWaitingForAck (enetPacket )) {
736
737
break ;
@@ -742,10 +743,10 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
742
743
PltLockMutex (& enetMutex );
743
744
744
745
// Try to send the packet again
745
- enet_host_service (client , NULL , 0 );
746
+ err = enet_host_service (client , NULL , 0 );
746
747
}
747
748
748
- if (peer -> state == ENET_PEER_STATE_CONNECTED && !packetFreed && !isPacketSentWaitingForAck (enetPacket )) {
749
+ if (err >= 0 && peer -> state == ENET_PEER_STATE_CONNECTED && !packetFreed && !isPacketSentWaitingForAck (enetPacket )) {
749
750
Limelog ("Control message took over 10 ms to send (net latency: %u ms | packet loss: %f%%)\n" ,
750
751
peer -> roundTripTime , peer -> packetLoss / (float )ENET_PEER_PACKET_LOSS_SCALE );
751
752
}
@@ -762,7 +763,9 @@ static bool sendMessageEnet(short ptype, short paylen, const void* payload, uint
762
763
763
764
if (err < 0 ) {
764
765
Limelog ("Failed to send ENet control packet\n" );
765
- enet_packet_destroy (enetPacket );
766
+ if (!packetQueued ) {
767
+ enet_packet_destroy (enetPacket );
768
+ }
766
769
return false;
767
770
}
768
771
0 commit comments