/**
  * 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
 /**
  * Receives data from the server.
  * 
  * Receives data from the server as a stream.
  * 
  * @param int              $length  The number of bytes to receive.
  * @param FilterCollection $filters A collection of filters to apply to the
  *     stream while receiving. Note that the filters will not be present on
  *     the stream after receiving is done.
  * @param string           $what    Descriptive string about what is being
  *     received (used in exception messages).
  * 
  * @return resource The received content.
  */
 public function receiveStream($length, FilterCollection $filters = null, $what = 'stream data')
 {
     if (false === ($previousState = $this->lock(self::DIRECTION_RECEIVE)) && $this->persist) {
         throw $this->createException('Unable to obtain receiving lock', 9);
     }
     try {
         $result = parent::receiveStream($length, $filters, $what);
     } catch (E $e) {
         $this->lock($previousState, true);
         throw $e;
     }
     $this->lock($previousState, true);
     return $result;
 }