Пример #1
0
 public function __construct(Server $server, EventDispatcher $event, $socket, WorkerConfig $config = null)
 {
     if (-1 === ($this->pid = pcntl_fork())) {
         throw new Exception('Failed to create a work process');
     } elseif ($this->pid) {
         return;
     }
     try {
         $server->setType(Server::WORKER);
         $event->reset();
         $this->server = $server;
         $this->event = $event;
         $this->socket = $socket;
         $this->timestamp = $server->getTimestamp();
         $this->timestamp->mark(ServerTimestampType::WorkerStart);
         $this->config = $config ?? new WorkerConfig();
         Posix::setUser($this->config->worker_user, $this->config->worker_user_group);
         $pipeline = $this->server->getPipeline();
         $pipeline->bind('worker', $this);
         if ($pipeline instanceof EventManageable && !$pipeline->getEvent()) {
             $pipeline->setEvent($event);
         }
         $this->createClient();
     } catch (\Throwable $ex) {
         echo sprintf('"%s" in "%s:%d"', $ex->getMessage(), $ex->getFile(), $ex->getLine()), "\n";
         echo $ex->getTraceAsString();
         if (!$this->event->getBase()->gotStop()) {
             $this->event->stop();
         }
     }
 }
Пример #2
0
 public function onAccept($socket)
 {
     if (!($client = socket_accept($socket))) {
         throw new Exception("Accept a socket connect failed");
     }
     socket_set_nonblock($client);
     $worker = $this->bind->createWorker($client);
     if (Server::MASTER === $this->bind->getType()) {
         $this->bind->getWorkManage()->add($worker);
         socket_close($client);
     }
 }