1<?PHP 2/* vim: set expandtab tabstop=4 shiftwidth=4: */ 3// +----------------------------------------------------------------------+ 4// | PHP Version 4 | 5// +----------------------------------------------------------------------+ 6// | Copyright (c) 1997-2002 The PHP Group | 7// +----------------------------------------------------------------------+ 8// | This source file is subject to version 2.0 of the PHP license, | 9// | that is bundled with this package in the file LICENSE, and is | 10// | available at through the world-wide-web at | 11// | http://www.php.net/license/2_02.txt. | 12// | If you did not receive a copy of the PHP license and are unable to | 13// | obtain it through the world-wide-web, please send a note to | 14// | license@php.net so we can mail you a copy immediately. | 15// +----------------------------------------------------------------------+ 16// | Authors: Stephan Schmidt <schst@php.net> | 17// +----------------------------------------------------------------------+ 18// 19// $Id$ 20 21/** 22 * PHP socket server base class 23 * 24 * @category Networking 25 * @package Net_Server 26 * @version 0.11 alpha 27 * @author Stephan Schmidt <schst@php.net> 28 * @license PHP License 29 */ 30 31/** 32 * uses PEAR's error handling and the destructors 33 */ 34require_once 'PEAR.php'; 35 36/** 37 * driver file does not exist 38 */ 39define('NET_SERVER_ERROR_UNKNOWN_DRIVER', 51); 40 41/** 42 * driver file does not contain class 43 */ 44define('NET_SERVER_ERROR_DRIVER_CORRUPT', 52); 45 46/** 47 * feature is not supported 48 */ 49define('NET_SERVER_ERROR_NOT_SUPPORTED', 53); 50 51/** 52 * needs PCNTL extension 53 */ 54define('NET_SERVER_ERROR_PCNTL_REQUIRED', 54); 55 56/** 57 * PHP socket server base class 58 * 59 * This class must only be used to create a new server using 60 * the static method 'create()'. 61 * 62 * To handle the events that happen while the server is running 63 * you have to create a new class that handles all events. 64 * 65 * <code> 66 * require_once 'myHandler.php'; 67 * require_once 'Net/Server.php'; 68 * 69 * $server = &Net_Server::create('fork', 'localhost', 9090); 70 * 71 * $handler = &new myHandler; 72 * 73 * $server->setCallbackObject($handler); 74 * 75 * $server->start(); 76 * </code> 77 * 78 * See Server/Handler.php for a baseclass that you can 79 * use to implement new handlers. 80 * 81 * @category Networking 82 * @package Net_Server 83 * @version 0.11 alpha 84 * @author Stephan Schmidt <schst@php.net> 85 */ 86class Net_Server { 87 88 /** 89 * Create a new server 90 * 91 * Currently two types of servers are supported: 92 * - 'sequential', creates a server where one process handles all request from all clients sequentially 93 * - 'fork', creates a server where a new process is forked for each client that connects to the server. This only works on *NIX 94 * 95 * This method will either return a server object or a PEAR error if the server 96 * type does not exist. 97 * 98 * @access public 99 * @static 100 * @param string $type type of the server 101 * @param string $host hostname 102 * @param integer $port port 103 * @return object Net_Server_Driver server object of the desired type 104 * @throws object PEAR_Error 105 */ 106 function &create($type, $host, $port) 107 { 108 if (!function_exists('socket_create')) { 109 return PEAR::raiseError('Sockets extension not available.'); 110 } 111 112 $type = ucfirst(strtolower($type)); 113 $driverFile = 'Net/Server/Driver/' . $type . '.php'; 114 $className = 'Net_Server_Driver_' . $type; 115 116 if (!include_once $driverFile) { 117 return PEAR::raiseError('Unknown server type', NET_SERVER_ERROR_UNKNOWN_DRIVER); 118 } 119 120 if (!class_exists($className)) { 121 return PEAR::raiseError('Driver file is corrupt.', NET_SERVER_ERROR_DRIVER_CORRUPT); 122 } 123 124 $server = &new $className($host, $port); 125 return $server; 126 } 127} 128?>