Added new events for network interface handling, close #1232 (#1250)

Added 
- NetworkInterfaceRegisterEvent (cancellable)
- NetworkInterfaceUnregisterEvent
- NetworkInterfaceCrashEvent
This commit is contained in:
Dylan K. Taylor
2017-09-06 11:22:10 +01:00
committed by GitHub
parent 2215543e39
commit 2c6205e3f3
8 changed files with 191 additions and 9 deletions

View File

@ -26,6 +26,9 @@ declare(strict_types=1);
*/
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;
@ -90,6 +93,8 @@ class Network{
$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()]));
@ -101,20 +106,24 @@ class Network{
* @param SourceInterface $interface
*/
public function registerInterface(SourceInterface $interface){
$this->interfaces[$hash = spl_object_hash($interface)] = $interface;
if($interface instanceof AdvancedSourceInterface){
$this->advancedInterfaces[$hash] = $interface;
$interface->setNetwork($this);
$this->server->getPluginManager()->callEvent($ev = new NetworkInterfaceRegisterEvent($interface));
if(!$ev->isCancelled()){
$interface->start();
$this->interfaces[$hash = spl_object_hash($interface)] = $interface;
if($interface instanceof AdvancedSourceInterface){
$this->advancedInterfaces[$hash] = $interface;
$interface->setNetwork($this);
}
$interface->setName($this->name);
}
$interface->setName($this->name);
}
/**
* @param SourceInterface $interface
*/
public function unregisterInterface(SourceInterface $interface){
unset($this->interfaces[$hash = spl_object_hash($interface)],
$this->advancedInterfaces[$hash]);
$this->server->getPluginManager()->callEvent(new NetworkInterfaceUnregisterEvent($interface));
unset($this->interfaces[$hash = spl_object_hash($interface)], $this->advancedInterfaces[$hash]);
}
/**

View File

@ -34,6 +34,11 @@ use pocketmine\Player;
*/
interface SourceInterface{
/**
* Performs actions needed to start the interface after it is registered.
*/
public function start();
/**
* Sends a DataPacket to the interface, returns an unique identifier for the packet if $needACK is true
*

View File

@ -67,10 +67,14 @@ class RakLibInterface implements ServerInstance, AdvancedSourceInterface{
$this->server = $server;
$this->identifiers = [];
$this->rakLib = new RakLibServer($this->server->getLogger(), $this->server->getLoader(), $this->server->getPort(), $this->server->getIp() === "" ? "0.0.0.0" : $this->server->getIp());
$this->rakLib = new RakLibServer($this->server->getLogger(), $this->server->getLoader(), $this->server->getPort(), $this->server->getIp() === "" ? "0.0.0.0" : $this->server->getIp(), false);
$this->interface = new ServerHandler($this->rakLib, $this);
}
public function start(){
$this->rakLib->start();
}
public function setNetwork(Network $network){
$this->network = $network;
}