Beispiel #1
0
 public function init($socket, Closure $closure)
 {
     $server = new ServerSocket($socket);
     $server->connectAndWait();
     while (true) {
         foreach ($server->select() as $node) {
             $clouse->__invoke($server);
         }
     }
 }
Beispiel #2
0
 /**
  * Run the shared worker.
  * It creates a zombie with \Hoa\Zombie.
  *
  * @return  void
  * @throws  \Hoa\Worker\Backend\Exception
  */
 public function run()
 {
     $server = new Socket\Server($this->_socket);
     $server->connectAndWait();
     Zombie::fork();
     $_eom = pack('C', 0);
     while (true) {
         foreach ($server->select() as $node) {
             $request = unpack('nr', $server->read(2));
             $length = unpack('Nl', $server->read(4));
             $message = unserialize($server->read($length['l']));
             $eom = unpack('Ce', $server->read(1));
             if ($eom['e'] != $_eom) {
                 $server->disconnect();
                 continue;
             }
             switch ($request['r']) {
                 case static::TYPE_MESSAGE:
                     $this->getListener()->fire('message', new Event\Bucket(['message' => $message]));
                     ++$this->_messages;
                     $this->_lastMessage = time();
                     break;
                 case static::TYPE_STOP:
                     if ($this->_password === $message) {
                         $server->disconnect();
                         break 3;
                     }
                     break;
                 case static::TYPE_INFORMATION:
                     $message = ['id' => $this->_wid, 'socket' => $this->_socket, 'start' => $this->_startTime, 'pid' => getmypid(), 'memory' => memory_get_usage(true), 'memory_allocated' => memory_get_usage(), 'memory_peak' => memory_get_peak_usage(true), 'memory_allocated_peak' => memory_get_usage(), 'messages' => $this->_messages, 'last_message' => $this->_lastMessage, 'filename' => $_SERVER['SCRIPT_FILENAME']];
                     $server->writeAll(static::pack(static::TYPE_MESSAGE, $message));
                     break;
             }
             $server->disconnect();
         }
     }
     $server->disconnect();
     if (null !== $this->_wid) {
         Worker\Run::unregister($this->_wid);
     }
     return;
 }