Io\Poll\Context::add
Starts watching a handle
説明
public Io\Poll\Watcher Io\Poll\Context::add(Io\Poll\Handle $handle, array $events, mixed $data = null)
Registers handle with the context, so that
Io\Poll\Context::wait reports it when one of the
given events occurs.
A handle can be watched only once by a context.
Two StreamPollHandle objects created from the same
stream count as the same handle.
The context keeps the returned Io\Poll\Watcher alive
until it is removed with Io\Poll\Watcher::remove,
its stream is closed, or the context is destroyed.
パラメータ
-
handle
-
The handle to watch.
If it is a StreamPollHandle whose stream is not
backed by a file descriptor, an
E_WARNING is
emitted in addition to the exception.
-
events
-
A non-empty list of Io\Poll\Event cases.
It combines the conditions to watch for, such as
Io\Poll\Event::Read and
Io\Poll\Event::Write, with the
Io\Poll\Event::OneShot and
Io\Poll\Event::EdgeTriggered modifiers.
-
data
-
An arbitrary value to associate with the watcher, for example a callback
or an object describing the connection.
It is returned by Io\Poll\Watcher::getData.
戻り値
Returns the Io\Poll\Watcher that represents the
registration.
エラー / 例外
Throws a TypeError if
events is empty or contains a value that is not an
Io\Poll\Event case.
Throws an Io\Poll\InvalidHandleException if
handle cannot be polled, for example because its
stream was closed or is not backed by a file descriptor.
Throws an
Io\Poll\HandleAlreadyWatchedException if
handle is already watched by this context.
Throws an Io\Poll\FailedHandleAddException
if the backend rejects the registration, for example when a regular file is
added to a context that uses Io\Poll\Backend::Epoll,
or when Io\Poll\Event::EdgeTriggered is requested from
Io\Poll\Backend::Poll.
例
例1 Io\Poll\Context::add example
<?php
use Io\Poll\Context;
use Io\Poll\Event;
use Io\Poll\HandleAlreadyWatchedException;
[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$context = new Context();
$handle = new StreamPollHandle($reader);
$watcher = $context->add($handle, [Event::Read], ['name' => 'reader']);
var_dump($watcher->isActive());
var_dump($watcher->getData());
try {
$context->add($handle, [Event::Write]);
} catch (HandleAlreadyWatchedException $exception) {
echo $exception->getMessage(), PHP_EOL;
}
bool(true)
array(1) {
["name"]=>
string(6) "reader"
}
Handle already added
参考
- Io\Poll\Context::wait
- Io\Poll\Watcher::modify
- Io\Poll\Watcher::remove
- StreamPollHandle