Trait Reactor

Source
pub trait Reactor {
    type UserCommand;

    // Required method
    fn on_inbound_message(
        &mut self,
        buf: &mut [u8],
        new_bytes: usize,
        decoded_msg_size: usize,
        ctx: &mut DispatchContext<'_, Self::UserCommand>,
    ) -> Result<MessageResult>;

    // Provided methods
    fn on_connected(
        &mut self,
        _ctx: &mut DispatchContext<'_, Self::UserCommand>,
        _listener: ReactorID,
    ) -> Result<()> { ... }
    fn on_readable(
        &mut self,
        ctx: &mut ReactorReableContext<'_, Self::UserCommand>,
    ) -> Result<()> { ... }
    fn on_command(
        &mut self,
        _cmd: Self::UserCommand,
        ctx: &mut DispatchContext<'_, Self::UserCommand>,
    ) -> Result<()> { ... }
    fn on_close(
        &mut self,
        _reactorid: ReactorID,
        _cmd_sender: &CmdSender<Self::UserCommand>,
    ) { ... }
}
Expand description

A Reactor is assigned a unique ReactorID when adding to a ReactRuntime, and is able to receive socket messsages (via reader) and commands. process_events of a ReactRuntime instance should be periodically called in a dedicated thread. Besides socket communication, Sending command is the only thread-safe way to communicate with a Reactor. A Reactor could send socket messages (via sender), or send commands (via cmd_sender) to another Reactor with specific ReactorID. A Reactor is destroyed when the socket is closed.

Required Associated Types§

Required Methods§

Source

fn on_inbound_message( &mut self, buf: &mut [u8], new_bytes: usize, decoded_msg_size: usize, ctx: &mut DispatchContext<'_, Self::UserCommand>, ) -> Result<MessageResult>

It’s called by in on_readable() when either decoded_msg_size==0 (meaning message size is unknown) or decoded_msg_size <= buf.len() (meaning a full message is read).

  • buf - The buffer containing all the received bytes
  • new_bytes - Number of new received bytes, which are also bytes that are not processed. If previously on_inbound_message returned DropMsgSize. new_bytes is the remaining bytes.
  • decoded_msg_size - the decoded message size which is return value of previous call of on_inbound_message. 0 means message having not been decoded.
  • return ExpectMsgSize(msgsize) to indicate more read until full msgsize is read then call next dispatch. msgsize==0 indicates msg size is unknown. DropMsgSize(msgsize) to indiciate message is processed already. framework can drop the message after call. then msgsize will be 0 again.
  • Note that when calling on_inbound_message(decoded_msg_size) -> ExpectMsgSize(expect_msg_size), if expect_msg_size!=0, it should be always > msg_size.

Provided Methods§

Source

fn on_connected( &mut self, _ctx: &mut DispatchContext<'_, Self::UserCommand>, _listener: ReactorID, ) -> Result<()>

ReactRuntime calls it when connection is established.

  • ctx - The context the used for reactor to send socket message or command.
    • Note that ctx.cmd_sener can only send command to a reactor that belongs to the same ReactRuntime.
  • listener - The listener ID when the reactor is created by a listener socket; otherwise, it’s INVALID_REACTOR_ID.
  • return err to close socket.
Source

fn on_readable( &mut self, ctx: &mut ReactorReableContext<'_, Self::UserCommand>, ) -> Result<()>

ReactRuntime calls it when there’s a readable event. ctx.reader could be used to read message. See MsgReader for usage. This is a default implementation which uses MsgReader to read all messages then call on_inbound_message to dispatch (default try_read_fast_read). User may override this function to implement other strategies (e.g. `try_read_fast_dispatch``).

  • return Err to close socket.
Source

fn on_command( &mut self, _cmd: Self::UserCommand, ctx: &mut DispatchContext<'_, Self::UserCommand>, ) -> Result<()>

ReactRuntime calls it when receiving a command. If no user command is used (e.g. type UserCommand=();), user may not need to override it. return Err to close the socket.

Source

fn on_close( &mut self, _reactorid: ReactorID, _cmd_sender: &CmdSender<Self::UserCommand>, )

ReactRuntime calls it when the reactor is removed from poller and before closing the socket. The Reactor will be destroyed after this call.

Implementors§

Source§

impl<AppData> Reactor for SimpleIoReactor<AppData>

Source§

impl<AppData> Reactor for SimpleIoService<AppData>