RCON: properly handle potential errors during socket setup

This commit is contained in:
Dylan K. Taylor 2020-04-16 01:29:28 +01:00
parent 63b109f23e
commit 619a9892e5
2 changed files with 10 additions and 3 deletions

View File

@ -25,6 +25,7 @@ namespace pocketmine\level\format\io\region;
use pocketmine\level\format\ChunkException;
use pocketmine\level\format\io\exception\CorruptedChunkException;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Binary;
use pocketmine\utils\MainLogger;
use function ceil;
@ -98,7 +99,9 @@ class RegionLoader{
throw new CorruptedRegionException("Region file should be padded to a multiple of 4KiB");
}
$this->filePointer = fopen($this->filePath, "r+b");
$filePointer = fopen($this->filePath, "r+b");
if($filePointer === false) throw new AssumptionFailedError("fopen() should not fail here");
$this->filePointer = $filePointer;
stream_set_read_buffer($this->filePointer, 1024 * 16); //16KB
stream_set_write_buffer($this->filePointer, 1024 * 16); //16KB
if(!$exists){

View File

@ -75,13 +75,17 @@ class RCON{
throw new \InvalidArgumentException("Empty password");
}
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($socket === false){
throw new \RuntimeException("Failed to create socket:" . trim(socket_strerror(socket_last_error())));
}
$this->socket = $socket;
if(!socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)){
throw new \RuntimeException("Unable to set option on socket: " . trim(socket_strerror(socket_last_error())));
}
if($this->socket === false or !@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){
if(!@socket_bind($this->socket, $interface, $port) or !@socket_listen($this->socket, 5)){
throw new \RuntimeException(trim(socket_strerror(socket_last_error())));
}