Пример #1
0
 /**
  * Add a listener to the socket server.
  *
  * @code
  * addListener("tcp://0.0.0.0:6060", '\MyHttpTransport'); // HTTP
  * addListener("ssl://0.0.0.0:6061", '\MyHttpTransport', $cert); // HTTPS
  * addListener("unix:///var/run/myhttpd-control", '\MyControlTransport'); // Unix Domain Socket
  * @endcode
  *
  * @param mixed $endpoint The endpoint (eg. tcp://0.0.0.0:6667)
  * @param ISocketTransport $transport The transport to use
  *
  * @x-new
  * @x-replaces addListener
  * @x-replaces addListenPort
  */
 public function addListener($endpoint, SocketTransport $transport, Certificate $cert = null, array $options = null)
 {
     // Initialize a null context
     $ctx = null;
     // If we got a certificate, let's set up an SSL context and then listen.
     if ($cert) {
         $ctx = $cert->getStreamContext();
         $server = \stream_socket_server($endpoint, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctx);
     } else {
         // Add the listener, and start listening.
         $server = \stream_socket_server($endpoint, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
     }
     if (!$server) {
         throw new \Exception("Server setup failed: {$errstr} ({$errno})");
     }
     $this->debug("Created new listener for {$endpoint} (on %s)", get_class($transport));
     // Save our instances so we can use them for select as well as to look
     // up the status.
     $this->listeners[$endpoint] = $server;
     $this->endpoints[$endpoint] = (object) ["socket" => $server, "endpoint" => $endpoint, "transport" => $transport, "clients" => 0, "options" => (array) $options];
 }