Added exponential backoff to reliable packets

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

View File

@ -1,11 +1,14 @@
#include <stdio.h>
#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;
}