Struct libuv::handles::poll::PollHandle

source ·
pub struct PollHandle { /* private fields */ }
Expand description

Poll handles are used to watch file descriptors for readability, writability and disconnection similar to the purpose of poll(2).

The purpose of poll handles is to enable integrating external libraries that rely on the event loop to signal it about the socket status changes, like c-ares or libssh2. Using PollHandle for any other purpose is not recommended; TcpHandle, UdpHandle, etc. provide an implementation that is faster and more scalable than what can be achieved with PollHandle, especially on Windows.

It is possible that poll handles occasionally signal that a file descriptor is readable or writable even when it isn’t. The user should therefore always be prepared to handle EAGAIN or equivalent when it attempts to read from or write to the fd.

It is not okay to have multiple active poll handles for the same socket, this can cause libuv to busyloop or otherwise malfunction.

The user should not close a file descriptor while it is being polled by an active poll handle. This can cause the handle to report an error, but it might also start polling another socket. However the fd can be safely closed immediately after a call to stop() or close().

Note: On windows only sockets can be polled with poll handles. On Unix any file descriptor that would be accepted by poll(2) can be used.

Note: On AIX, watching for disconnection is not supported.

Implementations§

source§

impl PollHandle

source

pub fn new(loop: &Loop, fd: File) -> Result<PollHandle>

Create and initialize a new poll handle using a file descriptor

source

pub fn new_socket(loop: &Loop, socket: Socket) -> Result<PollHandle>

Create and initialize a new poll handle using a socket descriptor. On Unix this is identical to new(). On windows it takes a SOCKET handle.

source

pub fn start<CB: Into<PollCB<'static>>>( &mut self, events: PollEvents, cb: CB ) -> Result<()>

Starts polling the file descriptor. events is a bitmask made up of READABLE, WRITABLE, PRIORITIZED and DISCONNECT. As soon as an event is detected the callback will be called with status set to 0, and the detected events set on the events field.

The PRIORITIZED event is used to watch for sysfs interrupts or TCP out-of-band messages.

The DISCONNECT event is optional in the sense that it may not be reported and the user is free to ignore it, but it can help optimize the shutdown path because an extra read or write call might be avoided.

If an error happens while polling, status will be a libuv::Error. The user should not close the socket while the handle is active. If the user does that anyway, the callback may be called reporting an error status, but this is not guaranteed. If status == EBADF polling is discontinued for the file handle and no further events will be reported. The user should then call close() on the handle.

Note: Calling start() on a handle that is already active is fine. Doing so will update the events mask that is being watched for.

Note: Though DISCONNECT can be set, it is unsupported on AIX and as such will not be set on the events field in the callback.

Note: If one of the events READABLE or WRITABLE are set, the callback will be called again, as long as the given fd/socket remains readable or writable accordingly. Particularly in each of the following scenarios:

  • The callback has been called because the socket became readable/writable and the callback did not conduct a read/write on this socket at all.
  • The callback committed a read on the socket, and has not read all the available data (when READABLE is set).
  • The callback committed a write on the socket, but it remained writable afterwards (when WRITABLE is set).
  • The socket has already became readable/writable before calling start() on a poll handle associated with this socket, and since then the state of the socket did not changed.

In all of the above listed scenarios, the socket remains readable or writable and hence the callback will be called again (depending on the events set in the bitmask). This behaviour is known as level triggering.

source

pub fn stop(&mut self) -> Result<()>

Stop polling the file descriptor, the callback will no longer be called.

Note: Calling stop() is effective immediately: any pending callback is also canceled, even if the socket state change notification was already pending.

Trait Implementations§

source§

impl Clone for PollHandle

source§

fn clone(&self) -> PollHandle

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl From<PollHandle> for Handle

source§

fn from(poll: PollHandle) -> Handle

Converts to this type from the input type.
source§

impl HandleTrait for PollHandle

source§

fn is_active(&self) -> bool

Returns non-zero if the handle is active, zero if it’s inactive. What “active” means depends on the type of handle: Read more
source§

fn is_closing(&self) -> bool

Returns non-zero if the handle is closing or closed, zero otherwise. Read more
source§

fn close<CB: Into<CloseCB<'static>>>(&mut self, cb: CB)

Request handle to be closed. close_cb will be called asynchronously after this call. This MUST be called on each handle before memory is released. Moreover, the memory can only be released in close_cb or after it has returned. Read more
source§

fn ref(&mut self)

Reference the given handle. References are idempotent, that is, if a handle is already referenced calling this function again will have no effect.
source§

fn unref(&mut self)

Un-reference the given handle. References are idempotent, that is, if a handle is not referenced calling this function again will have no effect.
source§

fn has_ref(&self) -> bool

Returns true if the handle referenced, zero otherwise.
source§

fn send_buffer_size(&mut self, value: i32) -> Result<i32>

Gets or sets the size of the send buffer that the operating system uses for the socket. Read more
source§

fn recv_buffer_size(&mut self, value: i32) -> Result<i32>

Gets or sets the size of the receive buffer that the operating system uses for the socket. Read more
source§

fn get_fileno(&self) -> Result<OsFile>

Gets the platform dependent file descriptor equivalent. Read more
source§

fn get_loop(&self) -> Loop

Returns the Loop associated with this handle.
source§

fn get_type(&self) -> HandleType

Returns the type of the handle.
source§

impl ToHandle for PollHandle

source§

impl TryFrom<Handle> for PollHandle

§

type Error = ConversionError

The type returned in the event of a conversion error.
source§

fn try_from(handle: Handle) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for PollHandle

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.