mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-29 23:55:59 +01:00
* Change `client::{future, sync}, server::{future, sync}` to `future::{client, server}, sync::{client, server}`
This reflects the most common usage pattern and allows for some types to not need to be fully qualified when used together (e.g. the previously-named `client::future::Options` and `server::future::Options` can now be `client::Options` and `server::Options`).
* sync::client: create a RequestHandler struct to encapsulate logic of processing client requests.
The largest benefit is that unit testing becomes easier, e.g. testing that the request processing stops when all request senders are dropped.
* Rename Serialize error variants to make sense.
* Rename tarpc_service_ConnectFuture__ => Connect (because it's public)
* Use tokio proto's ClientProxy.
Rather than reimplement the same logic. Curiously, this commit
isn't a net loss in LOC. Oh well.
* Factor out os-specific functionality in listener() into their own fns
* Remove service-fn dep
44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
// 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.
|
|
|
|
// required by `FutureClient` (not used directly in this example)
|
|
#![feature(plugin)]
|
|
#![plugin(tarpc_plugins)]
|
|
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
extern crate tokio_core;
|
|
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use tarpc::sync::{client, server};
|
|
use tarpc::sync::client::ClientExt;
|
|
use tarpc::util::Never;
|
|
|
|
service! {
|
|
rpc hello(name: String) -> String;
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct HelloServer;
|
|
|
|
impl SyncService for HelloServer {
|
|
fn hello(&self, name: String) -> Result<String, Never> {
|
|
Ok(format!("Hello, {}!", name))
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let handle = HelloServer.listen("localhost:0", server::Options::default()).unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
let client = SyncClient::connect(rx.recv().unwrap(), client::Options::default()).unwrap();
|
|
println!("{}", client.hello("Mom".to_string()).unwrap());
|
|
}
|