/** * 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]; }
function servicemain() { // Set up the httpd. Will be cloned for each new instance. $http = new \Higgs\HttpServer(); /* $http->addExtension(new \Higgs\Extensions\Misc\AddHeader([ "header" => "x-foo", "value" => "Hello World" ])); */ $cfg = ObjMan::getObject("local:/config/higgs"); $exts = $cfg->query("/httpd/server[default]/extension"); foreach ($exts as $ext) { $cn = \Utils::getClassFromDotted($ext[0]); class_exists($cn); } foreach ($exts as $ext) { $cn = \Utils::getClassFromDotted($ext[0]); if (class_exists($cn)) { $ext = new $cn($ext->getAttributes()); $http->addExtension($ext); } else { $this->warn("Could not load extension '{$ext[0]}'"); } } //$ctrl = new \Higgs\HttpControl(); $server = new SocketServer(); $ports = $cfg->query("/httpd/server[default]/listen"); foreach ($ports as $ep) { $endpoint = $ep[0]; if ($ep->hasAttribute("certificate")) { $cert = new Certificate("server.pem"); $this->debug("Using certificate %s", "server.pem"); $info = $cert->getCertificateInfo(); list($vfrom, $vto) = $cert->getValidity(); $this->debug(" Issued to: %s", $info["name"]); $this->debug(" Issuer: %s (%s)", $info["issuer"]["O"], $info["issuer"]["OU"]); $this->debug(" Hash: 0x%s", $info["hash"]); $this->debug(" Valid from: %s", $vfrom); $this->debug(" Valid until: %s", $vto); if ($cert->isSelfSigned()) { $this->warn("Warning! The certificate in use is self-signed. Consider getting a proper certificate for production use."); $this->warn("HSTS by design does not allow self-signed certificates. Enabling HSTS will not work."); } $server->addListener($endpoint, $http, $cert); } else { $server->addListener($endpoint, $http); } } /* $server->addListener("tcp://127.0.0.1:9700", $http); $server->addListener("ssl://127.0.0.1:9701", $http, $cert); $server->addListener("tcp://127.0.0.1:9799", $http); */ while ($server->process()) { usleep(5000); if ($this->stop) { break; } } }