NetworkSession: tidy up common disconnection logic

This commit is contained in:
Dylan K. Taylor 2022-12-24 20:36:18 +00:00
parent 80832ff763
commit b1c0eae1f6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -544,6 +544,8 @@ class NetworkSession{
$this->disconnectGuard = true;
$func();
$this->disconnectGuard = false;
$this->flushSendBuffer(true);
$this->sender->close("");
foreach($this->disposeHooks as $callback){
$callback();
}
@ -567,10 +569,12 @@ class NetworkSession{
*/
public function disconnect(string $reason, bool $notify = true) : void{
$this->tryDisconnect(function() use ($reason, $notify) : void{
if($notify){
$this->sendDataPacket(DisconnectPacket::create($reason));
}
if($this->player !== null){
$this->player->onPostDisconnect($reason, null);
}
$this->doServerDisconnect($reason, $notify);
}, $reason);
}
@ -583,7 +587,6 @@ class NetworkSession{
if($this->player !== null){
$this->player->onPostDisconnect($reason, null);
}
$this->doServerDisconnect($reason, false);
}, $reason);
}
@ -592,21 +595,10 @@ class NetworkSession{
*/
public function onPlayerDestroyed(string $reason) : void{
$this->tryDisconnect(function() use ($reason) : void{
$this->doServerDisconnect($reason, true);
$this->sendDataPacket(DisconnectPacket::create($reason));
}, $reason);
}
/**
* Internal helper function used to handle server disconnections.
*/
private function doServerDisconnect(string $reason, bool $notify = true) : void{
if($notify){
$this->sendDataPacket(DisconnectPacket::create($reason !== "" ? $reason : null), true);
}
$this->sender->close($notify ? $reason : "");
}
/**
* Called by the network interface to close the session when the client disconnects without server input, for
* example in a timeout condition or voluntary client disconnect.