Raised This Month: $ Target: $400
 0% 

Help with a PHP timeout plz


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 04-20-2007 , 13:48   Re: Help with a PHP timeout plz
Reply With Quote #1

I just added the timeout into the fsockopen line and added a check for feof to stop that error of having nothing left to read and trying to read out a long.
PHP Code:
<?php
class panServerinfo
{
    var 
$ip;
    var 
$port;
   
    var 
$fp;
    var 
$timeout;
   
    var 
$serverData;
    var 
$playerData;


    function 
panServerinfo($ip$port$timeout 3.0)
    {
        
$this->ip       $ip;
        
$this->port     $port;
       
        
$this->fp       fsockopen("udp://" $ip$port $errnum $errstr , (float)$timeout);
        
$this->timeout  $timeout;
    }


    function 
getServerData()
    {
        
$this->writeData($this->getQuery("A2S_INFO"));
       
        
$this->getData("byte");
        
$this->getString();
        
$this->serverData['hostname']   = $this->getString();
        
$this->serverData['map']        = $this->getString();
        
$this->getString();
        
$this->serverData['mod']        = $this->getString();
        
$this->serverData['players']    = $this->getData("byte");
        
$this->serverData['maxplayers'] = $this->getData("byte");
        
$this->getData("byte");
        
$this->serverData['servertype'] = (chr($this->getData("byte")) == "d") ? "Dedicated" "Listen";
        
$this->serverData['server_os']  = (chr($this->getData("byte")) == "w") ? "Windows" "Linux";
        
$this->serverData['password']       = $this->getData("byte");
        
$this->getData("byte");
        
$this->getString();
        
$this->getString();
        
$this->getString();
        
$this->getData("long");
        
$this->getData("long");
        
$this->getData("byte");
        
$this->getData("byte");
        
$this->serverData['vac']        = $this->getData("byte");
        
$this->getData("byte");
    }
   
   
    function 
getPlayerData()
    {
        
$this->writeData($this->getQuery("A2S_PLAYER"));
       
        
fread($this->fp4);
        
$this->getData("byte");
       
        
$count $this->getData("byte");
       
        for(
$i 0$i $count$i++)
        {
            
$this->playerData[$i]['id']     = $this->getData("byte");
            
$this->playerData[$i]['name']   = $this->getString();
            
$this->playerData[$i]['frags']  = $this->getData("long");
            
$this->playerData[$i]['time']   = round($this->getData("float"));
        }
    }


    function 
getQuery($queryType)
    {
        switch(
$queryType)
        {
            case 
"A2S_SERVERQUERY_GETCHALLENGE":
                return 
"\xFF\xFF\xFF\xFF\x57";
                break;

            case 
"A2S_INFO":
                return 
"\xFF\xFF\xFF\xFFTSource Engine Query\x00";
                break;

            case 
"A2S_PLAYER":
                return 
sprintf("\xFF\xFF\xFF\xFFU%s"$this->getChallenge());
                break;
        }
    }


    function 
getChallenge()
    {
        
$this->writeData($this->getQuery("A2S_SERVERQUERY_GETCHALLENGE"));
       
        return 
substr(fread($this->fp16), 59);
    }


    function 
getData($type)
    {
           if(
feof($this -> fp))
           return 
false;
        switch(
$type)
        {
            case 
"long":
                
$data unpack("L"fread($this->fp4));
                return 
$data[1];
                break;

            case 
"byte":
                return 
ord(fread($this->fp1));
                break;

            case 
"char":
                return 
fread($this->fp1);
                break;

            case 
"float":
                
$data unpack("f"fread($this->fp4));
                return 
$data[1];
                break;
        }
    }


    function 
getString()
    {
        
$string '';
        
$loop   TRUE;
       
        while(
$loop)
        {
            
$_fp $this->getData("char");

            if( 
ord($_fp) != )
            {
                
$string .= $_fp;
            }
            else { 
$loop FALSE; }
        }
       
        return 
$string;
    }


    function 
writeData($input)
    {
        if( !
$this->fp )
        {
            exit(
"Error: Couldn't connect to server.");
        }
        else {
            
fwrite($this->fp$input);
            
socket_set_timeout($this->fp$this->timeout);
        }
    }


    function 
setTimeFormat($format$input)
    {
        
$hours floor($input 3600);
        
$input $input 3600;

        
$minutes floor($input 60);
        
$input $input 60;

        
$seconds round($input);

        return 
sprintf($format$hours$minutes$seconds);
    }


    function 
sortPlayers($sort "time"$type "desc")
    {
        if(isset(
$this->playerData[0][$sort]))
        {
            for(
$i 0$i count($this->playerData); $i++)
            {
                
$temp[] = $this->playerData[$i][$sort];
            }

            switch(
$sort)
            {
                case 
"name":
                    
uasort($temp"strcasecmp");
                    break;
               
                default:
                    if(
$type == "desc") {
                        
arsort($temp);
                    }
                    elseif(
$type == "asc") {
                        
asort($temp);
                    }
                    break;
            }

            foreach(
$temp as $key => $value)
            {
                
$keys[] = $key;
            }

            foreach(
$keys AS $k => $v)
            {
                
$tempvar[$k]['id']     = $this->playerData[$v]['id'];
                
$tempvar[$k]['name']   = $this->playerData[$v]['name'];
                
$tempvar[$k]['frags']  = $this->playerData[$v]['frags'];
                
$tempvar[$k]['time']   = $this->playerData[$v]['time'];
            }

            
$this->playerData $tempvar;
        }
    }
}
?>

<?php
$ip_port 
= array(
                
=> "xx.xxx.xx.xxx:27015",
                     
"xxx.xx.xxx.xx:27015",
                     
"xxx.xxx.xxx.xxx:27015"
                
);

$rand       rand(1count($ip_port));
$getip_port explode(":"$ip_port[$rand]);

$ip     = (empty($_POST["ip"]))   ? $getip_port[0] : $_POST["ip"];
$port   = (empty($_POST["port"])) ? $getip_port[1] : $_POST["port"];

$s = &new panServerinfo($ip$port);
$s->getServerData();
$s->getPlayerData();
?>

<!-- Do an if statement if there was a timeout and if there was show it was offline -->
<?php echo $ip?><br>
Players: <?php echo $s->serverData['players'] . "/" $s->serverData['maxplayers']; ?><br>
Map: <?php echo $s->serverData['map']; ?><br>
Passworded: <?php echo ($s->serverData['password'] == 1) ? "Yes" "No"?>
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:18.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode