<?
/**
* PServer
*
* Class in PHP to create new Server Socket BSD
*
* @package pserver
* @subpackage pserver.class
* @author Pedro Vargas (hide@address.com) http://deerme.org
* @version 0.1
* @licence GNU General Public License (GPL)
*/
class pserver
{
/**
* @var $ip
* This is the ip of server
*/
var $ip;
/**
* @var $port
* This is the port of server
*/
var $port;
/**
* @var $sock
* This is the socket listen
*/
var $sock;
/**
* @var $maxc
* Maxime client connect
*/
var $maxc = 5;
/**
* @var $clients
* Clients connect to Server
*/
var $clients;
/**
* @var $msg_welcome
* Message welcome
*/
var $msg_welcome = "";
/**
* @var $msg_full
* Message to many clients connnected
*/
var $msg_full = "To many Clients connected!";
/**
* @var $bufferin
* Buffer in
*/
var $bufferin = "1024";
/**
* @var $pids
* Pids of the proceess
*/
var $pids;
var $socketbinary = false;
/**
* Constructor of pserver Class
*
* @param string $ip This is the ip of server
* @param string $port This is the port of server
*/
function pserver( $ip = '0' , $port = '30000' )
{
$this->ip = $ip;
$this->port = $port;
set_time_limit(0);
}
/**
* Start the Server
*
* @return boolean true If possible run the Server
*/
function start()
{
if ( $this->open_socket() )
{
$this->logger("Start Server","Listen in ".$this->ip.":".$this->port."");
$this->pids = array();
while( 1 == 1 )
{
$read[0] = $this->sock;
for($i=1; $i<count($this->clients)+1; ++$i)
{
if($this->clients[$i] != NULL)
{
$read[$i+1] = $this->clients[$i]['socket'];
}
}
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
if(in_array($this->sock, $read))
{
for($i=1; $i < ($this->maxc+1); $i++)
{
if(!isset($this->clients[$i]))
{
$this->clients[$i]['socket'] = socket_accept($this->sock);
socket_getpeername($this->clients[$i]['socket'],$ip);
$this->clients[$i]['ip'] = $ip;
// Call welcome msg
$this->client_welcome( $this->clients[$i]['socket'] );
$this->logger("New Client",$this->clients[$i]['ip']);
break ;
}
elseif($i == $this->maxc - 1)
{
$this->logger("New client", $this->msg_full );
$socktemp = socket_accept($this->sock);
socket_write($socktemp , $this->msg_full."\n\r");
socket_close($socktemp);
}
if($ready < 1)
{
continue;
}
}
}
for($i=1; $i<($this->maxc+1); ++$i)
{
if(in_array($this->clients[$i]['socket'], $read))
{
// PHP_BINARY_READ | PHP_NORMAL_READ
$data = FALSE;
if ( $this->socketbinary )
$data = @socket_read($this->clients[$i]['socket'], $this->bufferin, PHP_BINARY_READ);
else
$data = @socket_read($this->clients[$i]['socket'], $this->bufferin, PHP_NORMAL_READ);
if($data === FALSE)
{
$this->logger("Client disconnected",$this->clients[$i]['ip']);
unset($this->clients[$i]);
break;
}
else
{
// Call method read Data
$this->readData( $data , $this->clients[$i] );
$this->writeData( $data , $this->clients[$i] );
}
}
}
}
}
}
/**
* Open the Socket Listen
*
* @return boolean true If possible opened socket
*/
function open_socket()
{
$sock = &$this->sock;
// Open
if ( ($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == false )
{
$this->logger("Start Server","socket_create() failed: reason:".socket_strerror($sock));
die();
}
// Binding
if ( ($ret = socket_bind($sock, $this->ip, $this->port)) == false )
{
$this->logger("Start Server","socket_bind() failed: reason: " . socket_strerror($ret));
die();
}
// Listing
if ( ($ret = socket_listen($sock, (int)$this->maxc )) == false )
{
$this->logger("Start Server","socket_listen() failed: reason: ".socket_strerror($ret));
die();
}
$this->clients = array('0' => array('socket' => &$sock , 'time' => time() ));
return true;
}
/**
* Print first msg in the client
*/
function client_welcome( &$sock )
{
socket_write( $sock , $this->msg_welcome );
}
/**
* Read data of send the client
*/
function readData( $data , $client )
{
$data = trim($data);
$this->tpl["msg"] = "Base64(".$data.") = '".base64_encode($data)."'\n\r";
}
/**
* Write data on the client
*/
function writeData( $data , $client)
{
socket_write($client['socket'], $this->tpl["msg"] );
}
/**
* Logger information of the Server
*/
function logger( $area , $msg )
{
print date("[Y-m-d H:i:s]")."\t".$area."\t".$msg."\n\r";
}
/**
Color ASCII
*/
function colorshell($c,$t)
{
return sprintf("%c[%d;%d;%dm".$t,27,1, $c ,40);
}
}
?>