The Io\Poll\Event enum

はじめに

The Io\Poll\Event enum lists the conditions an Io\Poll\Context can watch for and report, together with two modifiers that change how they are reported.

A list of cases is passed to Io\Poll\Context::add, Io\Poll\Watcher::modify and Io\Poll\Watcher::modifyEvents, and returned by Io\Poll\Watcher::getWatchedEvents and Io\Poll\Watcher::getTriggeredEvents.

列挙型概要

Io\Poll Event Read Data is available for reading. For a listening socket, a connection is ready to be accepted. This event is also reported when the end of the stream is reached, for example when the peer closed a TCP connection. Write Data can be written without blocking. Error An error occurred on the handle. This event is reported even if it was not requested. HangUp The handle was hung up, for example because the other end of a pipe or of a Unix domain socket pair was closed. This event is reported even if it was not requested. ReadHangUp The other end closed the connection or shut down its writing half. This event is only reported if it was requested, and only by Io\Poll\Backend::Epoll. OneShot A modifier: the watcher is reported once, and then stays silent until its events are set again with Io\Poll\Watcher::modify or Io\Poll\Watcher::modifyEvents. EdgeTriggered A modifier: the watcher is reported when its state changes, for example when new data arrives, instead of for as long as the condition holds. It is only supported by the backends for which Io\Poll\Backend::supportsEdgeTriggering returns true.

例

例1 Level-triggered and edge-triggered reporting

<?php
use Io\Poll\Context;
use Io\Poll\Event;
use Time\Duration;

[$reader, $writer] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

// Requires a backend that supports edge triggering, see
// Io\Poll\Backend::supportsEdgeTriggering().
$context = new Context();
$context->add(new StreamPollHandle($reader), [Event::Read, Event::EdgeTriggered]);

fwrite($writer, 'ping');
var_dump(count($context->wait(Duration::fromMilliseconds(10))));

// The data was not read, but the readiness is not reported a second time.
var_dump(count($context->wait(Duration::fromMilliseconds(10))));

fwrite($writer, 'pong');
var_dump(count($context->wait(Duration::fromMilliseconds(10))));

上の例の出力は、 たとえば以下のようになります。

int(1)
int(0)
int(1)

参考

  • Io\Poll\Context::add
  • Io\Poll\Watcher::hasTriggered
  • Io\Poll\Backend