Struct message_io::node::NodeListener[][src]

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

Main entity to manipulates the network and signal events easily. The node run asynchronously.

Implementations

impl<S: Send + 'static> NodeListener<S>[src]

pub fn for_each(self, event_callback: impl FnMut(NodeEvent<'_, S>) + 'static)[src]

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

Note that any events generated before calling this function (e.g. some connection was done) will be storage 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));
handler.network().listen(Transport::FramedTcp, "0.0.0.0:1234");

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

pub fn for_each_async(
    self,
    event_callback: impl FnMut(NodeEvent<'_, S>) + Send + 'static
) -> NodeTask
[src]

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

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

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));
handler.network().listen(Transport::FramedTcp, "0.0.0.0:1234");

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() was called (1 sec).
// Also task.wait(); can be called doing the same (but taking a mutable reference).

println!("Node is stopped");

Trait Implementations

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

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

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

type Init = T

The type for initializers.

impl<T> Same<T> for T

type Output = T

Should always be Self

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>,