From 00c84ff291ce4c4fd7192037084ea8fac4ed7948 Mon Sep 17 00:00:00 2001 From: Guichaguri Date: Tue, 8 Jul 2014 02:18:14 -0300 Subject: [PATCH] Added configurable timeout --- classes/MinecraftAPI.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/classes/MinecraftAPI.php b/classes/MinecraftAPI.php index ade4f3f..2dca3a1 100644 --- a/classes/MinecraftAPI.php +++ b/classes/MinecraftAPI.php @@ -16,6 +16,7 @@ class MinecraftAPI // Ping Variables private $socket; + private $timeout = 10; // Minecraft server stats public $mc_status = false; @@ -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.' ); @@ -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 @@ -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