/**
  * Process events on the master socket ($this->socket)
  *
  * @return void
  */
 protected function processMasterSocket()
 {
     $new = null;
     try {
         $new = $this->socket->accept();
     } catch (Exception $e) {
         $this->server->log('Socket error: ' . $e, 'err');
         return;
     }
     $connection = $this->createConnection($new);
     $this->server->notify(Server::EVENT_SOCKET_CONNECT, array($new, $connection));
 }
Example #2
0
 /**
  * This is the main server loop.  This code is responsible for adding connections and triggering hooks.
  *
  * @throws \Navarr\Socket\Exception\SocketException
  *
  * @return bool Whether or not to shutdown the server
  */
 protected function loopOnce()
 {
     // Get all the Sockets we should be reading from
     $read = array_merge([$this->masterSocket], $this->clients);
     // Set up a block call to socket_select
     $write = null;
     $except = null;
     $ret = Socket::select($read, $write, $except, $this->timeout);
     if (!is_null($this->timeout) && $ret == 0) {
         if ($this->triggerHooks(self::HOOK_TIMEOUT, $this->masterSocket) === false) {
             // This only happens when a hook tells the server to shut itself down.
             return false;
         }
     }
     // If there is a new connection, add it
     if (in_array($this->masterSocket, $read)) {
         unset($read[array_search($this->masterSocket, $read)]);
         $socket = $this->masterSocket->accept();
         $this->clients[] = $socket;
         if ($this->triggerHooks(self::HOOK_CONNECT, $socket) === false) {
             // This only happens when a hook tells the server to shut itself down.
             return false;
         }
         unset($socket);
     }
     // Check for input from each client
     foreach ($read as $client) {
         $input = $this->read($client);
         if ($input === '') {
             if ($this->disconnect($client) === false) {
                 // This only happens when a hook tells the server to shut itself down.
                 return false;
             }
         } else {
             if ($this->triggerHooks(self::HOOK_INPUT, $client, $input) === false) {
                 // This only happens when a hook tells the server to shut itself down.
                 return false;
             }
         }
         unset($input);
     }
     // Unset the variables we were holding on to
     unset($read);
     unset($write);
     unset($except);
     // Tells self::run to Continue the Loop
     return true;
 }