diff --git a/composer.json b/composer.json index 43f69a3..fda9c54 100755 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "keywords": ["module", "xp"], "require" : { "xp-framework/core": "^12.0 | ^11.0 | ^10.0", - "xp-framework/networking": "^10.1", + "xp-framework/networking": "^11.0", "xp-forge/uri": "^3.1", "xp-forge/websockets": "^4.1", "php": ">=7.4.0" diff --git a/src/main/php/web/Application.class.php b/src/main/php/web/Application.class.php index 7aabce4..aa511c8 100755 --- a/src/main/php/web/Application.class.php +++ b/src/main/php/web/Application.class.php @@ -42,9 +42,10 @@ public final function routing() { * Initializes this application, being run once when the server starts. * Empty in this default implementation, overwrite in subclasses. * + * @param peer.server.ServerImplementation $impl * @return void */ - public function initialize() { + public function initialize($impl) { // Empty } diff --git a/src/main/php/xp/web/Servers.class.php b/src/main/php/xp/web/Servers.class.php index 226ea88..197be8e 100755 --- a/src/main/php/xp/web/Servers.class.php +++ b/src/main/php/xp/web/Servers.class.php @@ -1,7 +1,7 @@ application($args); - $application->initialize(); + $application->initialize($impl); // PHP doesn't start with a nonexistant document root if (!$docroot->exists()) { @@ -72,31 +73,13 @@ public function serve($source, $profile, $webroot, $docroot, $config, $args, $lo ); // Start the multiplex protocol in the foreground and forward requests - $impl= new AsyncServer(); $impl->listen(new ServerSocket($this->host, $this->port), Protocol::multiplex() ->serving('http', new ForwardRequests($backends)) ->serving('websocket', new WebSocketProtocol(new ForwardMessages($backends))) ); - // Inside `xp -supervise`, connect to signalling socket. Unfortunately, there - // is no way to signal "no timeout", so set a pretty high timeout of one year, - // then catch and handle it by continuing to check for reads. - if ($port= getenv('XP_SIGNAL')) { - $signal= new Socket('127.0.0.1', $port); - $signal->setTimeout(31536000); - $signal->connect(); - $impl->select($signal, function() use($impl) { - try { - next: yield 'read' => null; - } catch (SocketTimeoutException $e) { - goto next; - } - $impl->shutdown(); - }); - } - + $this->connect(getenv('XP_SIGNAL'), $impl); try { - $impl->init(); $impl->service(); } finally { Console::write('['); diff --git a/src/main/php/xp/web/srv/Server.class.php b/src/main/php/xp/web/srv/Server.class.php index 20e3a88..e7b7658 100755 --- a/src/main/php/xp/web/srv/Server.class.php +++ b/src/main/php/xp/web/srv/Server.class.php @@ -1,5 +1,7 @@ setTimeout(31536000); + $s->connect(); + $impl->select($s, function() use($impl) { + try { + next: yield 'read' => null; + } catch (SocketTimeoutException $e) { + goto next; + } + $impl->shutdown(); + }); + } + /** @return string */ public function host() { return $this->host; } diff --git a/src/main/php/xp/web/srv/Standalone.class.php b/src/main/php/xp/web/srv/Standalone.class.php index b994cb2..b014cb9 100755 --- a/src/main/php/xp/web/srv/Standalone.class.php +++ b/src/main/php/xp/web/srv/Standalone.class.php @@ -33,7 +33,7 @@ public function __construct($address, $impl) { public function serve($source, $profile, $webroot, $docroot, $config, $args, $logging) { $environment= new Environment($profile, $webroot, $docroot, $config, $args, $logging); $application= (new Source($source, $environment))->application($args); - $application->initialize(); + $application->initialize($this->impl); $application->routing(); $socket= new ServerSocket($this->host, $this->port); @@ -41,6 +41,7 @@ public function serve($source, $profile, $webroot, $docroot, $config, $args, $lo ->serving('http', new HttpProtocol($application, $environment->logging())) ->serving('websocket', new WebSocketProtocol(null, $environment->logging())) ); + // DEBUG $this->impl->setTrace(\util\log\Logging::all()->toConsole()); Console::writeLine("\e[33m@", nameof($this), '(HTTP @ ', $socket->toString(), ")\e[0m"); Console::writeLine("\e[1mServing {$profile}:", $application, $config, "\e[0m > ", $environment->logging()->target()); @@ -56,6 +57,11 @@ public function serve($source, $profile, $webroot, $docroot, $config, $args, $lo getmypid() ); - $this->impl->service(); + $this->connect(getenv('XP_SIGNAL'), $this->impl); + try { + $this->impl->service(); + } finally { + Console::writeLine('[.]'); + } } } \ No newline at end of file