Advertisement
Guest User

Untitled

a guest
Sep 14th, 2010
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. <?php
  2.  
  3. // Class by Boylett
  4. // Released under GNU/GPL.
  5.  
  6. // Designed for IVMP 0.1 Beta T4
  7.  
  8. class IVMPQuery
  9. {
  10.     var $socket = false;
  11.     var $ip = false;
  12.     var $port = false;
  13.  
  14.     function Query($ip,$port,&$errno,&$errstr,$timeout = 5,$gettimeout = 1)
  15.     {
  16.         $this->Close();
  17.         $this->ip = $ip;
  18.         $this->port = $port;
  19.         $this->socket = @fsockopen('udp://'.$ip,$port,$errno,$errstr,$timeout);
  20.         if($this->socket === false) return false;
  21.         @stream_set_timeout($this->socket,$gettimeout);
  22.         return true;
  23.     }
  24.  
  25.     function Close()
  26.     {
  27.         if($this->socket !== false)
  28.         {
  29.             fclose($this->socket);
  30.             $this->socket = false;
  31.         }
  32.     }
  33.  
  34.     function GetPacketData($command)
  35.     {
  36.         $packet = 'IVMP';
  37.         $packet .= $command;
  38.         return $packet;
  39.     }
  40.  
  41.     function ServerData()
  42.     {
  43.         fputs($this->socket,$this->GetPacketData('i'));
  44.         @fread($this->socket,5); // IVMPi
  45.        
  46.         $len = ord(@fread($this->socket,4));       
  47.         $hostname = @fread($this->socket,$len); // read hostname
  48.         $players = ord(@fread($this->socket,4)); // read players
  49.         $maxplayers = ord(@fread($this->socket,4)); // read max players
  50.         $passworded = ord(@fread($this->socket,1)); // 1 byte for password
  51.  
  52.         return array(
  53.             'hostname' => $hostname,
  54.             'players' => $players,
  55.             'maxplayers' => $maxplayers,
  56.             'passworded' => (bool)$passworded
  57.         );
  58.     }
  59.  
  60.     function ServerRules()
  61.     {
  62.         fputs($this->socket,$this->GetPacketData('r'));
  63.         @fread($this->socket,5); // IVMPr
  64.        
  65.         $count = ord(@fread($this->socket,4));
  66.         $arr = array();
  67.         for($i = 0; $i < $count; $i++)
  68.         {
  69.             $len = ord(@fread($this->socket,4));
  70.             $key = @fread($this->socket,$len);
  71.             $len = ord(@fread($this->socket,4));
  72.             $arr[$key] = @fread($this->socket,$len);
  73.         }
  74.        
  75.         return $arr;
  76.     }
  77.  
  78.     function Players()
  79.     {
  80.         fputs($this->socket,$this->GetPacketData('l'));
  81.         @fread($this->socket,5); // IVMPl
  82.        
  83.         $count = ord(@fread($this->socket,4));
  84.         $arr = array();
  85.         for($i = 0; $i < $count; $i++)
  86.         {
  87.             $id = ord(@fread($this->socket,4));
  88.             $len = ord(@fread($this->socket,4));
  89.             $arr[$id] = @fread($this->socket,$len);
  90.         }
  91.        
  92.         return $arr;
  93.     }
  94.  
  95.     // Default return is ms int. If $asfloat == true, will return as seconds and as a float
  96.     function Ping($asfloat = false)
  97.     {
  98.         $time = microtime(true);
  99.         fputs($this->socket,$this->GetPacketData('p'));
  100.         @fread($this->socket,5); // IVMPp      
  101.         $reply = @fread($this->socket,4);
  102.         $time = microtime(true) - $time;
  103.         if(!$asfloat)
  104.         {
  105.             $time *= 1000;
  106.             $time = (int)round($time);
  107.         }
  108.         if($reply == 'PONG')
  109.         {
  110.             return $time;
  111.         }
  112.        
  113.         return false;
  114.     }
  115. }
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement