Hoa central
Client.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Socket;
38 
39 use Hoa\Stream;
40 
49 class Client extends Connection
50 {
56  const ASYNCHRONOUS = STREAM_CLIENT_ASYNC_CONNECT;
57 
63  const CONNECT = STREAM_CLIENT_CONNECT;
64 
70  const PERSISTENT = STREAM_CLIENT_PERSISTENT;
71 
77  const ENCRYPTION_SSLv2 = STREAM_CRYPTO_METHOD_SSLv2_CLIENT;
78 
84  const ENCRYPTION_SSLv3 = STREAM_CRYPTO_METHOD_SSLv3_CLIENT;
85 
91  const ENCRYPTION_SSLv23 = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
92 
98  const ENCRYPTION_TLS = STREAM_CRYPTO_METHOD_TLS_CLIENT;
99 
105  protected $_stack = [];
106 
107 
108 
119  public function __construct(
120  $socket,
121  $timeout = 30,
122  $flag = self::CONNECT,
123  $context = null
124  ) {
125  parent::__construct($socket, $timeout, self::CONNECT & $flag, $context);
126 
127  return;
128  }
129 
138  protected function &_open($streamName, Stream\Context $context = null)
139  {
140  if (null === $context) {
141  $connection = @stream_socket_client(
142  $streamName,
143  $errno,
144  $errstr,
145  $this->getTimeout()/*,
146  $this->getFlag()*/
147  );
148  } else {
149  $connection = @stream_socket_client(
150  $streamName,
151  $errno,
152  $errstr,
153  $this->getTimeout()/*,
154  $this->getFlag(),
155  $context->getContext()*/
156  );
157  }
158 
159  if (false === $connection) {
160  if ($errno == 0) {
161  throw new Exception('Client cannot join %s.', 0, $streamName);
162  } else {
163  throw new Exception(
164  'Client returns an error (number %d): %s while trying ' .
165  'to join %s.',
166  1,
167  [$errno, $errstr, $streamName]
168  );
169  }
170  }
171 
172  $this->_stack[] = $connection;
173  $id = $this->getNodeId($connection);
174  $this->_node = dnew($this->getNodeName(), [$id, $connection, $this]);
175  $this->_nodes[$id] = $this->_node;
176 
177  return $connection;
178  }
179 
185  protected function _close()
186  {
187  if (true === $this->isPersistent()) {
188  return false;
189  }
190 
191  return @fclose($this->getStream());
192  }
193 
199  public function select()
200  {
201  $read = $this->_stack;
202  $write = null;
203  $except = null;
204 
205  @stream_select($read, $write, $except, $this->getTimeout(), 0);
206 
207  foreach ($read as $socket) {
208  $this->_iterator[] = $socket;
209  }
210 
211  return $this;
212  }
213 
220  public function consider(Connection $other)
221  {
222  if (!($other instanceof self)) {
223  throw new Exception(
224  'Other client must be of type %s.',
225  2,
226  __CLASS__
227  );
228  }
229 
230  if (true === $other->isDisconnected()) {
231  $other->connect();
232  }
233 
234  $otherNode = $other->getCurrentNode();
235  $this->_stack[] = $otherNode->getSocket();
236  $this->_nodes[$otherNode->getId()] = $otherNode;
237 
238  return $this;
239  }
240 
247  public function is(Connection $server)
248  {
249  return $this->getStream() === $server->getStream();
250  }
251 
257  public function current()
258  {
259  $current = parent::_current();
260 
261  return $this->_node = $this->_nodes[$this->getNodeId($current)];
262  }
263 
269  public function isConnected()
270  {
271  return (bool) ($this->getFlag() & self::CONNECT);
272  }
273 
279  public function isAsynchronous()
280  {
281  return (bool) ($this->getFlag() & self::ASYNCHRONOUS);
282  }
283 
289  public function isPersistent()
290  {
291  return (bool) ($this->getFlag() & self::PERSISTENT);
292  }
293 }
const ASYNCHRONOUS
Definition: Client.php:56
const ENCRYPTION_SSLv3
Definition: Client.php:84
& _open($streamName, Stream\Context $context=null)
Definition: Client.php:138
const ENCRYPTION_SSLv2
Definition: Client.php:77
const PERSISTENT
Definition: Client.php:70
consider(Connection $other)
Definition: Client.php:220
const ENCRYPTION_TLS
Definition: Client.php:98
__construct($socket, $timeout=30, $flag=self::CONNECT, $context=null)
Definition: Client.php:119
is(Connection $server)
Definition: Client.php:247
const ENCRYPTION_SSLv23
Definition: Client.php:91