more exception handling cleanup

This commit is contained in:
Dylan K. Taylor
2019-01-22 13:37:46 +00:00
parent 053defb7dc
commit 48a99937b9
3 changed files with 33 additions and 13 deletions

View File

@ -64,11 +64,20 @@ class RCON{
/** @var resource */
private $ipcThreadSocket;
/**
* @param Server $server
* @param string $password
* @param int $port
* @param string $interface
* @param int $maxClients
*
* @throws \RuntimeException
*/
public function __construct(Server $server, string $password, int $port = 19132, string $interface = "0.0.0.0", int $maxClients = 50){
$this->server = $server;
$this->server->getLogger()->info("Starting remote control listener");
if($password === ""){
throw new \InvalidArgumentException("Empty password");
throw new \RuntimeException("Empty password");
}
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

View File

@ -35,6 +35,11 @@ use function trim;
abstract class UPnP{
/**
* @param int $port
*
* @throws \RuntimeException
*/
public static function PortForward(int $port) : void{
if(!Internet::$online){
throw new \RuntimeException("Server is offline");
@ -56,8 +61,12 @@ abstract class UPnP{
throw new \RuntimeException("Failed to portforward using UPnP. Ensure that network discovery is enabled in Control Panel.");
}
/** @noinspection PhpUndefinedFieldInspection */
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
try{
/** @noinspection PhpUndefinedFieldInspection */
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
}catch(\com_exception $e){
throw new \RuntimeException($e->getMessage(), 0, $e);
}
}
public static function RemovePortForward(int $port) : bool{
@ -68,16 +77,18 @@ abstract class UPnP{
return false;
}
/** @noinspection PhpUndefinedClassInspection */
$com = new \COM("HNetCfg.NATUPnP");
/** @noinspection PhpUndefinedFieldInspection */
if($com === false or !is_object($com->StaticPortMappingCollection)){
return false;
}
try{
/** @noinspection PhpUndefinedClassInspection */
$com = new \COM("HNetCfg.NATUPnP");
/** @noinspection PhpUndefinedFieldInspection */
if($com === false or !is_object($com->StaticPortMappingCollection)){
return false;
}
/** @noinspection PhpUndefinedFieldInspection */
$com->StaticPortMappingCollection->Remove($port, "UDP");
}catch(\Throwable $e){
}catch(\com_exception $e){
//TODO: should this really be silenced?
return false;
}