NAME

UniEvent::Poll - polls sockets or files for readability or writeability

SYNOPSIS

	use UniEvent::Poll;

    my $loop = UniEvent::Loop->new;
    my $fd = ...; # obtain it somehow, i.e. via socket call
    my $h = UniEvent::Poll->new($fd, $loop);
    $h->event->add(sub {
    	my ($handle, $events, $error_code) = @_;
    	die("error: $error_code") if $error_code;
    	say "can read" if $events & READABLE;
    	say "can write" if $events & WRITABLE;
    });
    $h->start;
    $loop->run;
    $h->stop;
	close($fd); # it is safe to close now

DESCRIPTION

The Poll handle polls sockets or files for readability or writeability. It was designed for intergration of raw sockets with third-party libraries that signal about socket status change. Using the Poll handle for other purposes is discouraged as it is not very efficient; it is better to use standard UniEvent::Tcp capabilities, for example.

Beware that only one Poll handle should be per one socket, otherwise undefined behaviour might happen.

The user should not close a file descriptor while it is being polled by an active poll handle.

Portability notest: on *nix any file descriptor (including sockets) can be polled; on Windows only socket can be polled.

The Poll handle is inherited from UniEvent::Handle.

METHODS

new($fd [, $loop = UniEvent::Loop->default_loop])

Constructs new Poll handle for the specified file descriptor $fd and binds it to the specified event loop.

start($interested_events [, $callback = undef])

Starts the poll handle to watch the supplied $interested_events for the next event loop iteration. Optionally it adds the $callback to the event listeners.

stop()

Stops the poll handle, i.e. makes it inactive for the next event loop iteration. It is safe to close the underlying socket.

callback($code)

Sets the callback, which will be invoked in loop run. All previously set event listeners or callbacks are discarded.

event()

Returns XS::Framework::CallbackDispatcher instance, where callback(s) can be assigned with. The callbacks will be invoked in loop run.

The C++ interface is:

void(const PollSP& handle, int events, const std::error_code& err)

On perl side it the callbacks will be called :

$callback->($handle, $occured_events, $error_code)

The callback returns nothing.

Multiple callbacks can be added via the XS::Framework::CallbackDispatcher interface.

event_listener($delegate [, $weak = false])

Creates and returns wrapper around the $delegate object of arbitrary class, having the following methods:

$delegate->on_poll($handle, $occured_events, $error_code);

The delegated object can be seen as alternative of setting indivitual callbacks or as a way of groupping them. The $delegate object can optionally be weakened.

call_now($events, $error_code = undef)

Immediately ivokes assigned callbacks and listeners in the caller context (i.e. not waiting loop run).

CONSTANTS

READABLE

Checks whether the socket is readable

WRITABLE

Checks whether the socket is writeable

TYPE

Poll type constant

REFERENCES

UniEvent::Handle

UniEvent::Idle

XS::Framework::CallbackDispatcher