Add NetworkSession->getDisplayName(), clean up ip/port ad-hoc usages

This commit is contained in:
Dylan K. Taylor 2019-01-20 11:05:12 +00:00
parent 842eb0200e
commit 5d8568b1a4

View File

@ -139,6 +139,10 @@ class NetworkSession{
return $this->port; return $this->port;
} }
public function getDisplayName() : string{
return ($this->player !== null and $this->player->getName() !== "") ? $this->player->getName() : $this->ip . " " . $this->port;
}
/** /**
* Returns the last recorded ping measurement for this session, in milliseconds. * Returns the last recorded ping measurement for this session, in milliseconds.
* *
@ -181,7 +185,7 @@ class NetworkSession{
try{ try{
$payload = $this->cipher->decrypt($payload); $payload = $this->cipher->decrypt($payload);
}catch(\UnexpectedValueException $e){ }catch(\UnexpectedValueException $e){
$this->server->getLogger()->debug("Encrypted packet from " . $this->ip . " " . $this->port . ": " . bin2hex($payload)); $this->server->getLogger()->debug("Encrypted packet from " . $this->getDisplayName() . ": " . bin2hex($payload));
throw new BadPacketException("Packet decryption error: " . $e->getMessage(), 0, $e); throw new BadPacketException("Packet decryption error: " . $e->getMessage(), 0, $e);
}finally{ }finally{
Timings::$playerNetworkReceiveDecryptTimer->stopTiming(); Timings::$playerNetworkReceiveDecryptTimer->stopTiming();
@ -192,7 +196,7 @@ class NetworkSession{
try{ try{
$stream = new PacketStream(NetworkCompression::decompress($payload)); $stream = new PacketStream(NetworkCompression::decompress($payload));
}catch(\ErrorException $e){ }catch(\ErrorException $e){
$this->server->getLogger()->debug("Failed to decompress packet from " . $this->ip . " " . $this->port . ": " . bin2hex($payload)); $this->server->getLogger()->debug("Failed to decompress packet from " . $this->getDisplayName() . ": " . bin2hex($payload));
//TODO: this isn't incompatible game version if we already established protocol version //TODO: this isn't incompatible game version if we already established protocol version
throw new BadPacketException("Compressed packet batch decode error (incompatible game version?)", 0, $e); throw new BadPacketException("Compressed packet batch decode error (incompatible game version?)", 0, $e);
}finally{ }finally{
@ -203,7 +207,7 @@ class NetworkSession{
try{ try{
$buf = $stream->getString(); $buf = $stream->getString();
}catch(BinaryDataException $e){ }catch(BinaryDataException $e){
$this->server->getLogger()->debug("Packet batch from " . $this->ip . " " . $this->port . ": " . bin2hex($stream->getBuffer())); $this->server->getLogger()->debug("Packet batch from " . $this->getDisplayName() . ": " . bin2hex($stream->getBuffer()));
throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e); throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e);
} }
$this->handleDataPacket(PacketPool::getPacket($buf)); $this->handleDataPacket(PacketPool::getPacket($buf));
@ -226,7 +230,7 @@ class NetworkSession{
try{ try{
$packet->decode(); $packet->decode();
}catch(BadPacketException $e){ }catch(BadPacketException $e){
$this->server->getLogger()->debug($packet->getName() . " from " . $this->ip . " " . $this->port . ": " . bin2hex($packet->getBuffer())); $this->server->getLogger()->debug($packet->getName() . " from " . $this->getDisplayName() . ": " . bin2hex($packet->getBuffer()));
throw $e; throw $e;
} }
if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){ if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
@ -237,7 +241,7 @@ class NetworkSession{
$ev = new DataPacketReceiveEvent($this->player, $packet); $ev = new DataPacketReceiveEvent($this->player, $packet);
$ev->call(); $ev->call();
if(!$ev->isCancelled() and !$packet->handle($this->handler)){ if(!$ev->isCancelled() and !$packet->handle($this->handler)){
$this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->player->getName() . ": 0x" . bin2hex($packet->getBuffer())); $this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->getDisplayName() . ": 0x" . bin2hex($packet->getBuffer()));
} }
$timings->stopTiming(); $timings->stopTiming();
@ -393,7 +397,7 @@ class NetworkSession{
$this->cipher = new NetworkCipher($encryptionKey); $this->cipher = new NetworkCipher($encryptionKey);
$this->setHandler(new HandshakeSessionHandler($this)); $this->setHandler(new HandshakeSessionHandler($this));
$this->server->getLogger()->debug("Enabled encryption for $this->ip $this->port"); $this->server->getLogger()->debug("Enabled encryption for " . $this->getDisplayName());
} }
public function onLoginSuccess() : void{ public function onLoginSuccess() : void{