Source for file GameServerQuery.php
Documentation is available at GameServerQuery.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://au.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Aidan Lister <aidan@virtualexplorer.com.au> |
// +----------------------------------------------------------------------+
require_once ('PEAR.php');
require_once ('function.microtime_str.php');
define('NET_GAMESERVERQUERY_ERROR_INVALIDPROTOCOL', 0 );
define('NET_GAMESERVERQUERY_ERROR_COULDNOTCONNECT', 1 );
define('NET_GAMESERVERQUERY_ERROR_NOREPLY', 2 );
define('NET_GAMESERVERQUERY_ERROR_COULDNOTSEND', 3 );
* Communicate with a Half-Life server.
* @package Net_GameServerQuery
* The resource ID of the connection
* True if we have a udp "connection" open.
* The IP we're connecting to
* The port we wish to query
* Max time in seconds to send data for
private $_timeout_stream;
* Max time in seconds to attempt connection
private $_timeout_connect;
* What packet protocol we are using
$this->_timeout_stream = 1;
$this->_timeout_connect = 1;
* @param string $protocol The protocol to use (name of the game)
* @param string $ip The address to connect to
* @param int $port The information port of the server
* @return mixed true if query was successful, or a PEAR_Error if not.
public function query ($ip, $port, $protocol)
// Load the protocols ($games, $protcols)
include_once ('GameServerQuery/protocols.php');
$this->_protocol = $protocols[$games[$protocol]['protocol']]; }
// Perform the actual query
// $data is now the raw response from the server or an error
$data = $this->_proceduralquery ();
// Check if there was an error
// If there was, return the error obj
// If there wasn't, return the result object
// We parse a formatted associative array to the result object
$info = $this->_format ($data);
* Connect, Query, Receive and Disconnect
* @return mixed true if query was successful, or a PEAR_Error if not.
private function _proceduralquery ()
if (!$this->_connect ($this->_ip, $this->_port)) {
$packet = $this->_build ();
$start = microtime_str ();
if (!$this->_send ($packet)) {
$data = $this->_receive ();
// When we recieve the reply, end the timer
$this->_ping = round((($stop - $start) * 1000 ));
* Select the packet to use
* @return string The packet
private function _build ()
$packet = $this->_protocol['packet'];
* Take the raw server response and extract
* the information into an assoc array
* @param string $data The string from which to parse the data
* @return array Associative array of server data
private function _format ($data)
// Some games return the information in the second chunk
// so we need to define the delimiters for the chunk segments
$delimiter = $this->_protocol['delim'];
// Get the second chunk if need be
if ($delimiter !== null ) {
list (, $data) = explode($delimiter, $data); }
// The data chunk is backslash delimited
// Remove the first backslash, then explode
// Sort the data into an assoc array
for ($i = 0 , $x = count($data_arr); $i < $x; $i += 2 ) {
$info[$data_arr[$i]] = $data_arr[$i+1 ]; }
* @param string $ip IP of the server
* @param string $port Port of the server
* @return bool True if the connection is established
private function _connect ($ip, $port)
$fp = fsockopen('udp://' . $ip, $port, $errno, $errstr, $this->_timeout_connect);
// Check if connection was successful
$this->_connected = true;
* @return bool True if the connection was closed
private function _disconnect ()
// Check if we're connected
if ($this->_connected !== true ) {
* @param string $packet The packet to send
* @return bool True if the packet was sent
private function _send ($packet)
// Check if we're connected
if ($this->_connected !== true ) {
// If we have multiple packets
foreach ($packet as $pack)
if (!fwrite($this->_socket, $pack)) {
// If we have a single packet
if (!fwrite($this->_socket, $packet)) {
private function _receive ()
// Read the first bit, this will tell us if we have a valid packet
$data = fread($this->_socket, 1 );
$bytesleft = $metadata['unread_bytes'];
// If we have a valid packet, read the rest of it
$data .= fread($this->_socket, $bytesleft);
$bytesleft = $metadata['unread_bytes'];
* Return microtime as a single string.
private function microtime_str ()
$microtime = (float) $msec + (float) $sec;
* Net_GameServerQuery_Result
* Result object for GameServerQuery.
* @package Net_GameServerQuery
* Hold the assoc. array from a query request
* @param string $data The raw array of packet data
* @param int $ping The ping in milliseconds
* @param array $prot The query protocol information
$this->_protocol = $prot;
* Basic "status" information
* @return array The associative array of server data
// Get a list of the raw packet status keys
$keys = $this->_protocol['keys'];
// Normalise the raw packet
// numplayers is not supported for all game protocols, yet
'maxplayers' => $this->_data[$keys[0 ]],
'numplayers' => @$this->_data[$keys[1 ]],
'mapname' => $this->_data[$keys[2 ]],
'hostname' => $this->_data[$keys[3 ]],
* @return array The associative array of server data
* Information about players on the server
* This function is not complete. It will return a dummy array of player information.
* @return array The associative array of server data
* This function is not complete. It will return false.
* @return array The associative array of server data
* The time it took the server to respond to a request
* @return float Ping in milliseconds
* Net_GameServerQuery_Error
* Error object for GameServerQuery.
* @package Net_GameServerQuery
* @return constant The error code
* @return constant The error code
print $errors[$this->_errno];
Documentation generated on Mon, 11 Mar 2019 10:14:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|