public function __construct() { $this->param = ChildProcess::current()->getWorkerParam(); $this->dispatcher = $this->param['dispatcher']; $this->logic = new $this->param['dispatchLogic']($this->dispatcher); if (!$this->logic instanceof DispatchLogicInterface) { throw new \RuntimeException(sprintf('%s must instanceof EPS\\ServerDispatcher\\DispatcherInterface', $this->param['dispatchLogic']), 1); } $this->accept = $this->param['acceptMessage']; $this->receive = $this->param['receiveMessage']; //添加连接处理状态 EventLoop::addUsTimer([$this, 'onAccept'], 100); EventLoop::addUsTimer([$this, 'onReceive'], 10); }
protected function fork() { $pid = pcntl_fork(); if ($pid == -1) { $this->emit('error', sprintf('%s fork fail!', $this->getWorker())); } elseif ($pid === 0) { $this->init(); if (!$this->isMain) { //添加父进程退出检测(自动退出子进程) $process = $this; EventLoop::addScTimer(function () use($process) { if ($process->ppid !== posix_getppid()) { exit(0); } }, 1); } $this->runWorker(); } else { $this->pid = $pid; $this->ppid = posix_getpid(); if ($this->isMain && $this->isDaemon) { exit(0); } if ($this->isReboot()) { $process = $this; EventLoop::addScTimer(function () use($process) { //检查子进程 $status = null; while (($pid = pcntl_waitpid(-1, $status, WNOHANG)) > 0) { $process->restart($pid); } }, 1); } $this->isMain and EventLoop::run(); } return $pid; }