Net_GameServerQuery
[ class tree: Net_GameServerQuery ] [ index: Net_GameServerQuery ] [ all elements ]

Source for file GameServerQuery.php

Documentation is available at GameServerQuery.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available through the world-wide-web at the following url:           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Aidan Lister <aidan@php.net>                                |
  16. // |          Tom Buskens <ortega@php.net>                                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: GameServerQuery.php,v 1.45 2004/11/15 18:52:01 ortega Exp $
  20.  
  21.  
  22. require_once 'Net\GameServerQuery\Config.php';
  23. require_once 'Net\GameServerQuery\Communicate.php';
  24. require_once 'Net\GameServerQuery\Process.php';
  25.  
  26.  
  27. /**
  28.  * Query and retrieve information from game servers
  29.  *
  30.  * @category        Net
  31.  * @package         Net_GameServerQuery
  32.  * @author          Aidan Lister <aidan@php.net>
  33.  * @author          Tom Buskens <ortega@php.net>
  34.  * @version         $Revision: 1.45 $
  35.  */
  36. {
  37.     /**
  38.      * Hold the counter per server
  39.      *
  40.      * @var         int 
  41.      */
  42.     private $_servercount = -1;
  43.  
  44.     /**
  45.      * Hold the counter per socket
  46.      *
  47.      * @var         int 
  48.      */
  49.     private $_socketcount = -1;
  50.  
  51.     /**
  52.      * An instance of the Net_GameServerQuery_Config class
  53.      *
  54.      * @var         object 
  55.      */
  56.     private $_config;
  57.  
  58.     /**
  59.      * An instance of the Net_GameServerQuery_Communicate class
  60.      *
  61.      * @var         object 
  62.      */
  63.     private $_communicate;
  64.  
  65.     /**
  66.      * An instance of the Net_GameServerQuery_Process class
  67.      *
  68.      * @var         object 
  69.      */
  70.     private $_process;
  71.  
  72.     /**
  73.      * A master list of socket data
  74.      *
  75.      * Contains a mass of information about each socket opened
  76.      * serverid|flag|addr|port|packet|packetname|protocol|game
  77.      *
  78.      * @var         array 
  79.      */
  80.     private $_socketlist;
  81.  
  82.     /**
  83.      * Hold an array of runtime options
  84.      *
  85.      * @var         array 
  86.      */
  87.     private $_options;
  88.  
  89.  
  90.     /**
  91.      * Constructor
  92.      *
  93.      * Load the classes needed throughout the script
  94.      */
  95.     public function __construct()
  96.     {
  97.         // Set default option values
  98.         $this->_options = array('timeout' => 300);
  99.  
  100.         // Load classes
  101.         $this->_config      = new Net_GameServerQuery_Config;
  102.         $this->_communicate = new Net_GameServerQuery_Communicate;
  103.         $this->_process     = new Net_GameServerQuery_Process($this->_config);
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Set an option
  109.      *
  110.      * @param    string     $option       The option to set
  111.      * @param    string     $value        The value
  112.      */
  113.     public function setOption($option$value)
  114.     {
  115.         switch ($option):
  116.             case 'timeout':
  117.             case 'normalise':
  118.                 $this->_options[$option$value;
  119.                 break;
  120.             
  121.             default:
  122.                 throw new Exception ('Invalid option');
  123.                 break;
  124.  
  125.         endswitch;
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Get an option
  131.      *
  132.      * @param    string     $option       The option to get
  133.      */
  134.     public function getOption($option)
  135.     {
  136.         switch ($option):
  137.             case 'timeout':
  138.             case 'normalise':
  139.                 return $this->_options[$option];
  140.                 break;
  141.             
  142.             default:
  143.                 throw new Exception ('Invalid option');
  144.                 break;
  145.  
  146.         endswitch;
  147.     }
  148.  
  149.         
  150.     /**
  151.      * Add a server
  152.      *
  153.      * @param    string     $game         The type of game
  154.      * @param    string     $addr         The address to query
  155.      * @param    int        $port         The port to query
  156.      * @param    string     $status       A pipe delimited string of query types
  157.      * @return   int        The number used to identify the server just added
  158.      */
  159.     public function addServer($game$addr$port = null$query 'status')
  160.     {
  161.         // Validate game
  162.         if ($this->_config->validgame($game=== false{
  163.             throw new Exception ('Invalid Game');
  164.             return false;
  165.         }
  166.  
  167.         ++$this->_servercount;
  168.  
  169.         // Find default port
  170.         if (is_null($port)) {
  171.             $port $this->_config->getPort($game);
  172.         }
  173.  
  174.         // Find the protocol
  175.         $protocol $this->_config->getProtocol($game);
  176.  
  177.         // Get list of queries to be sent
  178.         $querylist $this->_getQueryFlags($query);
  179.  
  180.         // Create a list of socket data
  181.         $this->_buildSocketList($querylist$protocol$game$addr$port);
  182.  
  183.         // Return the counter for identifying the server later
  184.         return $this->_servercount;
  185.     }
  186.  
  187.  
  188.     /**
  189.      * Execute the query
  190.      *
  191.      * Communicate with the server, then send the information for
  192.      * processing. Then, reconstruct the array so the user can access
  193.      * the data.
  194.      *
  195.      * @param     int        $timeout        The timeout in milliseconds
  196.      * @return    array      An array of server information
  197.      */
  198.     public function execute($timeout = null)
  199.     {
  200.         // Check we have something to do
  201.         if ($this->_servercount === -1{
  202.             return false;
  203.         }
  204.  
  205.         // Set the timeout
  206.         if (!is_null($timeout)) {
  207.             $this->setOption('timeout'$timeout);
  208.         }
  209.  
  210.         // Timeout in millseconds
  211.         $timeout $this->getOption('timeout'* 1000;
  212.  
  213.         // Communicate with the servers
  214.         // We now have an array of unprocessed server data
  215.         $results $this->_communicate->query($this->_socketlist$timeout);
  216.  
  217.         // Finish the array for the process class
  218.         // Add the packets we just recieved into the array
  219.         foreach ($this->_socketlist as $key => $server{
  220.             // Check if we missed out on any packets
  221.             if (!isset($results[$key])) {
  222.                 throw new Exception ('Server did not reply to request');
  223.             }
  224.  
  225.             $this->_socketlist[$key]['response'$results[$key];
  226.         }
  227.  
  228.         // Process the results
  229.         $results $this->_process->process($this->_socketlist);
  230.  
  231.         // Put the data back together
  232.         foreach ($this->_socketlist as $key => $server{
  233.             $servid                     $server['serverid'];
  234.             $flag                       $server['flag'];
  235.             $newresults[$servid][$flag$results[$key];
  236.         }
  237.  
  238.         return $newresults;
  239.     }
  240.  
  241.  
  242.     /**
  243.      * Validate and process the query flags
  244.      *
  245.      * @param   string      $query        A pipe delimited list of query flags
  246.      */
  247.     private function _getQueryFlags($flags)
  248.     {
  249.         $flags explode('|'$flags);
  250.  
  251.         // Validate each flag
  252.         foreach ($flags as $flag{
  253.             if ($flag !== 'status' &&
  254.                 $flag !== 'players' &&
  255.                 $flag !== 'rules'{
  256.  
  257.                 throw new Exception ('Invalid Query Flag');
  258.             }
  259.         }
  260.  
  261.         return $flags;
  262.     }
  263.  
  264.     
  265.     /**
  266.      * Create an array containing all socket data
  267.      *
  268.      * @param   string      $flags        Query flags
  269.      * @param   string      $protocol     Protocol
  270.      * @param   string      $game         The game
  271.      * @param   string      $addr         The address
  272.      * @param   string      $port         The port
  273.      */
  274.     private function _buildSocketList($flags$protocol$game$addr$port)
  275.     {
  276.         // We loop through each of the query flags
  277.         // Each flag gets its own socket
  278.         foreach ($flags as $flag{
  279.  
  280.             ++$this->_socketcount;
  281.  
  282.             list($packetname$packet=
  283.                 $this->_config->getPacket($protocol$flag);
  284.  
  285.             // Master list
  286.             $this->_socketlist[$this->_socketcount= array(
  287.                 'serverid'   => $this->_servercount,
  288.                 'flag'       => $flag,
  289.                 'addr'       => $addr,
  290.                 'port'       => $port,
  291.                 'packet'     => $packet,
  292.                 'packetname' => $packetname,
  293.                 'protocol'   => $protocol,
  294.                 'game'       => $game
  295.                 );
  296.             
  297.         };
  298.     }
  299.  
  300. }
  301.  
  302. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.