Skip to content

Commit

Permalink
SendReliabilityLayer: Do not resend unreliable packets
Browse files Browse the repository at this point in the history
the entire point of unreliable packets is that they don't have to be resent if they disappear. Previously to this commit, RakLib would unconditionally resend the whole datagram when nacked, wasting bandwidth.
  • Loading branch information
dktapps committed Apr 8, 2020
1 parent 2d6d87f commit d0f019e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
48 changes: 48 additions & 0 deletions src/generic/ResendQueueEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* RakLib network library
*
*
* This project is not affiliated with Jenkins Software LLC nor RakNet.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*/

declare(strict_types=1);

namespace raklib\generic;

use raklib\protocol\EncapsulatedPacket;
use function microtime;

final class ResendQueueEntry{

/** @var EncapsulatedPacket[] */
private $packets;
/** @var float */
private $timestamp;

/**
* @param EncapsulatedPacket[] $packets
*/
public function __construct(array $packets){
$this->packets = $packets;
$this->timestamp = microtime(true);
}

/**
* @return EncapsulatedPacket[]
*/
public function getPackets() : array{
return $this->packets;
}

public function getTimestamp() : float{
return $this->timestamp;
}
}
28 changes: 20 additions & 8 deletions src/generic/SendReliabilityLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use function array_fill;
use function assert;
use function count;
use function microtime;
use function str_split;
use function strlen;
use function time;
Expand Down Expand Up @@ -67,7 +66,7 @@ final class SendReliabilityLayer{
/** @var Datagram[] */
private $packetToSend = [];

/** @var Datagram[] */
/** @var ResendQueueEntry[] */
private $recoveryQueue = [];

/** @var int[][] */
Expand All @@ -93,9 +92,17 @@ private function sendDatagram(Datagram $datagram) : void{
unset($this->recoveryQueue[$datagram->seqNumber]);
}
$datagram->seqNumber = $this->sendSeqNumber++;
$datagram->sendTime = microtime(true);
$this->recoveryQueue[$datagram->seqNumber] = $datagram;
($this->sendDatagramCallback)($datagram);

$resendable = [];
foreach($datagram->packets as $pk){
if(PacketReliability::isReliable($pk->reliability)){
$resendable[] = $pk;
}
}
if(count($resendable) !== 0){
$this->recoveryQueue[$datagram->seqNumber] = new ResendQueueEntry($resendable);
}
}

public function sendQueue() : void{
Expand Down Expand Up @@ -178,7 +185,7 @@ public function onACK(ACK $packet) : void{
$packet->decode();
foreach($packet->packets as $seq){
if(isset($this->recoveryQueue[$seq])){
foreach($this->recoveryQueue[$seq]->packets as $pk){
foreach($this->recoveryQueue[$seq]->getPackets() as $pk){
if($pk->identifierACK !== null and $pk->messageIndex !== null){
unset($this->needACK[$pk->identifierACK][$pk->messageIndex]);
if(count($this->needACK[$pk->identifierACK]) === 0){
Expand All @@ -196,7 +203,10 @@ public function onNACK(NACK $packet) : void{
$packet->decode();
foreach($packet->packets as $seq){
if(isset($this->recoveryQueue[$seq])){
$this->packetToSend[] = $this->recoveryQueue[$seq];
//TODO: group resends if the resulting datagram is below the MTU
$resend = new Datagram();
$resend->packets = $this->recoveryQueue[$seq]->getPackets();
$this->packetToSend[] = $resend;
unset($this->recoveryQueue[$seq]);
}
}
Expand Down Expand Up @@ -228,8 +238,10 @@ public function update() : void{
}

foreach($this->recoveryQueue as $seq => $pk){
if($pk->sendTime < (time() - 8)){
$this->packetToSend[] = $pk;
if($pk->getTimestamp() < (time() - 8)){
$resend = new Datagram();
$resend->packets = $pk->getPackets();
$this->packetToSend[] = $resend;
unset($this->recoveryQueue[$seq]);
}else{
break;
Expand Down
3 changes: 0 additions & 3 deletions src/protocol/Packet.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ abstract class Packet extends BinaryStream{
/** @var int */
public static $ID = -1;

/** @var float|null */
public $sendTime;

/**
* @return string
* @throws BinaryDataException
Expand Down

0 comments on commit d0f019e

Please sign in to comment.