EvIo
final class EvIo extends EvWatcher (View source)
Class EvIo
EvIo watchers check whether a file descriptor(or socket, or a stream castable to numeric file descriptor) is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data. This behaviour is called level-triggering because events are kept receiving as long as the condition persists. To stop receiving events just stop the watcher.
The number of read and/or write event watchers per fd is unlimited. Setting all file descriptors to non-blocking mode is also usually a good idea (but not required).
Another thing to watch out for is that it is quite easy to receive false readiness notifications, i.e. the callback might be called with Ev::READ but a subsequent read() will actually block because there is no data. It is very easy to get into this situation. Thus it is best to always use non-blocking I/O: An extra read() returning EAGAIN (or similar) is far preferable to a program hanging until some data arrives.
If for some reason it is impossible to run the fd in non-blocking mode, then separately re-test whether a file descriptor is really ready. Some people additionally use SIGALRM and an interval timer, just to be sure they won't block infinitely.
Always consider using non-blocking mode.
Properties
bool | $is_active | from EvWatcher | |
bool | $is_pending | from EvWatcher | |
mixed | $data | from EvWatcher | |
int | $priority | from EvWatcher | |
resource | $fd | ||
int | $events |
Methods
Constructs EvIo watcher object.
Invokes the watcher callback with the given received events bit mask.
Configures the watcher.
Create stopped EvIo watcher object.
Details
__construct(resource $fd, int $events, callable $callback, mixed $data = null, int $priority = 0)
Constructs EvIo watcher object.
Constructs EvIo watcher object and starts the watcher automatically.
int
clear()
Clear watcher pending status.
If the watcher is pending, this method clears its pending status and returns its revents bitset (as if its callback was invoked). If the watcher isn't pending it does nothing and returns 0.
Sometimes it can be useful to "poll" a watcher instead of waiting for its callback to be invoked, which can be accomplished with this function.
feed(int $revents)
Feeds the given revents set into the event loop.
Feeds the given revents set into the event loop, as if the specified event had happened for the watcher.
EvLoop
getLoop()
Returns the loop responsible for the watcher.
invoke(int $revents)
Invokes the watcher callback with the given received events bit mask.
keepalive(bool $value = true)
Configures whether to keep the loop from returning.
Configures whether to keep the loop from returning. With keepalive value set to FALSE the watcher won't keep Ev::run() / EvLoop::run() from returning even though the watcher is active.
Watchers have keepalive value TRUE by default.
Clearing keepalive status is useful when returning from Ev::run() / EvLoop::run() just because of the watcher is undesirable. It could be a long running UDP socket watcher or so.
setCallback(callable $callback)
Sets new callback for the watcher.
start()
Starts the watcher.
Marks the watcher as active. Note that only active watchers will receive events.
stop()
Stops the watcher.
Marks the watcher as inactive. Note that only active watchers will receive events.
set(resource $fd, int $events)
Configures the watcher.
final static EvIo
createStopped(mixed $fd, int $events, mixed $callback, mixed $data = null, int $priority = 0)
Create stopped EvIo watcher object.
The same as EvIo::__construct() , but doesn't start the watcher automatically.