Ejemplo n.º 1
0
 public function childServer($addr)
 {
     $output = $this->output;
     $bindTo = json_decode($addr, true);
     $this->childHost = $bindTo['host'];
     $this->port = $bindTo['port'];
     cli_set_process_title("mpcmf/console server:run/child -b {$this->childHost} -p {$this->port}");
     posix_setgid(99);
     posix_setuid(99);
     posix_seteuid(99);
     posix_setegid(99);
     $loop = Factory::create();
     $socket = new reactSocketServer($loop);
     $http = new reactHttpServer($socket);
     $http->on('request', function (reactRequest $request, reactResponse $response) use($output) {
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> New connection");
         //MPCMF_DEBUG && $clientName = $request->getRemoteAddress() . '#' . spl_object_hash($request);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Client connected");
         profiler::resetStack();
         if (!$this->prepare($request, $response, $output)) {
             return;
         }
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Starting application</info>");
         try {
             $app = $this->app();
             $slim = $app->slim();
             $originApplication = $this->applicationInstance->getCurrentApplication();
             $this->applicationInstance->setApplication($app);
             $slim->call();
         } catch (\Exception $e) {
             $response->writeHead(500);
             $response->end("Exception: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n{$e->getTraceAsString()}");
             return;
         }
         /** @var int[]|Headers[]|string[] $content */
         $content = $slim->response->finalize();
         Util::serializeCookies($content[1], $slim->response->cookies, $slim->settings);
         $content[1] = $content[1]->all();
         $this->applicationInstance->setApplication($originApplication);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Ending application</info>");
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Writing data and closing connection");
         static $serverSoftware;
         if ($serverSoftware === null) {
             $serverSoftware = 'MPCMF Async PHP ' . phpversion();
         }
         if (array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER) && strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false) {
             $content[1]['Content-Encoding'] = 'gzip';
             $content[2] = gzencode($content[2], 9);
         }
         $content[1]['X-PHP-Server'] = $serverSoftware;
         $content[1]['X-PHP-Server-Addr'] = "{$this->childHost}:{$this->port}";
         $response->writeHead($content[0], $content[1]);
         $response->end($content[2]);
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Connection closed");
     });
     $output->writeln("<error>[CHILD]</error> Starting child server on {$this->childHost}:{$this->port}");
     $socket->listen($this->port, $this->childHost);
     $loop->run();
 }