/** * Creates a singleton instance (if needed) and returns it. * * @access public * @return void */ public static function singleton() { if (empty(self::$_instance)) { self::$_instance = new IOLoop(); } return self::$_instance; }
/** * Adds a state to watch for to the stream. * * @access private * @return void */ private function _add_io_state($state) { if (!($state & $this->_state)) { $this->_state |= $state; $this->_loop->update_handler($this->_socket, $this->_state); } }
/** * Sets up the socket and listens for new requests. * * @access public * @return void */ public function listen() { // Make sure we have the values we need for connecting. if (empty($this->_config['SERVER_PORT']) || !is_array($this->_config['SERVER_PORT'])) { throw new RuntimeException('Ports must be defined in the configuration before running the server.'); } if (empty($this->_config['SERVER_ADDRESS'])) { throw new RuntimeException('The server address must be defined in the configuration before running the server.'); } // Create new sockets for all the ports we are listening on. foreach ($this->_config['SERVER_PORT'] as $port) { $tcp = stream_socket_server('tcp://' . $this->_config['SERVER_ADDRESS'] . ':' . $port); if ($tcp === false) { // Could not open on the port or IP address. continue; } // Tell the loop what to do with new requests. $this->_loop->add_handler($tcp, array($this, 'handle_events'), IOLoop::READ); $this->_sockets[] = $tcp; } if (!empty($this->_config['SSL_SERVER_PORT']) && is_array($this->_config['SSL_SERVER_PORT'])) { $context = stream_context_create(); stream_context_set_option($context, 'ssl', 'local_cert', $this->_config['SSL_CERT_PATH']); stream_context_set_option($context, 'ssl', 'allow_self_signed', true); stream_context_set_option($context, 'ssl', 'verify_peer', false); foreach ($this->_config['SSL_SERVER_PORT'] as $port) { $ssl = stream_socket_server('ssl://' . $this->_config['SERVER_ADDRESS'] . ':' . $port, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); // Tell the loop what to do with new requests. $this->_loop->add_handler($ssl, array($this, 'handle_events'), IOLoop::READ); $this->_sockets[] = $ssl; } } if (!count($this->_sockets)) { throw new RuntimeException('The server was unable to open any listening connections.'); } }
* @link http://www.spoutserver.com/ */ ini_set('display_errors', true); error_reporting(E_ALL); ini_set('memory_limit', '512M'); // Define some paths. if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } define('_BASE', __DIR__); define('_CONFIG', __DIR__ . DIRECTORY_SEPARATOR . 'config'); define('_LIBS', __DIR__ . DIRECTORY_SEPARATOR . 'libs'); // Include compatibility fixes. require_once _LIBS . DIRECTORY_SEPARATOR . 'compat.php'; // Load configuration file. $config = (require _CONFIG . DIRECTORY_SEPARATOR . 'config.php'); // Check for verbosity level from the command line. if (!empty($argc) && $argc >= 3) { if ($argv[1] == '-v' && is_numeric($argv[2])) { $config['server']['VERBOSE'] = $argv[2]; $config['dispatcher']['VERBOSE'] = $argv[2]; } } require_once _LIBS . DIRECTORY_SEPARATOR . 'dispatcher.php'; $loop = IOLoop::singleton(); $dispatcher = new Dispatcher($config['dispatcher']); $server = new HTTPServer(array($dispatcher, 'dispatch'), $loop, $config['server']); $dispatcher->set_server($server); $server->listen(); $loop->start(); $server->close();
/** * Start listening for new connections * * @param string $address * @param array $context * @return this */ public function listen($address, array $context = array()) { $address = is_numeric($address) ? "tcp://0.0.0.0:{$address}" : $address; $context = stream_context_create($context); $this->stream = @stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); $server = $this; if (!is_resource($this->stream) || $errstr) { $this->emit('error', array($errstr, $this)); return $this; } $this->emit('listening', array($this)); $this->iloop->add($this->stream, IOLoop::READ, function ($master, $ioloop) use($server) { $client = stream_socket_accept($master, 0); $client = new IOStream($ioloop, $client, IOStream::DUPLEX); $client->reader = 'fgets'; $client->writer = 'stream_socket_sendto'; $client->on('close', function ($client) { stream_socket_shutdown($client->stream, STREAM_SHUT_RDWR); }); $server->emit('connection', array($client)); }); return $this; }