Example #1
0
 function server()
 {
     // Note how we pass our ServerSocketClass here
     $server = new SocketServer("tcp://127.0.0.1:8081", "\\ServerSocketClass");
     echo "Server running. Now go ahead and connect to 127.0.0.1:8081 with netcat (nc 127.0.0.1 8081)\n";
     $running = true;
     while ($running) {
         // Go over the sockets that are ready to read
         foreach ($server->select() as $sock) {
             $str = $sock->read(1024);
             if ($str == "") {
                 // Call close on the server rather than disconnect to free
                 // it from the pool.
                 $server->close($sock);
             } else {
                 $sock->onData($str);
             }
         }
         // Do this for each loop
         $server->each(function ($client) {
             $client->onTick();
         });
     }
 }
Example #2
0
 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;
         }
     }
 }