Skip to content

Commit

Permalink
Added new events for network interface handling, close #1232 (#1250)
Browse files Browse the repository at this point in the history
Added 
- NetworkInterfaceRegisterEvent (cancellable)
- NetworkInterfaceUnregisterEvent
- NetworkInterfaceCrashEvent
  • Loading branch information
dktapps authored Sep 6, 2017
1 parent 2215543 commit 2c6205e
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/pocketmine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1613,14 +1613,16 @@ public function __construct(\ClassLoader $autoloader, \ThreadedLogger $logger, s
register_shutdown_function([$this, "crashDump"]);

$this->queryRegenerateTask = new QueryRegenerateEvent($this, 5);
$this->network->registerInterface(new RakLibInterface($this));

$this->pluginManager->loadPlugins($this->pluginPath);

$this->updater = new AutoUpdater($this, $this->getProperty("auto-updater.host", "update.pmmp.io"));

$this->enablePlugins(PluginLoadOrder::STARTUP);

$this->network->registerInterface(new RakLibInterface($this));


LevelProviderManager::addProvider(Anvil::class);
LevelProviderManager::addProvider(McRegion::class);
LevelProviderManager::addProvider(PMAnvil::class);
Expand Down
50 changes: 50 additions & 0 deletions src/pocketmine/event/server/NetworkInterfaceCrashEvent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
45 changes: 45 additions & 0 deletions src/pocketmine/event/server/NetworkInterfaceEvent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
35 changes: 35 additions & 0 deletions src/pocketmine/event/server/NetworkInterfaceRegisterEvent.php
Original file line number Diff line number Diff line change
@@ -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;

}
32 changes: 32 additions & 0 deletions src/pocketmine/event/server/NetworkInterfaceUnregisterEvent.php
Original file line number Diff line number Diff line change
@@ -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;

}
23 changes: 16 additions & 7 deletions src/pocketmine/network/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
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;

Expand Down Expand Up @@ -90,6 +93,8 @@ public function processInterfaces(){
$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()]));
Expand All @@ -101,20 +106,24 @@ public function processInterfaces(){
* @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]);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/pocketmine/network/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
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
*
Expand Down
6 changes: 5 additions & 1 deletion src/pocketmine/network/mcpe/RakLibInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ public function __construct(Server $server){
$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;
}
Expand Down

0 comments on commit 2c6205e

Please sign in to comment.