Struct message_io::node::NodeListener
source · pub struct NodeListener<S: Send + 'static> { /* private fields */ }
Expand description
Listen events for network and signal events.
Implementations§
source§impl<S: Send + 'static> NodeListener<S>
impl<S: Send + 'static> NodeListener<S>
sourcepub fn for_each(self, event_callback: impl FnMut(NodeEvent<'_, S>))
pub fn for_each(self, event_callback: impl FnMut(NodeEvent<'_, S>))
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");
sourcepub fn for_each_async(
self,
event_callback: impl FnMut(NodeEvent<'_, S>) + Send + 'static,
) -> NodeTask
pub fn for_each_async( self, event_callback: impl FnMut(NodeEvent<'_, S>) + Send + 'static, ) -> NodeTask
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_async(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");
sourcepub fn enqueue(self) -> (NodeTask, EventReceiver<StoredNodeEvent<S>>)
pub fn enqueue(self) -> (NodeTask, EventReceiver<StoredNodeEvent<S>>)
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(),
}
}