Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion classes/MinecraftAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MinecraftAPI

// Ping Variables
private $socket;
private $timeout = 10;

// Minecraft server stats
public $mc_status = false;
Expand Down Expand Up @@ -44,12 +45,14 @@ class MinecraftAPI
* @author Alexis Tan
* @version 1.0
*/
public function __construct (Cache $cache, $server = 'localhost:25565', $cacheName = 'cache')
public function __construct (Cache $cache, $server = 'localhost:25565', $cacheName = 'cache', $timeout = 10)
{
$this->cache = $cache;

$this->server = $server;
$this->cacheName = $cacheName;

$this->timeout = $timeout;

if ( strlen( $this->server ) == 0 ) {
throw new Exception( 'Invalid server address.' );
Expand All @@ -63,6 +66,14 @@ public function __construct (Cache $cache, $server = 'localhost:25565', $cacheNa

$this->cache->setConfig( 'path', $this->cache->getConfig( 'path' ) . $this->getServer( ) . '/' );
}

/*
* Get the timeout
*/
public function getTimeout( )
{
return $this->timeout;
}

/*
* Get the cache object
Expand Down Expand Up @@ -99,6 +110,7 @@ public function ping( )

// Create socket and connect
$socket = socket_create( AF_INET, SOCK_STREAM, getprotobyname( 'tcp' ) );
socket_set_option( $socket, SOL_SOCKET, SO_RCVTIMEO, array( 'sec' => $this->timeout, 'usec' => $this->timeout * 1000000 ) );
socket_connect( $socket, $this->server, $this->port );

// Stop the latencty listener
Expand Down