/**
* debug destination (filename or stdout)
* @var string $debugDest
*/
var $debugDest = "stdout";
/**
* empty array, used for socket_select
* @var array $null
*/
var $null = array();
/**
* all file descriptors are stored here
* @var array $clientFD
*/
var $clientFD = array();
/**
* needed to store client information
* @var array $clientInfo
*/
var $clientInfo = array();
/**
* needed to store server information
* @var array $serverInfo
*/
var $serverInfo = array();
/**
* amount of clients
* @var integer $clients
*/
var $clients = 0;
/**
* create a new socket server
*
* @access public
* @param string $domain domain to bind to
* @param integer $port port to listen to
*/
function patServer( $domain = "localhost", $port = 10000 ){
$this->domain = $domain;
$this->port = $port;
/**
* set maximum amount of simultaneous connections
*
* @access public
* @param int $maxClients
*/
function setMaxClients( $maxClients ){
$this->maxClients = $maxClients;
}
/**
* set debug mode
*
* @access public
* @param mixed $debug [text|htmlfalse]
* @param string $dest destination of debug message (stdout to output or filename if log should be written)
*/
function setDebugMode( $debug, $dest = "stdout" ){
if( $debug === false ){
$this->debug = false;
return true;
}
/**
* start the server
*
* @access public
* @param int $maxClients
*/
function start(){
$this->initFD = @socket_create( AF_INET, SOCK_STREAM, 0 );
if( !$this->initFD )
die( "patServer: Could not create socket." );
// adress may be reused
socket_setopt( $this->initFD, SOL_SOCKET, SO_REUSEADDR, 1 );
// bind the socket
if( !@socket_bind( $this->initFD, $this->domain, $this->port ) ){
@socket_close( $this->initFD );
die( "patServer: Could not bind socket to ".$this->domain." on port ".$this->port." ( ".$this->getLastSocketError( $this->initFd )." )." );
}
// listen on selected port
if( !@socket_listen( $this->initFD, $this->maxQueue ) )
die( "patServer: Could not listen ( ".$this->getLastSocketError( $this->initFd )." )." );
$this->sendDebugMessage( "Listening on port ".$this->port.". Server started at ".date( "H:i:s", time() ) );
// this allows the shutdown function to check whether the server is already shut down
$GLOBALS["_patServerStatus"] = "running";
// this ensures that the server will be sutdown correctly
register_shutdown_function( array( $this, "shutdown" ) );
/**
* read from a socket
*
* @access private
* @param integer $clientId internal id of the client to read from
* @return string $data data that was read
*/
function readFromSocket( $clientId ){
// start with empty string
$data = "";
// read data from socket
while( $buf = socket_read( $this->clientFD[$clientId], $this->readBufferSize ) ){
$data .= $buf;
/**
* check, whether a client is still connected
*
* @access public
* @param integer $id client id
* @return boolean $connected true if client is connected, false otherwise
*/
function isConnected( $id ){
if( !isset( $this->clientFD[$id] ) )
return false;
return true;
}
/**
* close connection to a client
*
* @access public
* @param int $clientID internal ID of the client
*/
function closeConnection( $id ){
if( !isset( $this->clientFD[$id] ) )
return false;
/**
* get current amount of clients
*
* @access public
* @return int $clients amount of clients
*/
function getClients(){
return $this->clients;
}
/**
* send data to a client
*
* @access public
* @param int $clientId ID of the client
* @param string $data data to send
* @param boolean $debugData flag to indicate whether data that is written to socket should also be sent as debug message
*/
function sendData( $clientId, $data, $debugData = true ){
if( !isset( $this->clientFD[$clientId] ) || $this->clientFD[$clientId] == NULL )
return false;
/**
* get current information about a client
*
* @access public
* @param int $clientId ID of the client
* @return array $info information about the client
*/
function getClientInfo( $clientId ){
if( !isset( $this->clientFD[$clientId] ) || $this->clientFD[$clientId] == NULL )
return false;
return $this->clientInfo[$clientId];
}
/**
* send a debug message
*
* @access private
* @param string $msg message to debug
*/
function sendDebugMessage( $msg ){
if( !$this->debug )
return false;