Added exponential backoff to reliable packets

This commit is contained in:
MysterD 2020-09-15 22:45:17 -07:00
parent 275d6444b1
commit eaaab8850f
1 changed files with 14 additions and 1 deletions

View File

@ -1,11 +1,14 @@
#include <stdio.h> #include <stdio.h>
#include "../network.h" #include "../network.h"
#include "pc/debuglog.h"
#define RELIABLE_RESEND_RATE 0.10f #define RELIABLE_RESEND_RATE 0.10f
#define MAX_RESEND_ATTEMPTS 20
struct PacketLinkedList { struct PacketLinkedList {
struct Packet p; struct Packet p;
clock_t lastSend; clock_t lastSend;
int sendAttempts;
struct PacketLinkedList* prev; struct PacketLinkedList* prev;
struct PacketLinkedList* next; struct PacketLinkedList* next;
}; };
@ -65,6 +68,7 @@ void network_remember_reliable(struct Packet* p) {
node->p = *p; node->p = *p;
node->p.sent = true; node->p.sent = true;
node->lastSend = clock(); node->lastSend = clock();
node->sendAttempts = 1;
node->prev = NULL; node->prev = NULL;
node->next = NULL; node->next = NULL;
@ -87,10 +91,19 @@ void network_update_reliable(void) {
struct PacketLinkedList* node = head; struct PacketLinkedList* node = head;
while (node != NULL) { while (node != NULL) {
float elapsed = (clock() - node->lastSend) / CLOCKS_PER_SEC; 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 // resend
network_send(&node->p); network_send(&node->p);
node->lastSend = clock(); 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; node = node->next;
} }