getTimeout(), $this->getFlag() ); } else { $connection = @stream_socket_client( $streamName, $errno, $errstr, $this->getTimeout(), $this->getFlag(), $context->getContext() ); } if (false === $connection) { if ($errno === 0) { throw new Exception('Client cannot join %s.', 0, $streamName); } else { throw new Exception( 'Client returns an error (number %d): %s while trying ' . 'to join %s.', 1, [$errno, $errstr, $streamName] ); } } $this->_stack[] = $connection; $id = $this->getNodeId($connection); $this->_node = Consistency\Autoloader::dnew( $this->getNodeName(), [$id, $connection, $this] ); $this->_nodes[$id] = $this->_node; return $connection; } /** * Close the current stream. * * @return bool */ protected function _close() { if (true === $this->isPersistent()) { return false; } return @fclose($this->getStream()); } /** * Select connections. * * @return \Hoa\Socket\Client */ public function select() { $read = $this->getStack(); $write = null; $except = null; @stream_select($read, $write, $except, $this->getTimeout(), 0); foreach ($read as $socket) { $this->_iterator[] = $socket; } return $this; } /** * Consider another client when selecting connection. * * @param \Hoa\Socket\Connection $other Other client. * @return \Hoa\Socket\Client * @throws \Hoa\Socket\Exception */ public function consider(Connection $other) { if (!($other instanceof self)) { throw new Exception( 'Other client must be of type %s.', 2, __CLASS__ ); } if (true === $other->isDisconnected()) { $other->connect(); } $otherNode = $other->getCurrentNode(); $this->_stack[] = $otherNode->getSocket(); $this->_nodes[$otherNode->getId()] = $otherNode; return $this; } /** * Check if the current node belongs to a specific server. * * @param \Hoa\Socket\Connection $server Server. * @return bool */ public function is(Connection $server) { return $this->getStream() === $server->getStream(); } /** * Set and get the current selected connection. * * @return \Hoa\Socket\Node */ public function current() { $current = parent::_current(); return $this->_node = $this->_nodes[$this->getNodeId($current)]; } /** * Check if the connection is connected or not. * * @return bool */ public function isConnected() { return (bool) ($this->getFlag() & self::CONNECT); } /** * Check if the connection is asynchronous or not. * * @return bool */ public function isAsynchronous() { return (bool) ($this->getFlag() & self::ASYNCHRONOUS); } /** * Check if the connection is persistent or not. * * @return bool */ public function isPersistent() { return (bool) ($this->getFlag() & self::PERSISTENT); } /** * Return internal node stack. * * @return array */ protected function getStack() { return $this->_stack; } }