[][src]Struct libzmq::poll::Poller

pub struct Poller { /* fields omitted */ }

A mechanism for input/output events multiplexing in a level-triggered fashion.

The poller is used to asynchronously detect events of a set of monitored Pollable elements. These elements must be registered for monitoring by adding them to the Poller.

Example

use libzmq::{prelude::*, *, poll::*};

// We initialize our sockets and connect them to each other.
let addr: TcpAddr = "127.0.0.1:*".try_into()?;

let server = Server::new()?;
server.bind(addr)?;

let bound = server.last_endpoint()?;

let client = Client::new()?;
client.connect(&bound)?;

// We create our poller instance.
let mut poller = Poller::new();
poller.add(&server, PollId(0), READABLE)?;
poller.add(&client, PollId(1), READABLE)?;

// Initialize the client.
client.send("ping")?;

let mut events = Events::new();

// Now the client and each server will send messages back and forth.
for _ in 0..100 {
    // Wait indefinitely until at least one event is detected.
    poller.poll(&mut events, Period::Infinite)?;
    // Iterate over the detected events. Note that, since the events are
    // guaranteed to be non-empty, we don't need to protect against spurious
    // wakeups.
    for event in &events {
        assert!(event.is_readable());
        match event.id() {
            // The server is ready to receive an incoming message.
            PollId(0) => {
                let msg = server.recv_msg()?;
                assert_eq!("ping", msg.to_str()?);
                server.send(msg)?;
            }
            // One of the clients is ready to receive an incoming message.
            PollId(1) => {
                let msg = client.recv_msg()?;
                assert_eq!("ping", msg.to_str()?);
                client.send(msg)?;
            }
            _ => unreachable!(),
        }
    }
}

Methods

impl Poller[src]

pub fn new() -> Self[src]

Create a new empty poller.

pub fn add<'a, P>(
    &mut self,
    pollable: P,
    id: PollId,
    trigger: Trigger
) -> Result<(), Error> where
    P: Into<Pollable<'a>>, 
[src]

Add a Pollable element for monitoring by the poller with the specified Trigger condition.

Returned Errors

Example

use libzmq::{Server, poll::*, ErrorKind};

let server = Server::new()?;

let mut poller = Poller::new();

poller.add(&server, PollId(0), EMPTY)?;
let err = poller.add(&server, PollId(1), EMPTY).unwrap_err();

match err.kind() {
    ErrorKind::InvalidInput { .. } => (),
    _ => panic!("unexpected error"),
}

pub fn remove<'a, P>(&mut self, pollable: P) -> Result<(), Error> where
    P: Into<Pollable<'a>>, 
[src]

Remove a Pollable element from monitoring by the poller.

Returned Errors

Example

use libzmq::{Server, poll::*, ErrorKind};

let server = Server::new()?;
let mut poller = Poller::new();

poller.add(&server, PollId(0), EMPTY)?;
poller.remove(&server)?;

let err = poller.remove(&server).unwrap_err();
match err.kind() {
    ErrorKind::InvalidInput { .. } => (), // cannot remove socket twice.
    _ => panic!("unexpected error"),
}

pub fn modify<'a, P>(
    &mut self,
    pollable: P,
    trigger: Trigger
) -> Result<(), Error> where
    P: Into<Pollable<'a>>, 
[src]

Modify the Trigger confition for the specified Pollable element monitored by the poller.

Returned Errors

pub fn try_poll(&mut self, events: &mut Events) -> Result<(), Error>[src]

Check for events in the monitored elements, returning instantly.

If no events occured, returns WouldBlock. Note that in this case, the Events would also be empty.

Returned Errors

pub fn poll(
    &mut self,
    events: &mut Events,
    timeout: Period
) -> Result<(), Error>
[src]

The poller will wait for events in the monitored elements, blocking until at least one event occurs, or the specified timeout Period expires.

If the specified Period is infinite, the poller will block forever until an event occurs.

Returned Errors

Trait Implementations

impl Debug for Poller[src]

impl Default for Poller[src]

impl Drop for Poller[src]

impl Eq for Poller[src]

impl PartialEq<Poller> for Poller[src]

impl StructuralEq for Poller[src]

impl StructuralPartialEq for Poller[src]

Auto Trait Implementations

impl RefUnwindSafe for Poller

impl !Send for Poller

impl !Sync for Poller

impl Unpin for Poller

impl UnwindSafe for Poller

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,