phpday 2027 - Call for Papers

The Io\Poll\Event enum

(PHP 8 >= 8.6.0)

简介

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().

注解摘要

namespace Io\Poll;
enum Event
{
case 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.

case Write ; // Data can be written without blocking.

case Error ; // An error occurred on the handle. This event is reported even if it was not requested.

case 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.

case 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.

case 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().

case 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)

参见

+添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top