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>

source

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");
source

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");
source

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(),
    }
}

Trait Implementations§

source§

impl<S: Send + 'static> Drop for NodeListener<S>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for NodeListener<S>

§

impl<S> Send for NodeListener<S>

§

impl<S> Sync for NodeListener<S>
where S: Sync,

§

impl<S> Unpin for NodeListener<S>
where S: Unpin,

§

impl<S> !UnwindSafe for NodeListener<S>

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> Same for T

§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V