cvs: pear /Net_Server Server.php /Net_Server/Server Driver.php /Net_Server/Server/Driver Fork.php Sequential.php /Net_Server/Server/Drivers
Fork.php Sequential.php
| From: | Stephan Schmidt | Date: | Fri, 02 Jan 2004 16:04:24 +0000 |
| Subject: | cvs: pear /Net_Server Server.php /Net_Server/Server Driver.php /Net_Server/Server/Driver Fork.php Sequential.php /Net_Server/Server/Drivers Fork.php Sequential.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-15273@lists.php.net to get a copy of this message | ||
schst Fri Jan 2 11:04:24 2004 EDT
Added files:
/pear/Net_Server/Server Driver.php
/pear/Net_Server/Server/Driver Fork.php Sequential.php
Removed files:
/pear/Net_Server/Server/Drivers Fork.php Sequential.php
Modified files:
/pear/Net_Server Server.php
Log:
added driver base class and moved code from Net_Server into this class
Net_Server class is now only used as factory
Index: pear/Net_Server/Server.php diff -u pear/Net_Server/Server.php:1.5 pear/Net_Server/Server.php:1.6 --- pear/Net_Server/Server.php:1.5 Fri Oct 31 11:25:36 2003 +++ pear/Net_Server/Server.php Fri Jan 2 11:04:22 2004 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Server.php,v 1.5 2003/10/31 16:25:36 schst Exp $ +// $Id: Server.php,v 1.6 2004/01/02 16:04:22 schst Exp $ require_once 'PEAR.php'; @@ -69,121 +69,6 @@ * @author Stephan Schmidt <schst@php.net> */ class Net_Server extends PEAR { - /** - * port to listen - * @access private - * @var integer $port - */ - var $port = 10000; - - /** - * domain to bind to - * @access private - * @var string $domain - */ - var $domain = "localhost"; - - /** - * all file descriptors are stored here - * @access private - * @var array $clientFD - */ - var $clientFD = array(); - - /** - * maximum amount of clients - * @access private - * @var integer $maxClients - */ - var $maxClients = -1; - - /** - * buffer size for socket_read - * @access private - * @var integer $readBufferSize - */ - var $readBufferSize = 128; - - /** - * end character for socket_read - * @access private - * @var integer $readEndCharacter - */ - var $readEndCharacter = "\n"; - - /** - * maximum of backlog in queue - * @access private - * @var integer $maxQueue - */ - var $maxQueue = 500; - - /** - * debug mode - * @access private - * @var boolean $_debug - */ - var $_debug = true; - - /** - * debug mode, normally only text is needed, as servers should not be run in a browser - * @access private - * @var string $_debugMode - */ - var $_debugMode = "text"; - - /** - * debug destination (filename or stdout) - * @access private - * @var string $_debugDest - */ - var $_debugDest = "stdout"; - - /** - * empty array, used for socket_select - * @access private - * @var array $null - */ - var $null = array(); - - /** - * needed to store client information - * @access private - * @var array $clientInfo - */ - var $clientInfo = array(); - - /** - * constructor _MUST_ not be called directly - * - * instead please use the Net_Server::create() method - * that can be called statically and will return a server - * of the specified type - * - * @access private - * @param string $domain domain to bind to - * @param integer $port port to listen to - */ - function Net_Server($domain = "localhost", $port = 10000) - { - $this->PEAR(); - - $this->domain = $domain; - $this->port = $port; - - // this is only needed, when server is not run in CLI - set_time_limit(0); - } - - /** - * destructor - * - * @access private - */ - function _Net_Server() - { - $this->shutdown(); - } /** * create a new server @@ -221,116 +106,5 @@ $server = &new $className($host, $port); return $server; } - - /** - * 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; - } - - $this->_debug = true; - $this->_debugMode = $debug; - $this->_debugDest = $dest; - } - - - /** - * 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 = 0) { - // start with empty string - $data = ""; - - // read data from socket - while($buf = socket_read($this->clientFD[$clientId], $this->readBufferSize)) { - $data .= $buf; - - $endString = substr($buf, - strlen($this->readEndCharacter)); - if ($endString == $this->readEndCharacter) { - break; - } - if ($buf == null) { - break; - } - } - - if ($buf === false) { - $this->_sendDebugMessage("Could not read from client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); - } - - return $data; - } - - /** - * send a debug message - * - * @access private - * @param string $msg message to debug - */ - function _sendDebugMessage($msg) { - if (!$this->_debug) { - return false; - } - - $msg = date("Y-m-d H:i:s", time()) . " " . $msg; - - switch($this->_debugMode) { - case "text": - $msg = $msg."\n"; - break; - case "html": - $msg = htmlspecialchars($msg) . "<br />\n"; - break; - } - - if ($this->_debugDest == "stdout" || empty($this->_debugDest)) { - echo $msg; - flush(); - return true; - } - - error_log($msg, 3, $this->_debugDest); - return true; - } - - /** - * register a callback object, that is used to handle all events - * - * @access public - * @param object $object callback object - */ - function setCallbackObject(&$object) - { - $this->callbackObj = &$object; - if (method_exists($this->callbackObj,'setServerReference')) { - $this->callbackObj->setServerReference($this); - } - } - - /** - * return string for last socket error - * - * @access public - * @return string $error last error - */ - function getLastSocketError(&$fd) { - if(!is_resource($fd)) { - return ""; - } - $lastError = socket_last_error($fd); - return "Msg: " . socket_strerror($lastError) . " / Code: ".$lastError; - } } ?> \ No newline at end of file Index: pear/Net_Server/Server/Driver.php +++ pear/Net_Server/Server/Driver.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Driver.php,v 1.1 2004/01/02 16:04:23 schst Exp $ /** * Base class for all drivers * * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Driver { /** * port to listen * @access private * @var integer $port */ var $port = 10000; /** * domain to bind to * @access private * @var string $domain */ var $domain = "localhost"; /** * all file descriptors are stored here * @access private * @var array $clientFD */ var $clientFD = array(); /** * maximum amount of clients * @access private * @var integer $maxClients */ var $maxClients = -1; /** * buffer size for socket_read * @access private * @var integer $readBufferSize */ var $readBufferSize = 128; /** * end character for socket_read * @access private * @var integer $readEndCharacter */ var $readEndCharacter = "\n"; /** * maximum of backlog in queue * @access private * @var integer $maxQueue */ var $maxQueue = 500; /** * debug mode * @access private * @var boolean $_debug */ var $_debug = true; /** * debug mode, normally only text is needed, as servers should not be run in a browser * @access private * @var string $_debugMode */ var $_debugMode = "text"; /** * debug destination (filename or stdout) * @access private * @var string $_debugDest */ var $_debugDest = "stdout"; /** * empty array, used for socket_select * @access private * @var array $null */ var $null = array(); /** * needed to store client information * @access private * @var array $clientInfo */ var $clientInfo = array(); /** * constructor _MUST_ not be called directly * * instead please use the Net_Server::create() method * that can be called statically and will return a server * of the specified type * * @access private * @param string $domain domain to bind to * @param integer $port port to listen to */ function Net_Server_Driver($domain = "localhost", $port = 10000) { $this->PEAR(); $this->domain = $domain; $this->port = $port; // this is only needed, when server is not run in CLI set_time_limit(0); } /** * destructor * * @access private */ function _Net_Server_Driver() { $this->shutdown(); } /** * 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; } $this->_debug = true; $this->_debugMode = $debug; $this->_debugDest = $dest; } /** * 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 = 0) { // start with empty string $data = ""; // read data from socket while($buf = socket_read($this->clientFD[$clientId], $this->readBufferSize)) { $data .= $buf; $endString = substr($buf, - strlen($this->readEndCharacter)); if ($endString == $this->readEndCharacter) { break; } if ($buf == null) { break; } } if ($buf === false) { $this->_sendDebugMessage("Could not read from client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } return $data; } /** * send a debug message * * @access private * @param string $msg message to debug */ function _sendDebugMessage($msg) { if (!$this->_debug) { return false; } $msg = date("Y-m-d H:i:s", time()) . " " . $msg; switch($this->_debugMode) { case "text": $msg = $msg."\n"; break; case "html": $msg = htmlspecialchars($msg) . "<br />\n"; break; } if ($this->_debugDest == "stdout" || empty($this->_debugDest)) { echo $msg; flush(); return true; } error_log($msg, 3, $this->_debugDest); return true; } /** * register a callback object, that is used to handle all events * * @access public * @param object $object callback object */ function setCallbackObject(&$object) { $this->callbackObj = &$object; if (method_exists($this->callbackObj,'setServerReference')) { $this->callbackObj->setServerReference($this); } } /** * return string for last socket error * * @access public * @return string $error last error */ function getLastSocketError(&$fd) { if(!is_resource($fd)) { return ""; } $lastError = socket_last_error($fd); return "Msg: " . socket_strerror($lastError) . " / Code: ".$lastError; } } ?> Index: pear/Net_Server/Server/Driver/Fork.php +++ pear/Net_Server/Server/Driver/Fork.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Fork.php,v 1.1 2004/01/02 16:04:24 schst Exp $ /** * needs the driver base class */ require_once 'Net/Server/Driver.php'; /** * Forking server class. * * This class will fork a new process for each connection. * This allows you to build servers, where communication between * the clients is no issue. * * Events that can be handled: * - onStart * - onConnect * - onClose * - onReceiveData * * @version 0.10 alpha * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Fork extends Net_Server_Driver { /** * flag to indicate whether this is the parent * @access private * @var boolean */ var $_isParent = true; /** * set maximum amount of simultaneous connections * * this is not possible as each client gets its own * process * * @access public * @param int $maxClients */ function setMaxClients($maxClients) { return $this->raiseError('Not supported.', NET_SERVER_ERROR_NOT_SUPPORTED); } /** * start the server * * @access public */ function start() { if (!function_exists('pcntl_fork')) { return $this->raiseError('Needs pcntl extension to fork processes.', NET_SERVER_ERROR_PCNTL_REQUIRED); } $this->initFD = @socket_create(AF_INET, SOCK_STREAM, 0); if (!$this->initFD) { return $this->raiseError("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)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not bind socket to ".$this->domain." on port ".$this->port." (".$error.")."); } // listen on selected port if (!@socket_listen($this->initFD, $this->maxQueue)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not listen (".$error.")."); } $this->_sendDebugMessage("Listening on port ".$this->port.". Server started at ".date("H:i:s", time())); if (method_exists($this->callbackObj, "onStart")) { $this->callbackObj->onStart(); } // Dear children, please do not become zombies pcntl_signal(SIGCHLD, SIG_IGN); // wait for incmoning connections while (true) { // new connection if(($fd = socket_accept($this->initFD))) { $pid = pcntl_fork(); if($pid == -1) { return $this->raiseError('Could not fork child process.'); } // This is the child => handle the request elseif($pid == 0) { // this is not the parent $this->_isParent = false; // store the new file descriptor $this->clientFD[0] = $fd; $peer_host = ""; $peer_port = ""; socket_getpeername($this->clientFD[0], $peer_host, $peer_port); $this->clientInfo[0] = array( "host" => $peer_host, "port" => $peer_port, "connectOn" => time() ); $this->_sendDebugMessage("New connection from ".$peer_host." on port ".$peer_port); if (method_exists($this->callbackObj, "onConnect")) { $this->callbackObj->onConnect($i); } $this->serviceRequest(); $this->closeConnection(); exit; } else { // the parent process does not have to do anything } } } } /** * service the current request * * * */ function serviceRequest() { while( true ) { $readFDs = array( $this->clientFD[0] ); // block and wait for data $ready = @socket_select($readFDs, $this->null, $this->null, null); if ($ready === false) { $this->_sendDebugMessage("socket_select failed."); $this->shutdown(); } if (in_array($this->clientFD[0], $readFDs)) { $data = $this->readFromSocket(); // empty data => connection was closed if (!$data) { $this->_sendDebugMessage("Connection closed by peer"); $this->closeConnection(); } else { $this->_sendDebugMessage("Received ".trim($data)." from ".$i); if (method_exists($this->callbackObj, "onReceiveData")) { $this->callbackObj->onReceiveData(0, $data); } } } } } /** * 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() { if (is_resource($this->clientFD[0])) { return true; } } /** * get current amount of clients * * not possible with forking * * @access public * @return PEAR_Error */ function getClients() { return $this->raiseError('Not implemented'); } /** * send data to a client * * @access public * @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($data, $debugData = true) { // keep it compatible to Net_Server_Sequential if (is_string($debugData)) { $data = $debugData; } if (!isset($this->clientFD[0]) || $this->clientFD[0] == null) { return $this->raiseError("Client does not exist."); } if ($debugData) { $this->_sendDebugMessage("sending: \"" . $data . "\" to: $clientId" ); } if (!@socket_write($this->clientFD[0], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } } /** * send data to all clients * * @access public * @param string $data data to send * @param array $exclude client ids to exclude */ function broadcastData($data, $exclude = array()) { $this->sendData($data); } /** * get current information about a client * * @access public * @return array $info information about the client */ function getClientInfo() { if (!isset($this->clientFD[0]) || $this->clientFD[0] == null) { return $this->raiseError("Client does not exist."); } return $this->clientInfo[$clientId]; } /** * close the current connection * * @access public */ function closeConnection() { if (!isset($this->clientFD[0])) { return $this->raiseError( "Connection already has been closed." ); } if (method_exists($this->callbackObj, "onClose")) { $this->callbackObj->onClose($id); } $this->_sendDebugMessage("Closed connection from ".$this->clientInfo[0]["host"]." on port ".$this->clientInfo[0]["port"]); @socket_close($this->clientFD[0]); $this->clientFD[0] = null; unset($this->clientInfo[0]); exit(); } /** * shutdown server * * @access public */ function shutDown() { $this->closeConnection(); exit; } } ?> Index: pear/Net_Server/Server/Driver/Sequential.php +++ pear/Net_Server/Server/Driver/Sequential.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Sequential.php,v 1.1 2004/01/02 16:04:24 schst Exp $ /** * needs the driver base class */ require_once 'Net/Server/Driver.php'; /** * Sequential server class. * * This class will handles all connection in one server process. * This allows you to build servers, where communication between * the clients is easy. The drawback is that clients are served * sequentially (hence the name). If you send large blocks of data * to a client, the others will have to wait. * For servers where communication between clients is not needed, * use Net_Server_Fork instead. * * Events that can be handled: * - onStart * - onConnect * - onConnectionRefused * - onClose * - onReceiveData * - onShutdown * * @version 0.10 alpha * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Sequential extends Net_Server_Driver { /** * amount of clients * @var integer $clients */ var $clients = 0; /** * set maximum amount of simultaneous connections * * @access public * @param int $maxClients */ function setMaxClients($maxClients) { $this->maxClients = $maxClients; } /** * start the server * * @access public */ function start() { $this->initFD = @socket_create(AF_INET, SOCK_STREAM, 0); if (!$this->initFD) { return $this->raiseError("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)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not bind socket to ".$this->domain." on port ".$this->port." (".$error.")."); } // listen on selected port if (!@socket_listen($this->initFD, $this->maxQueue)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not listen (".$error.")."); } $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["_Net_Server_Status"] = "running"; if (method_exists($this->callbackObj, "onStart")) { $this->callbackObj->onStart(); } while (true) { $readFDs = array(); array_push($readFDs, $this->initFD); // fetch all clients that are awaiting connections for ($i = 0; $i < count($this->clientFD); $i++) { if (isset($this->clientFD[$i])) array_push($readFDs, $this->clientFD[$i]); } // block and wait for data or new connection $ready = @socket_select($readFDs, $this->null, $this->null, NULL); if ($ready === false) { $this->_sendDebugMessage("socket_select failed."); $this->shutdown(); } // check for new connection if (in_array($this->initFD, $readFDs)) { $newClient = $this->acceptConnection($this->initFD); // check for maximum amount of connections if ($this->maxClients > 0) { if ($this->clients > $this->maxClients) { $this->_sendDebugMessage("Too many connections."); if (method_exists($this->callbackObj, "onConnectionRefused")) { $this->callbackObj->onConnectionRefused($newClient); } $this->closeConnection($newClient); } } if (--$ready <= 0) { continue; } } // check all clients for incoming data for($i = 0; $i < count($this->clientFD); $i++) { if (!isset($this->clientFD[$i])) { continue; } if (in_array($this->clientFD[$i], $readFDs)) { $data = $this->readFromSocket($i); // empty data => connection was closed if (!$data) { $this->_sendDebugMessage("Connection closed by peer"); $this->closeConnection($i); } else { $this->_sendDebugMessage("Received ".trim($data)." from ".$i); if (method_exists($this->callbackObj, "onReceiveData")) { $this->callbackObj->onReceiveData($i, $data); } } } } } } /** * accept a new connection * * @access public * @param resource &$socket socket that received the new connection * @return int $clientID internal ID of the client */ function acceptConnection(&$socket) { for($i = 0 ; $i <= count($this->clientFD); $i++) { if (!isset($this->clientFD[$i]) || $this->clientFD[$i] == NULL) { $this->clientFD[$i] = socket_accept($socket); socket_setopt($this->clientFD[$i], SOL_SOCKET, SO_REUSEADDR, 1); $peer_host = ""; $peer_port = ""; socket_getpeername($this->clientFD[$i], $peer_host, $peer_port); $this->clientInfo[$i] = array( "host" => $peer_host, "port" => $peer_port, "connectOn" => time() ); $this->clients++; $this->_sendDebugMessage("New connection (".$i.") from ".$peer_host." on port ".$peer_port); if (method_exists($this->callbackObj, "onConnect")) { $this->callbackObj->onConnect($i); } return $i; } } } /** * 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; } /** * 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 $this->raiseError("Client does not exist."); } if ($debugData) { $this->_sendDebugMessage("sending: \"" . $data . "\" to: $clientId" ); } if (!@socket_write($this->clientFD[$clientId], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } } /** * send data to all clients * * @access public * @param string $data data to send * @param array $exclude client ids to exclude */ function broadcastData($data, $exclude = array()) { if (!empty($exclude) && !is_array($exclude)) { $exclude = array($exclude); } for($i = 0; $i < count($this->clientFD); $i++) { if (isset($this->clientFD[$i]) && $this->clientFD[$i] != NULL && !in_array($i, $exclude)) { if (!@socket_write($this->clientFD[$i], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$i." (".$this->getLastSocketError($this->clientFD[$i]).")."); } } } } /** * 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 $this->raiseError("Client does not exist."); } return $this->clientInfo[$clientId]; } /** * close connection to a client * * @access public * @param int $clientID internal ID of the client */ function closeConnection($id = 0) { if (!isset($this->clientFD[$id])) { return $this->raiseError( "Connection already has been closed." ); } if (method_exists($this->callbackObj, "onClose")) { $this->callbackObj->onClose($id); } $this->_sendDebugMessage("Closed connection (".$id.") from ".$this->clientInfo[$id]["host"]." on port ".$this->clientInfo[$id]["port"]); @socket_close($this->clientFD[$id]); $this->clientFD[$id] = NULL; unset($this->clientInfo[$id]); $this->clients--; } /** * shutdown server * * @access public */ function shutDown() { if ($GLOBALS["_Net_Server_Status"] != "running") { exit(); } $GLOBALS["_Net_Server_Status"] = "stopped"; if (method_exists($this->callbackObj, "onShutdown")) { $this->callbackObj->onShutdown(); } $maxFD = count($this->clientFD); for($i = 0; $i < $maxFD; $i++) { $this->closeConnection($i); } @socket_close($this->initFD); $this->_sendDebugMessage("Shutdown server."); exit(); } } ?>
Index: pear/Net_Server/Server.php diff -u pear/Net_Server/Server.php:1.5 pear/Net_Server/Server.php:1.6 --- pear/Net_Server/Server.php:1.5 Fri Oct 31 11:25:36 2003 +++ pear/Net_Server/Server.php Fri Jan 2 11:04:22 2004 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Server.php,v 1.5 2003/10/31 16:25:36 schst Exp $ +// $Id: Server.php,v 1.6 2004/01/02 16:04:22 schst Exp $ require_once 'PEAR.php'; @@ -69,121 +69,6 @@ * @author Stephan Schmidt <schst@php.net> */ class Net_Server extends PEAR { - /** - * port to listen - * @access private - * @var integer $port - */ - var $port = 10000; - - /** - * domain to bind to - * @access private - * @var string $domain - */ - var $domain = "localhost"; - - /** - * all file descriptors are stored here - * @access private - * @var array $clientFD - */ - var $clientFD = array(); - - /** - * maximum amount of clients - * @access private - * @var integer $maxClients - */ - var $maxClients = -1; - - /** - * buffer size for socket_read - * @access private - * @var integer $readBufferSize - */ - var $readBufferSize = 128; - - /** - * end character for socket_read - * @access private - * @var integer $readEndCharacter - */ - var $readEndCharacter = "\n"; - - /** - * maximum of backlog in queue - * @access private - * @var integer $maxQueue - */ - var $maxQueue = 500; - - /** - * debug mode - * @access private - * @var boolean $_debug - */ - var $_debug = true; - - /** - * debug mode, normally only text is needed, as servers should not be run in a browser - * @access private - * @var string $_debugMode - */ - var $_debugMode = "text"; - - /** - * debug destination (filename or stdout) - * @access private - * @var string $_debugDest - */ - var $_debugDest = "stdout"; - - /** - * empty array, used for socket_select - * @access private - * @var array $null - */ - var $null = array(); - - /** - * needed to store client information - * @access private - * @var array $clientInfo - */ - var $clientInfo = array(); - - /** - * constructor _MUST_ not be called directly - * - * instead please use the Net_Server::create() method - * that can be called statically and will return a server - * of the specified type - * - * @access private - * @param string $domain domain to bind to - * @param integer $port port to listen to - */ - function Net_Server($domain = "localhost", $port = 10000) - { - $this->PEAR(); - - $this->domain = $domain; - $this->port = $port; - - // this is only needed, when server is not run in CLI - set_time_limit(0); - } - - /** - * destructor - * - * @access private - */ - function _Net_Server() - { - $this->shutdown(); - } /** * create a new server @@ -221,116 +106,5 @@ $server = &new $className($host, $port); return $server; } - - /** - * 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; - } - - $this->_debug = true; - $this->_debugMode = $debug; - $this->_debugDest = $dest; - } - - - /** - * 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 = 0) { - // start with empty string - $data = ""; - - // read data from socket - while($buf = socket_read($this->clientFD[$clientId], $this->readBufferSize)) { - $data .= $buf; - - $endString = substr($buf, - strlen($this->readEndCharacter)); - if ($endString == $this->readEndCharacter) { - break; - } - if ($buf == null) { - break; - } - } - - if ($buf === false) { - $this->_sendDebugMessage("Could not read from client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); - } - - return $data; - } - - /** - * send a debug message - * - * @access private - * @param string $msg message to debug - */ - function _sendDebugMessage($msg) { - if (!$this->_debug) { - return false; - } - - $msg = date("Y-m-d H:i:s", time()) . " " . $msg; - - switch($this->_debugMode) { - case "text": - $msg = $msg."\n"; - break; - case "html": - $msg = htmlspecialchars($msg) . "<br />\n"; - break; - } - - if ($this->_debugDest == "stdout" || empty($this->_debugDest)) { - echo $msg; - flush(); - return true; - } - - error_log($msg, 3, $this->_debugDest); - return true; - } - - /** - * register a callback object, that is used to handle all events - * - * @access public - * @param object $object callback object - */ - function setCallbackObject(&$object) - { - $this->callbackObj = &$object; - if (method_exists($this->callbackObj,'setServerReference')) { - $this->callbackObj->setServerReference($this); - } - } - - /** - * return string for last socket error - * - * @access public - * @return string $error last error - */ - function getLastSocketError(&$fd) { - if(!is_resource($fd)) { - return ""; - } - $lastError = socket_last_error($fd); - return "Msg: " . socket_strerror($lastError) . " / Code: ".$lastError; - } } ?> \ No newline at end of file Index: pear/Net_Server/Server/Driver.php +++ pear/Net_Server/Server/Driver.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Driver.php,v 1.1 2004/01/02 16:04:23 schst Exp $ /** * Base class for all drivers * * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Driver { /** * port to listen * @access private * @var integer $port */ var $port = 10000; /** * domain to bind to * @access private * @var string $domain */ var $domain = "localhost"; /** * all file descriptors are stored here * @access private * @var array $clientFD */ var $clientFD = array(); /** * maximum amount of clients * @access private * @var integer $maxClients */ var $maxClients = -1; /** * buffer size for socket_read * @access private * @var integer $readBufferSize */ var $readBufferSize = 128; /** * end character for socket_read * @access private * @var integer $readEndCharacter */ var $readEndCharacter = "\n"; /** * maximum of backlog in queue * @access private * @var integer $maxQueue */ var $maxQueue = 500; /** * debug mode * @access private * @var boolean $_debug */ var $_debug = true; /** * debug mode, normally only text is needed, as servers should not be run in a browser * @access private * @var string $_debugMode */ var $_debugMode = "text"; /** * debug destination (filename or stdout) * @access private * @var string $_debugDest */ var $_debugDest = "stdout"; /** * empty array, used for socket_select * @access private * @var array $null */ var $null = array(); /** * needed to store client information * @access private * @var array $clientInfo */ var $clientInfo = array(); /** * constructor _MUST_ not be called directly * * instead please use the Net_Server::create() method * that can be called statically and will return a server * of the specified type * * @access private * @param string $domain domain to bind to * @param integer $port port to listen to */ function Net_Server_Driver($domain = "localhost", $port = 10000) { $this->PEAR(); $this->domain = $domain; $this->port = $port; // this is only needed, when server is not run in CLI set_time_limit(0); } /** * destructor * * @access private */ function _Net_Server_Driver() { $this->shutdown(); } /** * 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; } $this->_debug = true; $this->_debugMode = $debug; $this->_debugDest = $dest; } /** * 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 = 0) { // start with empty string $data = ""; // read data from socket while($buf = socket_read($this->clientFD[$clientId], $this->readBufferSize)) { $data .= $buf; $endString = substr($buf, - strlen($this->readEndCharacter)); if ($endString == $this->readEndCharacter) { break; } if ($buf == null) { break; } } if ($buf === false) { $this->_sendDebugMessage("Could not read from client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } return $data; } /** * send a debug message * * @access private * @param string $msg message to debug */ function _sendDebugMessage($msg) { if (!$this->_debug) { return false; } $msg = date("Y-m-d H:i:s", time()) . " " . $msg; switch($this->_debugMode) { case "text": $msg = $msg."\n"; break; case "html": $msg = htmlspecialchars($msg) . "<br />\n"; break; } if ($this->_debugDest == "stdout" || empty($this->_debugDest)) { echo $msg; flush(); return true; } error_log($msg, 3, $this->_debugDest); return true; } /** * register a callback object, that is used to handle all events * * @access public * @param object $object callback object */ function setCallbackObject(&$object) { $this->callbackObj = &$object; if (method_exists($this->callbackObj,'setServerReference')) { $this->callbackObj->setServerReference($this); } } /** * return string for last socket error * * @access public * @return string $error last error */ function getLastSocketError(&$fd) { if(!is_resource($fd)) { return ""; } $lastError = socket_last_error($fd); return "Msg: " . socket_strerror($lastError) . " / Code: ".$lastError; } } ?> Index: pear/Net_Server/Server/Driver/Fork.php +++ pear/Net_Server/Server/Driver/Fork.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Fork.php,v 1.1 2004/01/02 16:04:24 schst Exp $ /** * needs the driver base class */ require_once 'Net/Server/Driver.php'; /** * Forking server class. * * This class will fork a new process for each connection. * This allows you to build servers, where communication between * the clients is no issue. * * Events that can be handled: * - onStart * - onConnect * - onClose * - onReceiveData * * @version 0.10 alpha * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Fork extends Net_Server_Driver { /** * flag to indicate whether this is the parent * @access private * @var boolean */ var $_isParent = true; /** * set maximum amount of simultaneous connections * * this is not possible as each client gets its own * process * * @access public * @param int $maxClients */ function setMaxClients($maxClients) { return $this->raiseError('Not supported.', NET_SERVER_ERROR_NOT_SUPPORTED); } /** * start the server * * @access public */ function start() { if (!function_exists('pcntl_fork')) { return $this->raiseError('Needs pcntl extension to fork processes.', NET_SERVER_ERROR_PCNTL_REQUIRED); } $this->initFD = @socket_create(AF_INET, SOCK_STREAM, 0); if (!$this->initFD) { return $this->raiseError("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)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not bind socket to ".$this->domain." on port ".$this->port." (".$error.")."); } // listen on selected port if (!@socket_listen($this->initFD, $this->maxQueue)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not listen (".$error.")."); } $this->_sendDebugMessage("Listening on port ".$this->port.". Server started at ".date("H:i:s", time())); if (method_exists($this->callbackObj, "onStart")) { $this->callbackObj->onStart(); } // Dear children, please do not become zombies pcntl_signal(SIGCHLD, SIG_IGN); // wait for incmoning connections while (true) { // new connection if(($fd = socket_accept($this->initFD))) { $pid = pcntl_fork(); if($pid == -1) { return $this->raiseError('Could not fork child process.'); } // This is the child => handle the request elseif($pid == 0) { // this is not the parent $this->_isParent = false; // store the new file descriptor $this->clientFD[0] = $fd; $peer_host = ""; $peer_port = ""; socket_getpeername($this->clientFD[0], $peer_host, $peer_port); $this->clientInfo[0] = array( "host" => $peer_host, "port" => $peer_port, "connectOn" => time() ); $this->_sendDebugMessage("New connection from ".$peer_host." on port ".$peer_port); if (method_exists($this->callbackObj, "onConnect")) { $this->callbackObj->onConnect($i); } $this->serviceRequest(); $this->closeConnection(); exit; } else { // the parent process does not have to do anything } } } } /** * service the current request * * * */ function serviceRequest() { while( true ) { $readFDs = array( $this->clientFD[0] ); // block and wait for data $ready = @socket_select($readFDs, $this->null, $this->null, null); if ($ready === false) { $this->_sendDebugMessage("socket_select failed."); $this->shutdown(); } if (in_array($this->clientFD[0], $readFDs)) { $data = $this->readFromSocket(); // empty data => connection was closed if (!$data) { $this->_sendDebugMessage("Connection closed by peer"); $this->closeConnection(); } else { $this->_sendDebugMessage("Received ".trim($data)." from ".$i); if (method_exists($this->callbackObj, "onReceiveData")) { $this->callbackObj->onReceiveData(0, $data); } } } } } /** * 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() { if (is_resource($this->clientFD[0])) { return true; } } /** * get current amount of clients * * not possible with forking * * @access public * @return PEAR_Error */ function getClients() { return $this->raiseError('Not implemented'); } /** * send data to a client * * @access public * @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($data, $debugData = true) { // keep it compatible to Net_Server_Sequential if (is_string($debugData)) { $data = $debugData; } if (!isset($this->clientFD[0]) || $this->clientFD[0] == null) { return $this->raiseError("Client does not exist."); } if ($debugData) { $this->_sendDebugMessage("sending: \"" . $data . "\" to: $clientId" ); } if (!@socket_write($this->clientFD[0], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } } /** * send data to all clients * * @access public * @param string $data data to send * @param array $exclude client ids to exclude */ function broadcastData($data, $exclude = array()) { $this->sendData($data); } /** * get current information about a client * * @access public * @return array $info information about the client */ function getClientInfo() { if (!isset($this->clientFD[0]) || $this->clientFD[0] == null) { return $this->raiseError("Client does not exist."); } return $this->clientInfo[$clientId]; } /** * close the current connection * * @access public */ function closeConnection() { if (!isset($this->clientFD[0])) { return $this->raiseError( "Connection already has been closed." ); } if (method_exists($this->callbackObj, "onClose")) { $this->callbackObj->onClose($id); } $this->_sendDebugMessage("Closed connection from ".$this->clientInfo[0]["host"]." on port ".$this->clientInfo[0]["port"]); @socket_close($this->clientFD[0]); $this->clientFD[0] = null; unset($this->clientInfo[0]); exit(); } /** * shutdown server * * @access public */ function shutDown() { $this->closeConnection(); exit; } } ?> Index: pear/Net_Server/Server/Driver/Sequential.php +++ pear/Net_Server/Server/Driver/Sequential.php <?PHP /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Stephan Schmidt <schst@php.net> | // +----------------------------------------------------------------------+ // // $Id: Sequential.php,v 1.1 2004/01/02 16:04:24 schst Exp $ /** * needs the driver base class */ require_once 'Net/Server/Driver.php'; /** * Sequential server class. * * This class will handles all connection in one server process. * This allows you to build servers, where communication between * the clients is easy. The drawback is that clients are served * sequentially (hence the name). If you send large blocks of data * to a client, the others will have to wait. * For servers where communication between clients is not needed, * use Net_Server_Fork instead. * * Events that can be handled: * - onStart * - onConnect * - onConnectionRefused * - onClose * - onReceiveData * - onShutdown * * @version 0.10 alpha * @author Stephan Schmidt <schst@php.net> */ class Net_Server_Sequential extends Net_Server_Driver { /** * amount of clients * @var integer $clients */ var $clients = 0; /** * set maximum amount of simultaneous connections * * @access public * @param int $maxClients */ function setMaxClients($maxClients) { $this->maxClients = $maxClients; } /** * start the server * * @access public */ function start() { $this->initFD = @socket_create(AF_INET, SOCK_STREAM, 0); if (!$this->initFD) { return $this->raiseError("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)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not bind socket to ".$this->domain." on port ".$this->port." (".$error.")."); } // listen on selected port if (!@socket_listen($this->initFD, $this->maxQueue)) { $error = $this->getLastSocketError($this->initFd); @socket_close($this->initFD); return $this->raiseError("Could not listen (".$error.")."); } $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["_Net_Server_Status"] = "running"; if (method_exists($this->callbackObj, "onStart")) { $this->callbackObj->onStart(); } while (true) { $readFDs = array(); array_push($readFDs, $this->initFD); // fetch all clients that are awaiting connections for ($i = 0; $i < count($this->clientFD); $i++) { if (isset($this->clientFD[$i])) array_push($readFDs, $this->clientFD[$i]); } // block and wait for data or new connection $ready = @socket_select($readFDs, $this->null, $this->null, NULL); if ($ready === false) { $this->_sendDebugMessage("socket_select failed."); $this->shutdown(); } // check for new connection if (in_array($this->initFD, $readFDs)) { $newClient = $this->acceptConnection($this->initFD); // check for maximum amount of connections if ($this->maxClients > 0) { if ($this->clients > $this->maxClients) { $this->_sendDebugMessage("Too many connections."); if (method_exists($this->callbackObj, "onConnectionRefused")) { $this->callbackObj->onConnectionRefused($newClient); } $this->closeConnection($newClient); } } if (--$ready <= 0) { continue; } } // check all clients for incoming data for($i = 0; $i < count($this->clientFD); $i++) { if (!isset($this->clientFD[$i])) { continue; } if (in_array($this->clientFD[$i], $readFDs)) { $data = $this->readFromSocket($i); // empty data => connection was closed if (!$data) { $this->_sendDebugMessage("Connection closed by peer"); $this->closeConnection($i); } else { $this->_sendDebugMessage("Received ".trim($data)." from ".$i); if (method_exists($this->callbackObj, "onReceiveData")) { $this->callbackObj->onReceiveData($i, $data); } } } } } } /** * accept a new connection * * @access public * @param resource &$socket socket that received the new connection * @return int $clientID internal ID of the client */ function acceptConnection(&$socket) { for($i = 0 ; $i <= count($this->clientFD); $i++) { if (!isset($this->clientFD[$i]) || $this->clientFD[$i] == NULL) { $this->clientFD[$i] = socket_accept($socket); socket_setopt($this->clientFD[$i], SOL_SOCKET, SO_REUSEADDR, 1); $peer_host = ""; $peer_port = ""; socket_getpeername($this->clientFD[$i], $peer_host, $peer_port); $this->clientInfo[$i] = array( "host" => $peer_host, "port" => $peer_port, "connectOn" => time() ); $this->clients++; $this->_sendDebugMessage("New connection (".$i.") from ".$peer_host." on port ".$peer_port); if (method_exists($this->callbackObj, "onConnect")) { $this->callbackObj->onConnect($i); } return $i; } } } /** * 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; } /** * 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 $this->raiseError("Client does not exist."); } if ($debugData) { $this->_sendDebugMessage("sending: \"" . $data . "\" to: $clientId" ); } if (!@socket_write($this->clientFD[$clientId], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$clientId." (".$this->getLastSocketError($this->clientFD[$clientId]).")."); } } /** * send data to all clients * * @access public * @param string $data data to send * @param array $exclude client ids to exclude */ function broadcastData($data, $exclude = array()) { if (!empty($exclude) && !is_array($exclude)) { $exclude = array($exclude); } for($i = 0; $i < count($this->clientFD); $i++) { if (isset($this->clientFD[$i]) && $this->clientFD[$i] != NULL && !in_array($i, $exclude)) { if (!@socket_write($this->clientFD[$i], $data)) { $this->_sendDebugMessage("Could not write '".$data."' client ".$i." (".$this->getLastSocketError($this->clientFD[$i]).")."); } } } } /** * 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 $this->raiseError("Client does not exist."); } return $this->clientInfo[$clientId]; } /** * close connection to a client * * @access public * @param int $clientID internal ID of the client */ function closeConnection($id = 0) { if (!isset($this->clientFD[$id])) { return $this->raiseError( "Connection already has been closed." ); } if (method_exists($this->callbackObj, "onClose")) { $this->callbackObj->onClose($id); } $this->_sendDebugMessage("Closed connection (".$id.") from ".$this->clientInfo[$id]["host"]." on port ".$this->clientInfo[$id]["port"]); @socket_close($this->clientFD[$id]); $this->clientFD[$id] = NULL; unset($this->clientInfo[$id]); $this->clients--; } /** * shutdown server * * @access public */ function shutDown() { if ($GLOBALS["_Net_Server_Status"] != "running") { exit(); } $GLOBALS["_Net_Server_Status"] = "stopped"; if (method_exists($this->callbackObj, "onShutdown")) { $this->callbackObj->onShutdown(); } $maxFD = count($this->clientFD); for($i = 0; $i < $maxFD; $i++) { $this->closeConnection($i); } @socket_close($this->initFD); $this->_sendDebugMessage("Shutdown server."); exit(); } } ?>