Created
March 26, 2012 11:25
-
-
Save vimishor/2204505 to your computer and use it in GitHub Desktop.
Check hlds status and calculate latency.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* hldsStatus class file. | |
* | |
* Check hlds status and calculate latency. | |
* | |
* @author Alexandru G. | |
* @link http://www.gentle.ro/ | |
* @version 0.1.1 | |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Public License | |
*/ | |
class hldsStatus | |
{ | |
/** | |
* Server IP | |
* @var string | |
*/ | |
protected $ip; | |
/** | |
* Server port | |
* @var int | |
*/ | |
protected $port; | |
/** | |
* Server latency | |
* @var int | |
*/ | |
protected $latency; | |
/** | |
* Socket holder | |
* @var ?? | |
*/ | |
protected $sock; | |
/** | |
* Check if specified server is online | |
* | |
* @access public | |
* @param string $address Server address | |
* @param int $timeout Socket timeout (optional) | |
* @return string | |
*/ | |
public function status($address, $timeout = 6) | |
{ | |
$this->_parseAddr($address); | |
if ($this->sock = @fsockopen('udp://'.$this->ip, $this->port, $errno, $errstr, $timeout)) | |
{ | |
$startTime = microtime(true); | |
stream_set_timeout($this->sock, $timeout); | |
fwrite($this->sock, "\xFF\xFF\xFF\xFFTSource Engine Query".chr(0)); | |
$status = (!fread($this->sock, 5)) ? 'Offline' : 'Online'; | |
fclose($this->sock); | |
$stopTime = microtime(true); | |
$this->latency = number_format( ($stopTime - $startTime) * 1000, 3 ); | |
} | |
else | |
{ | |
$status = 'Offline'; | |
} | |
return $status; | |
} | |
/** | |
* Get the server latency | |
* | |
* @access public | |
* @param string $address Server address (optional) | |
* @return int -1 on error | |
*/ | |
public function ping($address = null) | |
{ | |
if (!is_numeric($this->port)) | |
{ | |
if (is_null($address)) | |
{ | |
throw new Exception('Specify the server address'); | |
} | |
$this->status($address); | |
} | |
return (is_numeric($this->latency)) ? $this->latency : '-1'; | |
} | |
/** | |
* Extract IP and Port from given address | |
* | |
* @access private | |
* @param string $address | |
* @return void | |
*/ | |
private function _parseAddr($address) | |
{ | |
if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) | |
{ | |
$this->ip = $address; | |
$this->port = 27015; | |
} | |
else | |
{ | |
$pcs = explode(':', $address); | |
$this->ip = gethostbyname($pcs[0]); | |
$this->port = (isset($pcs[1])) ? $pcs[1] : 27015; | |
} | |
} | |
/** | |
* Clean before exit | |
*/ | |
public function __destruct() | |
{ | |
if (!is_null($this->sock)) | |
{ | |
$this->sock = $this->ip = $this->port = null; | |
} | |
} | |
} | |
/** | |
* Example | |
*/ | |
$server = new hldsStatus(); | |
echo $server->status('216.52.148.71:27015').' - '.$server->ping().'<br>'; | |
echo $server->status('212.200.163.180:27027').' - '.$server->ping().'<br>'; | |
echo $server->status('cs.extream.ro').' - '.$server->ping().'<br>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment