Skip to main content

MessageHandler

Trait MessageHandler 

Source
pub trait MessageHandler<M, E>:
    Send
    + Sync
    + 'static
where M: MessageBody, E: Error,
{ // Required method fn handle(&self, connection: Connection, msg: Message<M>) -> Result<(), E>; // Provided method fn is_valid(&self) -> bool { ... } }
Expand description

Interface for handling received messages.

Handles a single Message, and optionally returns an error. The Connection that the message was sent from is also given, to distinguish which connection it came from in networking stacks that have multiple connections.

Message handlers may be executed from networking driver internal threads. Because of this, any work done in the handler should be light, as it has the potential to block further networking processing. If heavy work needs to be done, it should be delegated to a separate thread. For example, a MessageQueue can be used to put all messages in a queue, where they can be polled and processed from another thread.

Required Methods§

Source

fn handle(&self, connection: Connection, msg: Message<M>) -> Result<(), E>

Process and handle a Message.

The Connection represents which connection sent the message.

Provided Methods§

Source

fn is_valid(&self) -> bool

Whether this handler is still valid.

If it is not valid, it will be removed and no longer used to handle messages. This is primarily used with e.g. handlers implemented for weak references. Note that MessageHandler is automatically implemented for any Weak<H: MessageHandler>, where this method will return false if the weak reference is no longer valid.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<M, E, H> MessageHandler<M, E> for Arc<H>
where M: MessageBody, E: Error, H: MessageHandler<M, E>,

Source§

fn handle(&self, connection: Connection, msg: Message<M>) -> Result<(), E>

Source§

impl<M, E, H> MessageHandler<M, E> for Weak<H>
where M: MessageBody, E: Error, H: MessageHandler<M, E>,

Source§

fn handle(&self, connection: Connection, msg: Message<M>) -> Result<(), E>

Source§

fn is_valid(&self) -> bool

Implementors§

Source§

impl<M, E, F> MessageHandler<M, E> for F
where M: MessageBody, E: Error, F: Fn(Connection, Message<M>) -> Result<(), E> + Send + Sync + 'static,

Source§

impl<M> MessageHandler<M, MessageQueueFull> for MessageQueue<M>
where M: MessageBody + Send + Sync + 'static,