mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-28 23:27:25 +01:00
1. Renames
Some of the items in this module were renamed to be less generic:
- Handler => Incoming
- ClientHandler => Requests
- ResponseHandler => InFlightRequest
- Channel::{respond_with => requests}
In the case of Handler: handler of *what*? Now it's a bit clearer that
this is a stream of Channels (aka *incoming* connections).
Similarly, ClientHandler was a stream of requests over a single
connection. Hopefully Requests better reflects that.
ResponseHandler was renamed InFlightRequest because it no longer
contains the serving function. Instead, it is just the request, plus
the response channel and an abort hook. As a result of this,
Channel::respond_with underwent a big change: it used to take the
serving function and return a ClientHandler; now it has been renamed
Channel::requests and does not take any args.
2. Execute methods
All methods thats actually result in responses being generated
have been consolidated into methods named `execute`:
- InFlightRequest::execute returns a future that completes when a
response has been generated and sent to the server Channel.
- Requests::execute automatically spawns response handlers for all
requests over a single channel.
- Channel::execute is a convenience for `channel.requests().execute()`.
- Incoming::execute automatically spawns response handlers for all
requests over all channels.
3. Removal of Server.
server::Server was removed, as it provided no value over the Incoming/Channel
abstractions. Additionally, server::new was removed, since it just
returned a Server.
81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
// Copyright 2018 Google LLC
|
|
//
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file or at
|
|
// https://opensource.org/licenses/MIT.
|
|
|
|
use clap::{App, Arg};
|
|
use futures::{future, prelude::*};
|
|
use service::World;
|
|
use std::{
|
|
io,
|
|
net::{IpAddr, SocketAddr},
|
|
};
|
|
use tarpc::{
|
|
context,
|
|
server::{self, Channel, Incoming},
|
|
tokio_serde::formats::Json,
|
|
};
|
|
|
|
// This is the type that implements the generated World trait. It is the business logic
|
|
// and is used to start the server.
|
|
#[derive(Clone)]
|
|
struct HelloServer(SocketAddr);
|
|
|
|
#[tarpc::server]
|
|
impl World for HelloServer {
|
|
async fn hello(self, _: context::Context, name: String) -> String {
|
|
format!("Hello, {}! You are connected from {:?}.", name, self.0)
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> io::Result<()> {
|
|
env_logger::init();
|
|
|
|
let flags = App::new("Hello Server")
|
|
.version("0.1")
|
|
.author("Tim <tikue@google.com>")
|
|
.about("Say hello!")
|
|
.arg(
|
|
Arg::with_name("port")
|
|
.short("p")
|
|
.long("port")
|
|
.value_name("NUMBER")
|
|
.help("Sets the port number to listen on")
|
|
.required(true)
|
|
.takes_value(true),
|
|
)
|
|
.get_matches();
|
|
|
|
let port = flags.value_of("port").unwrap();
|
|
let port = port
|
|
.parse()
|
|
.unwrap_or_else(|e| panic!(r#"--port value "{}" invalid: {}"#, port, e));
|
|
|
|
let server_addr = (IpAddr::from([0, 0, 0, 0]), port);
|
|
|
|
// JSON transport is provided by the json_transport tarpc module. It makes it easy
|
|
// to start up a serde-powered json serialization strategy over TCP.
|
|
let mut listener = tarpc::serde_transport::tcp::listen(&server_addr, Json::default).await?;
|
|
listener.config_mut().max_frame_length(usize::MAX);
|
|
listener
|
|
// Ignore accept errors.
|
|
.filter_map(|r| future::ready(r.ok()))
|
|
.map(server::BaseChannel::with_defaults)
|
|
// Limit channels to 1 per IP.
|
|
.max_channels_per_key(1, |t| t.as_ref().peer_addr().unwrap().ip())
|
|
// serve is generated by the service attribute. It takes as input any type implementing
|
|
// the generated World trait.
|
|
.map(|channel| {
|
|
let server = HelloServer(channel.as_ref().as_ref().peer_addr().unwrap());
|
|
channel.requests().execute(server.serve())
|
|
})
|
|
// Max 10 channels.
|
|
.buffer_unordered(10)
|
|
.for_each(|_| async {})
|
|
.await;
|
|
|
|
Ok(())
|
|
}
|