Simplify NetworkSession ticking

we need to tick sessions every tick anyway, because other stuff is happening (e.g. sync attributes/entity metadata, batch buffer flush).
This commit is contained in:
Dylan K. Taylor 2021-08-02 14:31:30 +01:00
parent eb23d27004
commit 01b48a21d9
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 8 additions and 21 deletions

View File

@ -31,15 +31,13 @@ class NetworkSessionManager{
/** @var NetworkSession[] */
private $sessions = [];
/** @var NetworkSession[] */
private $updateSessions = [];
/**
* Adds a network session to the manager. This should only be called on session creation.
*/
public function add(NetworkSession $session) : void{
$idx = spl_object_id($session);
$this->sessions[$idx] = $this->updateSessions[$idx] = $session;
$this->sessions[$idx] = $session;
}
/**
@ -48,14 +46,7 @@ class NetworkSessionManager{
*/
public function remove(NetworkSession $session) : void{
$idx = spl_object_id($session);
unset($this->sessions[$idx], $this->updateSessions[$idx]);
}
/**
* Requests an update to be scheduled on the given network session at the next tick.
*/
public function scheduleUpdate(NetworkSession $session) : void{
$this->updateSessions[spl_object_id($session)] = $session;
unset($this->sessions[$idx]);
}
/**
@ -72,9 +63,10 @@ class NetworkSessionManager{
* Updates all sessions which need it.
*/
public function tick() : void{
foreach($this->updateSessions as $k => $session){
if(!$session->tick()){
unset($this->updateSessions[$k]);
foreach($this->sessions as $k => $session){
$session->tick();
if(!$session->isConnected()){
unset($this->sessions[$k]);
}
}
}
@ -87,6 +79,5 @@ class NetworkSessionManager{
$session->disconnect($reason);
}
$this->sessions = [];
$this->updateSessions = [];
}
}

View File

@ -421,7 +421,6 @@ class NetworkSession{
$timings->startTiming();
try{
$this->sendBuffer[] = $packet;
$this->manager->scheduleUpdate($this); //schedule flush at end of tick
}finally{
$timings->stopTiming();
}
@ -1021,14 +1020,13 @@ class NetworkSession{
$this->sendDataPacket(SetTitlePacket::setAnimationTimes($fadeIn, $stay, $fadeOut));
}
public function tick() : bool{
public function tick() : void{
if($this->info === null){
if(time() >= $this->connectTime + 10){
$this->disconnect("Login timeout");
return false;
}
return true; //keep ticking until timeout
return;
}
if($this->player !== null){
@ -1044,7 +1042,5 @@ class NetworkSession{
}
$this->flushSendBuffer();
return true;
}
}