pub trait MessageHandler<M, E>:
Send
+ Sync
+ 'staticwhere
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§
Sourcefn handle(&self, connection: Connection, msg: Message<M>) -> Result<(), E>
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§
Sourcefn is_valid(&self) -> bool
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".