diff --git a/src/pocketmine/network/NetworkInterface.php b/src/pocketmine/network/NetworkInterface.php index 8ef8484637..47241c341f 100644 --- a/src/pocketmine/network/NetworkInterface.php +++ b/src/pocketmine/network/NetworkInterface.php @@ -26,8 +26,6 @@ declare(strict_types=1); */ namespace pocketmine\network; -use pocketmine\network\mcpe\NetworkSession; - /** * Network interfaces are transport layers which can be used to transmit packets between the server and clients. */ @@ -38,23 +36,6 @@ interface NetworkInterface{ */ public function start() : void; - /** - * Sends a packet to the interface, returns an unique identifier for the packet if $needACK is true - * - * @param NetworkSession $session - * @param string $payload - * @param bool $immediate - */ - public function putPacket(NetworkSession $session, string $payload, bool $immediate = true) : void; - - /** - * Terminates the connection - * - * @param NetworkSession $session - * @param string $reason - */ - public function close(NetworkSession $session, string $reason = "unknown reason") : void; - /** * @param string $name */ diff --git a/src/pocketmine/network/mcpe/NetworkSession.php b/src/pocketmine/network/mcpe/NetworkSession.php index 6f91972d44..2ec17d0f63 100644 --- a/src/pocketmine/network/mcpe/NetworkSession.php +++ b/src/pocketmine/network/mcpe/NetworkSession.php @@ -137,10 +137,14 @@ class NetworkSession{ /** @var InventoryManager|null */ private $invManager = null; - public function __construct(Server $server, NetworkSessionManager $manager, NetworkInterface $interface, string $ip, int $port){ + /** @var PacketSender */ + private $sender; + + public function __construct(Server $server, NetworkSessionManager $manager, NetworkInterface $interface, PacketSender $sender, string $ip, int $port){ $this->server = $server; $this->manager = $manager; $this->interface = $interface; + $this->sender = $sender; $this->ip = $ip; $this->port = $port; @@ -431,7 +435,7 @@ class NetworkSession{ $payload = $this->cipher->encrypt($payload); Timings::$playerNetworkSendEncryptTimer->stopTiming(); } - $this->interface->putPacket($this, $payload, $immediate); + $this->sender->send($payload, $immediate); } private function tryDisconnect(\Closure $func, string $reason) : void{ @@ -506,7 +510,7 @@ class NetworkSession{ $this->sendDataPacket($reason === "" ? DisconnectPacket::silent() : DisconnectPacket::message($reason), true); } - $this->interface->close($this, $notify ? $reason : ""); + $this->sender->close($notify ? $reason : ""); } /** diff --git a/src/pocketmine/network/mcpe/PacketSender.php b/src/pocketmine/network/mcpe/PacketSender.php new file mode 100644 index 0000000000..857293ffb5 --- /dev/null +++ b/src/pocketmine/network/mcpe/PacketSender.php @@ -0,0 +1,42 @@ +sessions[$sessionId])){ $session = $this->sessions[$sessionId]; - unset($this->identifiers[spl_object_id($session)]); unset($this->sessions[$sessionId]); $session->onClientDisconnect($reason); } } - public function close(NetworkSession $session, string $reason = "unknown reason") : void{ - if(isset($this->identifiers[$h = spl_object_id($session)])){ - unset($this->sessions[$this->identifiers[$h]]); - $this->interface->closeSession($this->identifiers[$h], $reason); - unset($this->identifiers[$h]); + public function close(int $sessionId, string $reason = "unknown reason") : void{ + if(isset($this->sessions[$sessionId])){ + unset($this->sessions[$sessionId]); + $this->interface->closeSession($sessionId, $reason); } } @@ -139,9 +133,8 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{ } public function openSession(int $sessionId, string $address, int $port, int $clientID) : void{ - $session = new NetworkSession($this->server, $this->network->getSessionManager(), $this, $address, $port); + $session = new NetworkSession($this->server, $this->network->getSessionManager(), $this, new RakLibPacketSender($sessionId, $this), $address, $port); $this->sessions[$sessionId] = $session; - $this->identifiers[spl_object_id($session)] = $sessionId; } public function handleEncapsulated(int $sessionId, EncapsulatedPacket $packet, int $flags) : void{ @@ -225,16 +218,14 @@ class RakLibInterface implements ServerInstance, AdvancedNetworkInterface{ } } - public function putPacket(NetworkSession $session, string $payload, bool $immediate = true) : void{ - if(isset($this->identifiers[$h = spl_object_id($session)])){ - $identifier = $this->identifiers[$h]; - + public function putPacket(int $sessionId, string $payload, bool $immediate = true) : void{ + if(isset($this->sessions[$sessionId])){ $pk = new EncapsulatedPacket(); $pk->buffer = self::MCPE_RAKNET_PACKET_ID . $payload; $pk->reliability = PacketReliability::RELIABLE_ORDERED; $pk->orderChannel = 0; - $this->interface->sendEncapsulated($identifier, $pk, ($immediate ? RakLib::PRIORITY_IMMEDIATE : RakLib::PRIORITY_NORMAL)); + $this->interface->sendEncapsulated($sessionId, $pk, ($immediate ? RakLib::PRIORITY_IMMEDIATE : RakLib::PRIORITY_NORMAL)); } } diff --git a/src/pocketmine/network/mcpe/RakLibPacketSender.php b/src/pocketmine/network/mcpe/RakLibPacketSender.php new file mode 100644 index 0000000000..90a8c3433e --- /dev/null +++ b/src/pocketmine/network/mcpe/RakLibPacketSender.php @@ -0,0 +1,53 @@ +sessionId = $sessionId; + $this->handler = $handler; + } + + public function send(string $payload, bool $immediate) : void{ + if(!$this->closed){ + $this->handler->putPacket($this->sessionId, $payload, $immediate); + } + } + + public function close(string $reason = "unknown reason") : void{ + if(!$this->closed){ + $this->closed = true; + $this->handler->closeSession($this->sessionId, $reason); + } + } +}