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

@ -1613,7 +1613,6 @@ class Server{
register_shutdown_function([$this, "crashDump"]);
$this->queryRegenerateTask = new QueryRegenerateEvent($this, 5);
$this->network->registerInterface(new RakLibInterface($this));
$this->pluginManager->loadPlugins($this->pluginPath);
@ -1621,6 +1620,9 @@ class Server{
$this->enablePlugins(PluginLoadOrder::STARTUP);
$this->network->registerInterface(new RakLibInterface($this));
LevelProviderManager::addProvider(Anvil::class);
LevelProviderManager::addProvider(McRegion::class);
LevelProviderManager::addProvider(PMAnvil::class);

View File

@ -0,0 +1,50 @@
<?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);
namespace pocketmine\event\server;
use pocketmine\network\SourceInterface;
/**
* Called when a network interface crashes, with relevant crash information.
*/
class NetworkInterfaceCrashEvent extends NetworkInterfaceEvent{
public static $handlerList = null;
/**
* @var \Throwable
*/
private $exception;
public function __construct(SourceInterface $interface, \Throwable $throwable){
parent::__construct($interface);
$this->exception = $throwable;
}
/**
* @return \Throwable
*/
public function getCrashInformation() : \Throwable{
return $this->exception;
}
}

View File

@ -0,0 +1,45 @@
<?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);
namespace pocketmine\event\server;
use pocketmine\network\SourceInterface;
class NetworkInterfaceEvent extends ServerEvent{
/** @var SourceInterface */
protected $interface;
/**
* @param SourceInterface $interface
*/
public function __construct(SourceInterface $interface){
$this->interface = $interface;
}
/**
* @return SourceInterface
*/
public function getInterface() : SourceInterface{
return $this->interface;
}
}

View File

@ -0,0 +1,35 @@
<?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);
namespace pocketmine\event\server;
use pocketmine\event\Cancellable;
use pocketmine\network\SourceInterface;
/**
* Called when a network interface is registered into the network, for example the RakLib interface.
*/
class NetworkInterfaceRegisterEvent extends NetworkInterfaceEvent implements Cancellable{
public static $handlerList = null;
}

View File

@ -0,0 +1,32 @@
<?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);
namespace pocketmine\event\server;
/**
* Called when a network interface is unregistered
*/
class NetworkInterfaceUnregisterEvent extends NetworkInterfaceEvent{
public static $handlerList = null;
}

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;
}