diff --git a/src/pc/network/packets/packet_reliable.c b/src/pc/network/packets/packet_reliable.c index b80a4f77..285286a9 100644 --- a/src/pc/network/packets/packet_reliable.c +++ b/src/pc/network/packets/packet_reliable.c @@ -1,11 +1,14 @@ #include #include "../network.h" +#include "pc/debuglog.h" #define RELIABLE_RESEND_RATE 0.10f +#define MAX_RESEND_ATTEMPTS 20 struct PacketLinkedList { struct Packet p; clock_t lastSend; + int sendAttempts; struct PacketLinkedList* prev; struct PacketLinkedList* next; }; @@ -65,6 +68,7 @@ void network_remember_reliable(struct Packet* p) { node->p = *p; node->p.sent = true; node->lastSend = clock(); + node->sendAttempts = 1; node->prev = NULL; node->next = NULL; @@ -87,10 +91,19 @@ void network_update_reliable(void) { struct PacketLinkedList* node = head; while (node != NULL) { float elapsed = (clock() - node->lastSend) / CLOCKS_PER_SEC; - if (elapsed > RELIABLE_RESEND_RATE) { + float maxElapsed = (node->sendAttempts * node->sendAttempts * RELIABLE_RESEND_RATE) / ((float)MAX_RESEND_ATTEMPTS); + if (elapsed > maxElapsed) { // resend network_send(&node->p); node->lastSend = clock(); + node->sendAttempts++; + if (node->sendAttempts >= MAX_RESEND_ATTEMPTS) { + struct PacketLinkedList* next = node->next; + remove_node_from_list(node); + node = next; + LOG_ERROR("giving up on reliable packet"); + continue; + } } node = node->next; }