(wL) Forums

Full Version: Misc Libraries By Leaky
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
TCP socket library (could be for UDP too but it's not really designed for that):

socket.class.php:

PHP Code:
<?PHP

class Socket {
    public 
$socket NULL;
    public 
$type NULL;
    public 
$family NULL;
    public 
$protocol NULL;
    public 
$connected false;
    
    function 
Socket($f AF_INET$t SOCK_STREAM$p SOL_TCP) {
        
$this->family $f;
        
$this->type $t;
        
$this->protocol $p;
        
$this->socket socket_create($f$t$p);
    }
    
    function 
connect($host$port) {
        if(!
socket_connect($this->socket$host$port)) {
            
trigger_error("Error: " socket_strerror(socket_last_error($this->socket)));
            
socket_clear_error($this->socket);
            
$this->connected false;
            return 
false;
        }
        else {
            
$this->connected true;
            return 
true;
        }
    }
    
    function 
disconnect() {
        if(!
socket_close($this->socket)) {
            
trigger_error("Error: Could not disconnect socket");
        }
        
$this->connected false;
        
$this->socket socket_create($this->family$this->type$this->protocol);
    }
    
    function 
sendData($data) {
        if(!@
socket_write($this->socket$datastrlen($data))) {
            
$this->connected false;
            return 
false;
        }
        return 
true;
    }
    
    function 
recvData($len 4096) {
        
//$select = array($this->socket);
        //socket_select($select, $write = NULL, $exept = NULL, 0);
        
$read '';
        
$readlen = @socket_recv($this->socket$read$lenMSG_WAITALL);
        if(
$readlen === false || $readlen === '' || $readlen $len) {
            
$this->connected false;
            return 
false;
        }
        else {
            return 
$read;
        }
    }
    
    function 
hasData() {
        
$readable NULL;
        
$read = array($this->socket);
        
$readable socket_select($read$write NULL$exception NULL0);
        return (
$readable != 0);
    }
}

?>


stream reader library modeled off of java's stream reader library:

packet.class.php:
PHP Code:
<?php

/* class packet module */


error_reporting(E_ALL);

class 
Packet {

    private 
$sockstream;
    public 
$RawData "";
    
    public static function 
debugOutput($buffer) {
        
$i 0;
        
$j 0;
        
$returnString "";
        for(
$i 0$i strlen($buffer); $i++) {
            if((
$i != 0) && ($i 16 == 0)) {
                
$returnString $returnString "\t";
                for(
$j $i 16$j $i$j++) {
                    if(
ord($buffer[$j]) < 0x20 || ord($buffer[$j]) > 0x7F)
                        
$returnString $returnString '.';
                    else
                        
$returnString $returnString $buffer[$j];
                }
                
// Add a linefeed after the string
                
$returnString $returnString "\n";
            }
            
$returnString $returnString bin2hex($buffer[$i]) . " ";
        }
        if(
$i != && $i 16 != 0) {
            for(
$j 0$j < ((16 - ($i 16)) * 3); $j++) {
                
$returnString $returnString " ";
            }
        }
        
$returnString $returnString "\t";
        if(
$i && ($i 16) == 0) {
            
$j $i 16;
        }
        else {
            
$j = ($i - ($i 16));
        }
        for(; 
$i >= && $j $i$j++) {
            if(
ord($buffer[$j]) < 0x20 || ord($buffer[$j]) > 0x7F) {
                
$returnString $returnString ".";
            }
            else {
                
$returnString $returnString $buffer[$j];
            }
        }
        
$returnString $returnString "\n";
        
$returnString $returnString "Length: " strlen($buffer) . "\n";
        return 
$returnString;
    }
    
    public function 
Packet(Socket &$stream) {
        
$this->sockstream $stream;
    }
    
    public function 
Skip($bytes) {
        
$this->sockstream->recvData($bytes);
    }
    
    public function 
Send() {
        
$this->sockstream->sendData($this->RawData);
        
$this->RawData '';
    }
    
    public function 
BYTE($data) {
        
//return (int)substr($data, 0, 1);
        
return pack("C",$data);
    }
    
    public function 
BYTE2($data) {
        
$tmp unpack("c"$data);
        return 
$tmp[1];
    }
    
    public function 
WORD($data) {
        return 
pack("n"$data);
    }
    
    function 
WORD2($data) {
        
$tmp unpack("n"$data);
        if(
$tmp[1] >= pow(215)) {
            
$tmp[1] -= pow(216);
        }
        return 
$tmp[1];
    }
    
    public function 
DWORD($data) {
        return 
pack("N"$data);
    }
    
    public function 
DWORD2($data) {
        
$tmp unpack("N"$data);
        if(
$tmp[1] >= pow(231)) {
            
$tmp[1] -= pow(232);
        }
        return 
$tmp[1];
    }
    
    public function 
DOUBLE($data) {
        return 
pack("d"$data);
    }
    
    public function 
DOUBLE2($data) {
        
$tmp unpack("d"$data);
        return 
$tmp[1];
    }
    
    public function 
FLOAT($data) {
        return 
pack("f"$data);
    }
    
    public function 
FLOAT2($data) {
        
$tmp unpack("f"$data);
        return 
$tmp[1];
    }
    
    public function 
ReadUTF() {
        
$len $strTemp NULL;
        try {
            
$len $this->ReadShort();
        }
        catch(
Exception $e) {
            throw 
$e;
        }
        if(
$len 0) {
            
$strTemp $this->sockstream->recvData($len);
            if(
$strTemp === false) {
                throw new 
Exception("Socket disconnected");
            }
        }
        else {
            
$strTemp '';
        }
        return 
$strTemp;
    }
    
    public function 
ReadByte() {
        
$tmp $this->sockstream->recvData(1);
        return 
$this->BYTE2($tmp);
    }
    
    public function 
ReadShort() {
        
$tmp $this->sockstream->recvData(2);
        if(
$tmp === false || $tmp === '') {
            throw new 
Exception("Socket disconnected");
        }
        return 
$this->WORD2($tmp);
    }
    
    public function 
ReadInt() {
        
$tmp $this->sockstream->recvData(4);
        if(
$tmp === false || $tmp === '') {
            throw new 
Exception("Socket disconnected");
        }
        return 
$this->DWORD2($tmp);
    }
    
    public function 
ReadDouble() {
        
$tmp $this->sockstream->recvData(8);
        if(
$tmp === false || $tmp === '') {
            throw new 
Exception("Socket disconnected");
        }
        return 
$this->DOUBLE2($tmp);
    }
    
    public function 
ReadFloat() {
        
$tmp $this->sockstream->recvData(4);
        if(
$tmp === false || $tmp === '') {
            throw new 
Exception("Socket disconnected");
        }
        return 
$this->FLOAT2($tmp);
    }
    
    public function 
WriteUTF($Data) {
        
$this->WriteShort(strlen($Data));
        
$this->RawData $this->RawData $Data;
    }
    
    public function 
WriteByte($Data) {
        
$this->RawData $this->RawData $this->BYTE($Data);
    }
    
    public function 
WriteShort($data) {
        
$this->RawData $this->RawData $this->WORD($data);
    }
    
    public function 
WriteInt($data) {
        
$this->RawData $this->RawData $this->DWORD($data);
    }
    
    public function 
WriteDouble($data) {
        
$this->RawData $this->RawData $this->DOUBLE($data);
    }
    
    public function 
WriteFloat($data) {
        
$this->RawData $this->RawData $this->FLOAT($data);
    }
}
?>