mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-29 23:55:59 +01:00
- fn serve -> Service::serve - fn new_stub -> Client::new This allows the generated function names to remain consistent across service definitions while preventing collisions.
87 lines
2.6 KiB
Rust
87 lines
2.6 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.
|
|
|
|
#![feature(async_await)]
|
|
|
|
use clap::{App, Arg};
|
|
use futures::{
|
|
future::{self, Ready},
|
|
prelude::*,
|
|
};
|
|
use service::World;
|
|
use std::{io, net::SocketAddr};
|
|
use tarpc::{
|
|
context,
|
|
server::{self, Channel, Handler},
|
|
};
|
|
|
|
// 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);
|
|
|
|
impl World for HelloServer {
|
|
// Each defined rpc generates two items in the trait, a fn that serves the RPC, and
|
|
// an associated type representing the future output by the fn.
|
|
|
|
type HelloFut = Ready<String>;
|
|
|
|
fn hello(self, _: context::Context, name: String) -> Self::HelloFut {
|
|
future::ready(format!(
|
|
"Hello, {}! You are connected from {:?}.",
|
|
name, self.0
|
|
))
|
|
}
|
|
}
|
|
|
|
#[runtime::main(runtime_tokio::Tokio)]
|
|
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 = ([0, 0, 0, 0], port).into();
|
|
|
|
// bincode_transport is provided by the associated crate bincode-transport. It makes it easy
|
|
// to start up a serde-powered bincode serialization strategy over TCP.
|
|
json_transport::listen(&server_addr)?
|
|
// 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.respond_with(server.serve())
|
|
})
|
|
// Max 10 channels.
|
|
.buffer_unordered(10)
|
|
.for_each(|_| futures::future::ready(()))
|
|
.await;
|
|
|
|
Ok(())
|
|
}
|