Skip to content

Commit

Permalink
SendReliabilityLayer: make retransmit timeout less insane
Browse files Browse the repository at this point in the history
this hardcoded timeout really needs to be replaced by a calculation based on RTT, but that's a job for another time.
  • Loading branch information
dktapps committed Mar 4, 2024
1 parent ebd8b6c commit be2783b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/generic/SendReliabilityLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
use function array_push;
use function assert;
use function count;
use function microtime;
use function str_split;
use function strlen;
use function time;

final class SendReliabilityLayer{
private const DATAGRAM_MTU_OVERHEAD = 36 + Datagram::HEADER_SIZE; //IP header (20 bytes) + UDP header (8 bytes) + RakNet weird (8 bytes) = 36
private const MIN_POSSIBLE_PACKET_SIZE_LIMIT = Session::MIN_MTU_SIZE - self::DATAGRAM_MTU_OVERHEAD;
/**
* Delay in seconds before an unacked packet is retransmitted.
* TODO: Replace this with dynamic calculation based on roundtrip times (that's a complex task for another time)
*/
private const UNACKED_RETRANSMIT_DELAY = 2.0;

/** @var EncapsulatedPacket[] */
private array $sendQueue = [];
Expand Down Expand Up @@ -255,8 +260,9 @@ public function needsUpdate() : bool{
}

public function update() : void{
$retransmitOlderThan = microtime(true) - self::UNACKED_RETRANSMIT_DELAY;
foreach($this->reliableCache as $seq => $pk){
if($pk->getTimestamp() < (time() - 8)){
if($pk->getTimestamp() < $retransmitOlderThan){
//behave as if a NACK was received
array_push($this->resendQueue, ...$pk->getPackets());
unset($this->reliableCache[$seq]);
Expand Down

0 comments on commit be2783b

Please sign in to comment.