Improve reliable packet timer/resend rate

This commit is contained in:
MysterD 2022-04-29 21:27:23 -07:00
parent ef51666c98
commit 58cee9098b
1 changed files with 15 additions and 7 deletions

View File

@ -3,8 +3,8 @@
#include "pc/utils/misc.h" #include "pc/utils/misc.h"
#include "pc/debuglog.h" #include "pc/debuglog.h"
#define RELIABLE_RESEND_RATE 0.33f #define RELIABLE_RESEND_RATE 0.07f
#define MAX_RESEND_ATTEMPTS 10 #define MAX_RESEND_ATTEMPTS 15
struct PacketLinkedList { struct PacketLinkedList {
struct Packet p; struct Packet p;
@ -117,7 +117,7 @@ void network_remember_reliable(struct Packet* p) {
tail = node; tail = node;
} }
static float network_adjust_max_elapsed(enum PacketType packetType, float maxElapsed) { static float adjust_max_elapsed(enum PacketType packetType, float maxElapsed) {
switch (packetType) { switch (packetType) {
case PACKET_DOWNLOAD_REQUEST: case PACKET_DOWNLOAD_REQUEST:
case PACKET_DOWNLOAD: case PACKET_DOWNLOAD:
@ -126,18 +126,26 @@ static float network_adjust_max_elapsed(enum PacketType packetType, float maxEla
case PACKET_MOD_LIST_ENTRY: case PACKET_MOD_LIST_ENTRY:
case PACKET_MOD_LIST_FILE: case PACKET_MOD_LIST_FILE:
case PACKET_MOD_LIST_DONE: case PACKET_MOD_LIST_DONE:
return 0.2f + maxElapsed * 2.0f; return MIN(0.2f + maxElapsed * 2.0f, 5);
default: default:
return maxElapsed; return MIN(maxElapsed, 5);
} }
} }
static float get_max_elapsed_time(int sendAttempts) {
f32 scalar = (sendAttempts / MAX_RESEND_ATTEMPTS);
scalar *= scalar;
f32 aScalar = 1.0 - scalar;
f32 interp = sendAttempts * aScalar + (sendAttempts * sendAttempts) * scalar;
return interp * RELIABLE_RESEND_RATE;
}
void network_update_reliable(void) { void network_update_reliable(void) {
struct PacketLinkedList* node = head; struct PacketLinkedList* node = head;
while (node != NULL) { while (node != NULL) {
float elapsed = (clock_elapsed() - node->lastSend); float elapsed = (clock_elapsed() - node->lastSend);
float maxElapsed = (node->sendAttempts * node->sendAttempts * RELIABLE_RESEND_RATE) / ((float)MAX_RESEND_ATTEMPTS); float maxElapsed = get_max_elapsed_time(node->sendAttempts);
maxElapsed = network_adjust_max_elapsed(node->p.packetType, maxElapsed); maxElapsed = adjust_max_elapsed(node->p.packetType, maxElapsed);
if (elapsed > maxElapsed) { if (elapsed > maxElapsed) {
if (node->p.packetType == PACKET_JOIN_REQUEST && gNetworkPlayerServer != NULL) { if (node->p.packetType == PACKET_JOIN_REQUEST && gNetworkPlayerServer != NULL) {
node->p.localIndex = gNetworkPlayerServer->localIndex; node->p.localIndex = gNetworkPlayerServer->localIndex;