pub struct NodeListener<S: Send + 'static> { /* private fields */ }
Expand description

Listen events for network and signal events.

Implementations

Iterate indefinitely over all generated NetEvent. This function will work until NodeHandler::stop() is called.

Note that any events generated before calling this function (e.g. some connection was done) will be stored and offered once you call for_each().

Example
use message_io::node::{self, NodeEvent};
use message_io::network::Transport;

let (handler, listener) = node::split();
handler.signals().send_with_timer((), std::time::Duration::from_secs(1));
let (id, addr) = handler.network().listen(Transport::FramedTcp, "127.0.0.1:0").unwrap();

listener.for_each(move |event| match event {
    NodeEvent::Network(net_event) => { /* Your logic here */ },
    NodeEvent::Signal(_) => handler.stop(),
});
// Blocked here until handler.stop() is called (1 sec).
println!("Node is stopped");

Similar to NodeListener::for_each() but it returns the control to the user after calling it. The events will be processed asynchronously. A NodeTask representing this asynchronous job is returned. Destroying this object will result in blocking the current thread until NodeHandler::stop() is called.

In order to allow the node working asynchronously, you can move the NodeTask to a an object with a longer lifetime.

Example
use message_io::node::{self, NodeEvent};
use message_io::network::Transport;

let (handler, listener) = node::split();
handler.signals().send_with_timer((), std::time::Duration::from_secs(1));
let (id, addr) = handler.network().listen(Transport::FramedTcp, "127.0.0.1:0").unwrap();

let task = listener.for_each(move |event| match event {
     NodeEvent::Network(net_event) => { /* Your logic here */ },
     NodeEvent::Signal(_) => handler.stop(),
});
// for_each_async() will act asynchronous during 'task' lifetime.

// ...
println!("Node is running");
// ...

drop(task); // Blocked here until handler.stop() is called (1 sec).
// Also task.wait(); can be called doing the same (but taking a mutable reference).

println!("Node is stopped");

Consumes the listener to create a NodeTask and an EventReceiver where the events of this node will be sent. The events will be sent to the EventReceiver during the NodeTask lifetime. The aim of this method is to offer a synchronous way of working with a node, without using a clousure. This easier API management has a performance cost. Compared to NodeListener::for_each(), this function adds latency because the node event must be copied and no longer reference data from the internal socket buffer.

Example
use message_io::node::{self, StoredNodeEvent as NodeEvent};
use message_io::network::Transport;

let (handler, listener) = node::split();
handler.signals().send_with_timer((), std::time::Duration::from_secs(1));
let (id, addr) = handler.network().listen(Transport::FramedTcp, "127.0.0.1:0").unwrap();

let (task, mut receiver) = listener.enqueue();

loop {
    match receiver.receive() {
        NodeEvent::Network(net_event) => { /* Your logic here */ },
        NodeEvent::Signal(_) => break handler.stop(),
    }
}

Trait Implementations

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.