pub trait Handler: Send + Iterator<Item = Action<Self::Listener, Self::Transport>> {
type Listener: Resource;
type Transport: Resource;
type Command: Debug + Send;
// Required methods
fn tick(&mut self, time: Timestamp);
fn handle_timer(&mut self);
fn handle_listener_event(
&mut self,
id: <Self::Listener as Resource>::Id,
event: <Self::Listener as Resource>::Event,
time: Timestamp
);
fn handle_transport_event(
&mut self,
id: <Self::Transport as Resource>::Id,
event: <Self::Transport as Resource>::Event,
time: Timestamp
);
fn handle_command(&mut self, cmd: Self::Command);
fn handle_error(&mut self, err: Error<Self::Listener, Self::Transport>);
fn handover_listener(&mut self, listener: Self::Listener);
fn handover_transport(&mut self, transport: Self::Transport);
}
Expand description
A service which handles I/O events generated in the Reactor
.
Required Associated Types§
sourcetype Listener: Resource
type Listener: Resource
Type for a listener resource.
Listener resources are resources which may spawn more resources and can’t be written to. A
typical example of a listener resource is a std::net::TcpListener
, however this may also
be a special form of a peripheral device or something else.
sourcetype Transport: Resource
type Transport: Resource
Type for a transport resource.
Transport is a “full” resource which can be read from - and written to. Usual files, network connections, database connections etc are all fall into this category.
sourcetype Command: Debug + Send
type Command: Debug + Send
A command which may be sent to the Handler
from outside of the Reactor
, including
other threads.
The handler object is owned by the reactor runtime and executes always in the context of the reactor runtime thread. Thus, if other (micro)services within the app needs to communicate to the handler they have to use this data type, which usually is an enumeration for a set of commands supported by the handler.
The commands are sent by using reactor Controller
API.
Required Methods§
sourcefn tick(&mut self, time: Timestamp)
fn tick(&mut self, time: Timestamp)
Method called by the reactor on the start of each event loop once the poll has returned.
sourcefn handle_timer(&mut self)
fn handle_timer(&mut self)
Method called by the reactor when a previously set timeout is fired.
Related: Action::SetTimer
.
sourcefn handle_listener_event(
&mut self,
id: <Self::Listener as Resource>::Id,
event: <Self::Listener as Resource>::Event,
time: Timestamp
)
fn handle_listener_event( &mut self, id: <Self::Listener as Resource>::Id, event: <Self::Listener as Resource>::Event, time: Timestamp )
Method called by the reactor upon an I/O event on a listener resource.
Since listener doesn’t support writing, it can be only a read event (indicating that a new resource can be spawned from the listener).
sourcefn handle_transport_event(
&mut self,
id: <Self::Transport as Resource>::Id,
event: <Self::Transport as Resource>::Event,
time: Timestamp
)
fn handle_transport_event( &mut self, id: <Self::Transport as Resource>::Id, event: <Self::Transport as Resource>::Event, time: Timestamp )
Method called by the reactor upon I/O event on a transport resource.
sourcefn handle_command(&mut self, cmd: Self::Command)
fn handle_command(&mut self, cmd: Self::Command)
Method called by the reactor when a Self::Command
is received for the Handler
.
The commands are sent via Controller
from outside of the reactor, including other
threads.
sourcefn handle_error(&mut self, err: Error<Self::Listener, Self::Transport>)
fn handle_error(&mut self, err: Error<Self::Listener, Self::Transport>)
Method called by the reactor on any kind of error during the event loop, including errors of the poll syscall or I/O errors returned as a part of the poll result events.
See Error
for the details on errors which may happen.
sourcefn handover_listener(&mut self, listener: Self::Listener)
fn handover_listener(&mut self, listener: Self::Listener)
Method called by the reactor upon receiving Action::UnregisterListener
.
Passes the listener resource to the Handler
when it is already not a part of the reactor
poll. From this point of time it is safe to send the resource to other threads (like
workers) or close the resource.
sourcefn handover_transport(&mut self, transport: Self::Transport)
fn handover_transport(&mut self, transport: Self::Transport)
Method called by the reactor upon receiving Action::UnregisterTransport
.
Passes the transport resource to the Handler
when it is already not a part of the
reactor poll. From this point of time it is safe to send the resource to other threads
(like workers) or close the resource.