NetworkSession: fixed rate limit not being increased correctly on Windows

due to the 15ms scheduler interval, the server will often sleep 45ms instead of 50ms, which causes the budget not to get updated.
This commit is contained in:
Dylan K. Taylor 2023-01-09 00:01:56 +00:00
parent bb3f87f862
commit 992cb06da6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -159,6 +159,7 @@ class NetworkSession{
* @see self::INCOMING_PACKET_BATCH_MAX_BUDGET
*/
private int $incomingPacketBatchBudget = self::INCOMING_PACKET_BATCH_MAX_BUDGET;
private int $lastPacketBudgetUpdateTimeNs;
private \PrefixedLogger $logger;
private ?Player $player = null;
@ -197,8 +198,6 @@ class NetworkSession{
*/
private ObjectSet $disposeHooks;
private int $lastUpdateTimeNs;
public function __construct(
private Server $server,
private NetworkSessionManager $manager,
@ -219,7 +218,7 @@ class NetworkSession{
$this->disposeHooks = new ObjectSet();
$this->connectTime = time();
$this->lastUpdateTimeNs = hrtime(true);
$this->lastPacketBudgetUpdateTimeNs = hrtime(true);
$this->setHandler(new SessionStartPacketHandler(
$this->server,
@ -1140,10 +1139,6 @@ class NetworkSession{
}
public function tick() : void{
$nowNs = hrtime(true);
$timeSinceLastUpdateNs = $nowNs - $this->lastUpdateTimeNs;
$this->lastUpdateTimeNs = $nowNs;
if($this->info === null){
if(time() >= $this->connectTime + 10){
$this->disconnect("Login timeout");
@ -1165,15 +1160,16 @@ class NetworkSession{
}
$this->flushSendBuffer();
$ticksSinceLastUpdate = intdiv($timeSinceLastUpdateNs, 50_000_000);
if($ticksSinceLastUpdate > 0){
if($ticksSinceLastUpdate > 1){
$this->logger->debug("Adding packet budget for $ticksSinceLastUpdate ticks (current budget is $this->incomingPacketBatchBudget)");
}
$nowNs = hrtime(true);
$timeSinceLastUpdateNs = $nowNs - $this->lastPacketBudgetUpdateTimeNs;
if($timeSinceLastUpdateNs > 50_000_000){
$ticksSinceLastUpdate = intdiv($timeSinceLastUpdateNs, 50_000_000);
$this->incomingPacketBatchBudget = min(
$this->incomingPacketBatchBudget + (self::INCOMING_PACKET_BATCH_PER_TICK * 2 * $ticksSinceLastUpdate),
self::INCOMING_PACKET_BATCH_MAX_BUDGET
);
$this->lastPacketBudgetUpdateTimeNs = $nowNs;
}
}
}