Sonr is built on top of Mio with the aim to make it easier to create networking applications.
Reactor reacts to
Events from the
System, or the output of another
reactor. This makes it possible (and intended) to chain two
reactors. Such a chain is in it self a
Reactor, and can be chained further.
System receives an
Event it is pushed down the chain(s) of
reactors until a reactor matching the event id reacts, returning a
that is passed to the next
Reactor in the chain.
System is thread local and has to exist for each thread using
There can only be one instance of a
System per thread, and this instance is created by
init twice in the same thread will panic).
The reactor trait consists of two types and a method:
To chain one reactor with another the
Output of the first has to be
the same type as the
Input of the second.
The following example shows the creation of a chain from two reactors.
Events there is no need for the
Since reactors in a chain will always push data forward and never return
anything other than
Reaction::Continue it is not possible to capture the
output from line 46:
To print the result in the above example it would be possible to create a third
reactor that prints the given
Input, however rather than creating yet another reactor
map function of a reactor accepts a closure that is called if the reactor
val as an argument.
Updating the previous example to use
map to print the output of
map function takes a closure as an argument, where the argument for the
closure is the
Output of the reactor.
Since the output of
UppercaseString is a
map function is
called with a closure
FnMut(UppercaseString::Output) -> T.
This is a convenient way to change the output of a reactor.
ReactiveTcpListener outputs a tuple:
The map function could be used here if the
SocketAddr is not required:
streaminstead of the tuple, the Reactor Output is changed to
Another method available on a Reactor is
This is useful to run more than one reactor in parallel.
Since the output of a tcp listener is a stream and a socket address, it would
not be possible to chain two tcp listeners together.
It is also not possible to call
System::start twice in the same thread.
To run two tcp listeners at the same time use
This means a
Reaction::Event(event) from the
System will be sent to the
first listener and the second listener.
System::init call has to come before a
ReactiveTcpStream or a
ReactiveTcpListener is created.
System::start(reactor) is called, the
Events and passes the events the reactor.
If the event does not belong to the reactor it is passed down the chain until a reactor reacts to the event.
System has a few responsibilities:
- Polling system events and passing them to the reactors
- Registering / Re registering
- Managing tokens used when registering an
System also registers EventedReactors
Shutting down a
init returns a
At the time of writing the
SystemEvent only contains one variance: