mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-26 05:04:57 +00:00
DataPacketSendEvent and DataPacketReceiveEvent will no longer capture BatchPackets In most places strings are now used instead of DataPackets, to remove limitations on what data can be sent to a network interface Removed CraftingManager's cyclic dependency on Server There is a lot more work to do aside from this, but this commit is intended to clean up what is necessary to fix the handling of BatchPacket.
191 lines
4.7 KiB
PHP
191 lines
4.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Network-related classes
|
|
*/
|
|
namespace pocketmine\network;
|
|
|
|
use pocketmine\event\server\NetworkInterfaceCrashEvent;
|
|
use pocketmine\event\server\NetworkInterfaceRegisterEvent;
|
|
use pocketmine\event\server\NetworkInterfaceUnregisterEvent;
|
|
use pocketmine\network\mcpe\protocol\PacketPool;
|
|
use pocketmine\Server;
|
|
|
|
class Network{
|
|
/** @var Server */
|
|
private $server;
|
|
|
|
/** @var NetworkInterface[] */
|
|
private $interfaces = [];
|
|
|
|
/** @var AdvancedNetworkInterface[] */
|
|
private $advancedInterfaces = [];
|
|
|
|
private $upload = 0;
|
|
private $download = 0;
|
|
|
|
/** @var string */
|
|
private $name;
|
|
|
|
public function __construct(Server $server){
|
|
PacketPool::init();
|
|
|
|
$this->server = $server;
|
|
|
|
}
|
|
|
|
public function addStatistics(float $upload, float $download) : void{
|
|
$this->upload += $upload;
|
|
$this->download += $download;
|
|
}
|
|
|
|
public function getUpload() : float{
|
|
return $this->upload;
|
|
}
|
|
|
|
public function getDownload() : float{
|
|
return $this->download;
|
|
}
|
|
|
|
public function resetStatistics() : void{
|
|
$this->upload = 0;
|
|
$this->download = 0;
|
|
}
|
|
|
|
/**
|
|
* @return NetworkInterface[]
|
|
*/
|
|
public function getInterfaces() : array{
|
|
return $this->interfaces;
|
|
}
|
|
|
|
public function processInterfaces() : void{
|
|
foreach($this->interfaces as $interface){
|
|
$this->processInterface($interface);
|
|
}
|
|
}
|
|
|
|
public function processInterface(NetworkInterface $interface) : void{
|
|
try{
|
|
$interface->process();
|
|
}catch(\Throwable $e){
|
|
$logger = $this->server->getLogger();
|
|
if(\pocketmine\DEBUG > 1){
|
|
$logger->logException($e);
|
|
}
|
|
|
|
$this->server->getPluginManager()->callEvent(new NetworkInterfaceCrashEvent($interface, $e));
|
|
|
|
$interface->emergencyShutdown();
|
|
$this->unregisterInterface($interface);
|
|
$logger->critical($this->server->getLanguage()->translateString("pocketmine.server.networkError", [get_class($interface), $e->getMessage()]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param NetworkInterface $interface
|
|
*/
|
|
public function registerInterface(NetworkInterface $interface) : void{
|
|
$this->server->getPluginManager()->callEvent($ev = new NetworkInterfaceRegisterEvent($interface));
|
|
if(!$ev->isCancelled()){
|
|
$interface->start();
|
|
$this->interfaces[$hash = spl_object_hash($interface)] = $interface;
|
|
if($interface instanceof AdvancedNetworkInterface){
|
|
$this->advancedInterfaces[$hash] = $interface;
|
|
$interface->setNetwork($this);
|
|
}
|
|
$interface->setName($this->name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param NetworkInterface $interface
|
|
*/
|
|
public function unregisterInterface(NetworkInterface $interface) : void{
|
|
$this->server->getPluginManager()->callEvent(new NetworkInterfaceUnregisterEvent($interface));
|
|
unset($this->interfaces[$hash = spl_object_hash($interface)], $this->advancedInterfaces[$hash]);
|
|
}
|
|
|
|
/**
|
|
* Sets the server name shown on each interface Query
|
|
*
|
|
* @param string $name
|
|
*/
|
|
public function setName(string $name) : void{
|
|
$this->name = $name;
|
|
foreach($this->interfaces as $interface){
|
|
$interface->setName($this->name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getName() : string{
|
|
return $this->name;
|
|
}
|
|
|
|
public function updateName() : void{
|
|
foreach($this->interfaces as $interface){
|
|
$interface->setName($this->name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return Server
|
|
*/
|
|
public function getServer() : Server{
|
|
return $this->server;
|
|
}
|
|
|
|
/**
|
|
* @param string $address
|
|
* @param int $port
|
|
* @param string $payload
|
|
*/
|
|
public function sendPacket(string $address, int $port, string $payload) : void{
|
|
foreach($this->advancedInterfaces as $interface){
|
|
$interface->sendRawPacket($address, $port, $payload);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Blocks an IP address from the main interface. Setting timeout to -1 will block it forever
|
|
*
|
|
* @param string $address
|
|
* @param int $timeout
|
|
*/
|
|
public function blockAddress(string $address, int $timeout = 300) : void{
|
|
foreach($this->advancedInterfaces as $interface){
|
|
$interface->blockAddress($address, $timeout);
|
|
}
|
|
}
|
|
|
|
public function unblockAddress(string $address) : void{
|
|
foreach($this->advancedInterfaces as $interface){
|
|
$interface->unblockAddress($address);
|
|
}
|
|
}
|
|
}
|