mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-02-23 15:49:54 +01:00
Rewrite using tokio (#44)
* Rewrite tarpc on top of tokio. * Add examples * Move error types to their own module. Also, cull unused error variants. * Remove unused fn * Remove CanonicalRpcError* types. They're 100% useless. * Track tokio master (WIP) * The great error revamp. Removed the canonical rpc error type. Instead, the user declares the error type for each rpc: In the above example, the error type is Baz. Declaring an error is optional; if none is specified, it defaults to Never, a convenience struct that wraps the never type (exclamation mark) to impl Serialize, Deserialize, Error, etc. Also adds the convenience type StringError for easily using a String as an error type. * Add missing license header * Minor cleanup * Rename StringError => Message * Create a sync::Connect trait. Along with this, the existing Connect trait moves to future::Connect. The future and sync modules are reexported from the crate root. Additionally, the utility errors Never and Message are no longer reexported from the crate root. * Update readme * Track tokio/futures master. Add a Spawn utility trait to replace the removed forget. * Fix pre-push hook * Add doc comment to SyncServiceExt. * Fix up some documentation * Track tokio-proto master * Don't set tcp nodelay * Make future::Connect take an associated type for the future. * Unbox FutureClient::connect return type * Use type alias instead of newtype struct for ClientFuture * Fix benches/latency.rs * Write a plugin to convert lower_snake_case idents/types to UpperCamelCase. Use it to add associated types to FutureService instead of boxing the return futures. * Specify plugin = true in snake_to_camel/Cargo.toml. Weird things happen otherwise. * Add clippy.toml
This commit is contained in:
73
examples/server_calling_server.rs
Normal file
73
examples/server_calling_server.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
|
||||
// This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
#![feature(conservative_impl_trait, plugin)]
|
||||
#![plugin(snake_to_camel)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate tarpc;
|
||||
extern crate futures;
|
||||
|
||||
use futures::{BoxFuture, Future};
|
||||
use add::{FutureService as AddService, FutureServiceExt as AddExt};
|
||||
use double::{FutureService as DoubleService, FutureServiceExt as DoubleExt};
|
||||
use tarpc::util::{Never, Message};
|
||||
use tarpc::future::Connect as Fc;
|
||||
use tarpc::sync::Connect as Sc;
|
||||
|
||||
pub mod add {
|
||||
service! {
|
||||
/// Add two ints together.
|
||||
rpc add(x: i32, y: i32) -> i32;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod double {
|
||||
use tarpc::util::Message;
|
||||
|
||||
service! {
|
||||
/// 2 * x
|
||||
rpc double(x: i32) -> i32 | Message;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AddServer;
|
||||
|
||||
impl AddService for AddServer {
|
||||
type Add = futures::Finished<i32, Never>;
|
||||
|
||||
fn add(&self, x: i32, y: i32) -> Self::Add {
|
||||
futures::finished(x + y)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct DoubleServer {
|
||||
client: add::FutureClient,
|
||||
}
|
||||
|
||||
impl DoubleService for DoubleServer {
|
||||
type Double = BoxFuture<i32, Message>;
|
||||
|
||||
fn double(&self, x: i32) -> Self::Double {
|
||||
self.client
|
||||
.add(&x, &x)
|
||||
.map_err(|e| e.to_string().into())
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let add = AddServer.listen("localhost:0").unwrap();
|
||||
let add_client = add::FutureClient::connect(add.local_addr()).wait().unwrap();
|
||||
let double = DoubleServer { client: add_client };
|
||||
let double = double.listen("localhost:0").unwrap();
|
||||
|
||||
let double_client = double::SyncClient::connect(double.local_addr()).unwrap();
|
||||
for i in 0..5 {
|
||||
println!("{:?}", double_client.double(&i).unwrap());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user