/**
  * Creates a new connection with the specified options.
  * 
  * @param resource   $server  A socket server, created with
  *     {@link stream_socket_server()}.
  * @param float|null $timeout The timeout for the connection. Leaving this
  *     to NULL uses the default socket timeout.
  */
 public function __construct($server, $timeout = null)
 {
     $this->streamType = '_SERVER';
     if (!self::isStream($server)) {
         throw $this->createException('Invalid server supplied.', 9);
     }
     $timeout = null == $timeout ? ini_get('default_socket_timeout') : $timeout;
     set_error_handler(array($this, 'handleError'));
     try {
         parent::__construct(stream_socket_accept($server, $timeout, $peerName));
         restore_error_handler();
         $portString = strrchr($peerName, ':');
         $this->peerPort = (int) substr($portString, 1);
         $ipString = substr($peerName, 0, strlen($peerName) - strlen($portString));
         if (strpos($ipString, '[') === 0 && strpos(strrev($ipString), ']') === 0) {
             $ipString = substr($ipString, 1, strlen($ipString) - 2);
         }
         $this->peerIP = $ipString;
     } catch (E $e) {
         restore_error_handler();
         throw $this->createException('Failed to initialize connection.', 10, $e);
     }
 }
示例#2
0
 /**
  * Creates a new connection with the specified options.
  * 
  * @param string   $host    Hostname (IP or domain) of the server.
  * @param int      $port    The port on the server.
  * @param bool     $persist Whether or not the connection should be a
  *     persistent one.
  * @param float    $timeout The timeout for the connection.
  * @param string   $key     A string that uniquely identifies the
  *     connection.
  * @param string   $crypto  Encryption setting. Must be one of the
  *     NetworkStream::CRYPTO_* constants. By default, encryption is
  *     disabled. If the setting has an associated scheme for it, it will be
  *     used, and if not, the setting will be adjusted right after the
  *     connection is estabilished.
  * @param resource $context A context for the socket.
  */
 public function __construct($host, $port, $persist = false, $timeout = null, $key = '', $crypto = parent::CRYPTO_OFF, $context = null)
 {
     $this->streamType = '_CLIENT';
     if (strpos($host, ':') !== false) {
         $host = "[{$host}]";
     }
     $flags = STREAM_CLIENT_CONNECT;
     if ($persist) {
         $flags |= STREAM_CLIENT_PERSISTENT;
     }
     $timeout = null == $timeout ? ini_get('default_socket_timeout') : $timeout;
     $key = rawurlencode($key);
     if (null === $context) {
         $context = stream_context_get_default();
     } elseif (!is_resource($context) || 'stream-context' !== get_resource_type($context)) {
         throw $this->createException('Invalid context supplied.', 6);
     }
     $hasCryptoScheme = array_key_exists($crypto, static::$cryptoScheme);
     $scheme = $hasCryptoScheme ? static::$cryptoScheme[$crypto] : 'tcp';
     $this->uri = "{$scheme}://{$host}:{$port}/{$key}";
     set_error_handler(array($this, 'handleError'));
     try {
         parent::__construct(stream_socket_client($this->uri, $this->errorNo, $this->errorStr, $timeout, $flags, $context));
         restore_error_handler();
     } catch (E $e) {
         restore_error_handler();
         if (0 === $this->errorNo) {
             throw $this->createException('Failed to initialize socket.', 7, $e);
         }
         throw $this->createException('Failed to connect with socket.', 8, $e);
     }
     if ($hasCryptoScheme) {
         $this->crypto = $crypto;
     } elseif (parent::CRYPTO_OFF !== $crypto) {
         $this->setCrypto($crypto);
     }
     $this->setIsBlocking(parent::CRYPTO_OFF === $crypto);
     if ($persist) {
         $this->shmHandler = SHM::factory(__CLASS__ . ' ' . $this->uri . ' ');
         self::$lockState[$this->uri] = self::DIRECTION_NONE;
     }
 }