Struct thrust::reactor::Reactor
[−]
[src]
pub struct Reactor { // some fields omitted }
The Reactor
is the component that interacts with networking. The reactor is
built around Mio's event loop and manages both TcpListeners and TcpStreams.
The reactor isn't responsible for anything Thrift related, so it doesn't know about parsing, protocols, serialization, etc... All it's responsible for is sending and receiving data from various connections and dispatching them to the appropriate channels.
To communicate into the event loop, you use a copy of Mio's Sender
channel
type to send Message
s. These will be intercepted in the event loop and processed.
Things you might send to the Reactor
through this mechanism:
- Binding a new TCP listener — Each reactor is capable of handling an unbounded number of listeners, who will all be able to accept new sockets.
Binding a new listener requires that you have already established a blocking variant
through net::TcpListener
. The listener will be converted to Mio's non-blocking variant.
use std::net::TcpListener;
use std::sync::mpsc::channel;
// The channel is used as a channel. Any socket being accepted
// by this listener will also use this channel (the sender part).
let (tx, rx) = channel();
let addr = "127.0.0.1:4566".parse().unwrap();
reactor_sender.send(Message::Bind(addr, tx));
- Connecting to a remote TCP server and establishing a new non-blocking
TcpStream
.
use std::sync::mpsc::channel;
// The callback channel on the single socket.
let (tx, rx) = channel();
let addr = "127.0.0.1::4566".parse().unwrap();
reactor_sender.send(Message::Connect(addr, tx));
- Sending RPC calls (initiating or reply) to a socket. Instead of writing or reading
primitives, we boil everything down to Rpc or Data messages, each with an associative Token
to mark the responsible
TcpStream
orConnection
.
reactor_sender.send(Message::Rpc(Token(1), vec![0, 1, 3, 4]));