Esempio n. 1
0
 /**
  * Constructor
  *
  * @access public
  * @param  resource $socket
  * @param  IOLoop   $io_loop
  * @return void
  */
 public function __construct($socket, IOLoop $io_loop)
 {
     stream_set_blocking($socket, false);
     $this->_socket = $socket;
     $this->_loop = $io_loop;
     $this->_state = IOLoop::ERROR;
     $this->_loop->add_handler($this->_socket, array($this, 'handle_events'), $this->_state);
 }
Esempio n. 2
0
 /**
  * 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.');
     }
 }