Пример #1
0
 /**
  * @param string        $socketAddress
  * @param resource|null $socketContext
  *
  * @throws Exception\BindOrListenException
  *
  * @return resource
  */
 public static function createServerSocket($socketAddress, $socketContext = null)
 {
     try {
         return self::doCreateServerSocket($socketAddress, $socketContext);
     } catch (Exception\BindOrListenException $e) {
         if (($socketFile = IdentificationHelper::getSocketFile($socketAddress)) !== null) {
             try {
                 fclose(self::createClientSocket($socketAddress, 1, $socketContext));
                 // Really in use
                 throw $e;
             } catch (Exception\ConnectException $e2) {
                 // False positive due to a residual socket file
                 unlink($socketFile);
                 return self::doCreateServerSocket($socketAddress, $socketContext);
             }
         } else {
             throw $e;
         }
     }
 }
Пример #2
0
 /**
  * @param string $socketAddress
  *
  * @throws Exception\BindOrListenException
  * @throws Exception\RuntimeException
  *
  * @return resource
  */
 private static function startListening($socketAddress)
 {
     $lock = Lock::acquire();
     if (self::$killSwitchPath !== null) {
         $kswitch = new KillSwitch(self::$killSwitchPath);
         if ($kswitch->getGlobal() || $kswitch->hasAddress($socketAddress)) {
             throw new Exception\RuntimeException('This worker has been prevented from starting using the kill switch');
         }
     }
     $socketFile = IdentificationHelper::getSocketFile($socketAddress);
     if ($socketFile !== null) {
         $socketDir = dirname($socketFile);
         if (!is_dir($socketDir)) {
             mkdir($socketDir, 0777, true);
         }
     }
     $server = SocketFactory::createServerSocket($socketAddress, self::$socketContext);
     $lock->release();
     self::$socket = $server;
     self::$listening = true;
     self::$toDelete = $socketFile;
     return $server;
 }