mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-02-23 15:49:54 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cf8e440f7 | ||
|
|
2e02f33fc4 | ||
|
|
d8472dcd1c | ||
|
|
2c5846621f | ||
|
|
6a6157948a | ||
|
|
1c18a3c4fe | ||
|
|
e8ec295e85 | ||
|
|
44eec09418 | ||
|
|
fe116a1b6b | ||
|
|
ec4fa8636b | ||
|
|
2d58340d16 |
@@ -23,7 +23,7 @@ function then returns the value produced by that other server.
|
|||||||
Add to your `Cargo.toml` dependencies:
|
Add to your `Cargo.toml` dependencies:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
tarpc = "0.5.0"
|
tarpc = "0.6"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|||||||
10
RELEASES.md
10
RELEASES.md
@@ -1,4 +1,11 @@
|
|||||||
|
## 0.6 (20216-08-07)
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
|
* Updated serde to 0.8. Requires dependents to update as well.
|
||||||
|
|
||||||
## 0.5 (2016-04-24)
|
## 0.5 (2016-04-24)
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
0.5 adds support for arbitrary transports via the
|
0.5 adds support for arbitrary transports via the
|
||||||
[`Transport`](tarpc/src/transport/mod.rs#L7) trait.
|
[`Transport`](tarpc/src/transport/mod.rs#L7) trait.
|
||||||
Out of the box tarpc provides implementations for:
|
Out of the box tarpc provides implementations for:
|
||||||
@@ -6,6 +13,9 @@ Out of the box tarpc provides implementations for:
|
|||||||
* Tcp, for types `impl`ing `ToSocketAddrs`.
|
* Tcp, for types `impl`ing `ToSocketAddrs`.
|
||||||
* Unix sockets via the `UnixTransport` type.
|
* Unix sockets via the `UnixTransport` type.
|
||||||
|
|
||||||
|
This was a breaking change: `handler.local_addr()` was renamed
|
||||||
|
`handler.dialer()`.
|
||||||
|
|
||||||
## 0.4 (2016-04-02)
|
## 0.4 (2016-04-02)
|
||||||
|
|
||||||
### Breaking Changes
|
### Breaking Changes
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tarpc"
|
name = "tarpc"
|
||||||
version = "0.5.0"
|
version = "0.6.0"
|
||||||
authors = ["Adam Wright <adam.austin.wright@gmail.com>", "Tim Kuehn <timothy.j.kuehn@gmail.com>"]
|
authors = ["Adam Wright <adam.austin.wright@gmail.com>", "Tim Kuehn <timothy.j.kuehn@gmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
documentation = "https://google.github.io/tarpc"
|
documentation = "https://google.github.io/tarpc"
|
||||||
@@ -11,13 +11,13 @@ readme = "../README.md"
|
|||||||
description = "An RPC framework for Rust with a focus on ease of use."
|
description = "An RPC framework for Rust with a focus on ease of use."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bincode = "0.5"
|
bincode = "0.6"
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
scoped-pool = "0.1"
|
scoped-pool = "1.0"
|
||||||
serde = "0.7"
|
serde = "0.8"
|
||||||
unix_socket = "0.5"
|
unix_socket = "0.5"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy_static = "0.1"
|
lazy_static = "0.2"
|
||||||
env_logger = "0.3"
|
env_logger = "0.3"
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
|
|||||||
@@ -250,18 +250,20 @@ macro_rules! impl_deserialize {
|
|||||||
/// * `__Reply` -- an implementation detail
|
/// * `__Reply` -- an implementation detail
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! service {
|
macro_rules! service {
|
||||||
|
// Entry point
|
||||||
(
|
(
|
||||||
$( $tokens:tt )*
|
$(
|
||||||
|
$(#[$attr:meta])*
|
||||||
|
rpc $fn_name:ident( $( $arg:ident : $in_:ty ),* ) $(-> $out:ty)*;
|
||||||
|
)*
|
||||||
) => {
|
) => {
|
||||||
service_inner! {{
|
service! {{
|
||||||
$( $tokens )*
|
$(
|
||||||
|
$(#[$attr])*
|
||||||
|
rpc $fn_name( $( $arg : $in_ ),* ) $(-> $out)*;
|
||||||
|
)*
|
||||||
}}
|
}}
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! service_inner {
|
|
||||||
// Pattern for when the next rpc has an implicit unit return type
|
// Pattern for when the next rpc has an implicit unit return type
|
||||||
(
|
(
|
||||||
{
|
{
|
||||||
@@ -272,7 +274,7 @@ macro_rules! service_inner {
|
|||||||
}
|
}
|
||||||
$( $expanded:tt )*
|
$( $expanded:tt )*
|
||||||
) => {
|
) => {
|
||||||
service_inner! {
|
service! {
|
||||||
{ $( $unexpanded )* }
|
{ $( $unexpanded )* }
|
||||||
|
|
||||||
$( $expanded )*
|
$( $expanded )*
|
||||||
@@ -291,7 +293,7 @@ macro_rules! service_inner {
|
|||||||
}
|
}
|
||||||
$( $expanded:tt )*
|
$( $expanded:tt )*
|
||||||
) => {
|
) => {
|
||||||
service_inner! {
|
service! {
|
||||||
{ $( $unexpanded )* }
|
{ $( $unexpanded )* }
|
||||||
|
|
||||||
$( $expanded )*
|
$( $expanded )*
|
||||||
@@ -300,7 +302,7 @@ macro_rules! service_inner {
|
|||||||
rpc $fn_name( $( $arg : $in_ ),* ) -> $out;
|
rpc $fn_name( $( $arg : $in_ ),* ) -> $out;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Pattern when all return types have been expanded
|
// Pattern for when all return types have been expanded
|
||||||
(
|
(
|
||||||
{ } // none left to expand
|
{ } // none left to expand
|
||||||
$(
|
$(
|
||||||
@@ -498,7 +500,8 @@ macro_rules! service_inner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)] // because we're just testing that the macro expansion compiles
|
#[allow(dead_code)]
|
||||||
|
// because we're just testing that the macro expansion compiles
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod syntax_test {
|
mod syntax_test {
|
||||||
// Tests a service definition with a fn that takes no args
|
// Tests a service definition with a fn that takes no args
|
||||||
@@ -592,7 +595,7 @@ mod functional_test {
|
|||||||
fn async_try_clone_unix() {
|
fn async_try_clone_unix() {
|
||||||
let temp_dir = tempdir::TempDir::new("tarpc").unwrap();
|
let temp_dir = tempdir::TempDir::new("tarpc").unwrap();
|
||||||
let temp_file = temp_dir.path()
|
let temp_file = temp_dir.path()
|
||||||
.join("async_try_clone_unix.tmp");
|
.join("async_try_clone_unix.tmp");
|
||||||
let handle = Server.spawn(UnixTransport(temp_file)).unwrap();
|
let handle = Server.spawn(UnixTransport(temp_file)).unwrap();
|
||||||
let client1 = AsyncClient::new(handle.dialer()).unwrap();
|
let client1 = AsyncClient::new(handle.dialer()).unwrap();
|
||||||
let client2 = client1.try_clone().unwrap();
|
let client2 = client1.try_clone().unwrap();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
use serde;
|
use serde;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::{self, BufReader, BufWriter, Read};
|
use std::io::{self, BufReader, BufWriter};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
@@ -119,9 +119,9 @@ impl<Request, Reply, S> Drop for Client<Request, Reply, S>
|
|||||||
// finish.
|
// finish.
|
||||||
debug!("Joining writer and reader.");
|
debug!("Joining writer and reader.");
|
||||||
reader_guard.take()
|
reader_guard.take()
|
||||||
.expect(pos!())
|
.expect(pos!())
|
||||||
.join()
|
.join()
|
||||||
.expect(pos!());
|
.expect(pos!());
|
||||||
debug!("Successfully joined writer and reader.");
|
debug!("Successfully joined writer and reader.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,8 +185,8 @@ mod test {
|
|||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let server = Arc::new(Server::new());
|
let server = Arc::new(Server::new());
|
||||||
let serve_handle = server.spawn_with_config("localhost:0",
|
let serve_handle = server.spawn_with_config("localhost:0",
|
||||||
Config { timeout: Some(Duration::new(0, 10)) })
|
Config { timeout: Some(Duration::new(0, 10)) })
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let client: Client<(), u64, _> = Client::new(serve_handle.dialer()).unwrap();
|
let client: Client<(), u64, _> = Client::new(serve_handle.dialer()).unwrap();
|
||||||
let thread = thread::spawn(move || serve_handle.shutdown());
|
let thread = thread::spawn(move || serve_handle.shutdown());
|
||||||
info!("force_shutdown:: rpc1: {:?}", client.rpc(()));
|
info!("force_shutdown:: rpc1: {:?}", client.rpc(()));
|
||||||
@@ -197,9 +197,9 @@ mod test {
|
|||||||
fn client_failed_rpc() {
|
fn client_failed_rpc() {
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let server = Arc::new(Server::new());
|
let server = Arc::new(Server::new());
|
||||||
let serve_handle = server.spawn_with_config("localhost:0",
|
let serve_handle =
|
||||||
Config { timeout: test_timeout() })
|
server.spawn_with_config("localhost:0", Config { timeout: test_timeout() })
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let client: Arc<Client<(), u64, _>> = Arc::new(Client::new(serve_handle.dialer()).unwrap());
|
let client: Arc<Client<(), u64, _>> = Arc::new(Client::new(serve_handle.dialer()).unwrap());
|
||||||
client.rpc(()).unwrap();
|
client.rpc(()).unwrap();
|
||||||
serve_handle.shutdown();
|
serve_handle.shutdown();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer, de, ser};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
/// Packet shared between client and server.
|
/// Packet shared between client and server.
|
||||||
@@ -19,40 +19,10 @@ impl<T: Serialize> Serialize for Packet<T> {
|
|||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer
|
where S: Serializer
|
||||||
{
|
{
|
||||||
serializer.serialize_struct(PACKET,
|
let mut state = try!(serializer.serialize_struct(PACKET, 2));
|
||||||
MapVisitor {
|
try!(serializer.serialize_struct_elt(&mut state, RPC_ID, &self.rpc_id));
|
||||||
value: self,
|
try!(serializer.serialize_struct_elt(&mut state, MESSAGE, &self.message));
|
||||||
state: 0,
|
serializer.serialize_struct_end(state)
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct MapVisitor<'a, T: 'a> {
|
|
||||||
value: &'a Packet<T>,
|
|
||||||
state: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: Serialize> ser::MapVisitor for MapVisitor<'a, T> {
|
|
||||||
#[inline]
|
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
|
||||||
where S: Serializer
|
|
||||||
{
|
|
||||||
match self.state {
|
|
||||||
0 => {
|
|
||||||
self.state += 1;
|
|
||||||
Ok(Some(try!(serializer.serialize_struct_elt(RPC_ID, &self.value.rpc_id))))
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
self.state += 1;
|
|
||||||
Ok(Some(try!(serializer.serialize_struct_elt(MESSAGE, &self.value.message))))
|
|
||||||
}
|
|
||||||
_ => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn len(&self) -> Option<usize> {
|
|
||||||
Some(2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ impl<'a, S, St> ConnectionHandler<'a, S, St>
|
|||||||
scope.execute(move || Self::write(rx, write_stream));
|
scope.execute(move || Self::write(rx, write_stream));
|
||||||
loop {
|
loop {
|
||||||
match read_stream.deserialize() {
|
match read_stream.deserialize() {
|
||||||
Ok(Packet { rpc_id, message, }) => {
|
Ok(Packet { rpc_id, message }) => {
|
||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
scope.execute(move || {
|
scope.execute(move || {
|
||||||
let reply = server.serve(message);
|
let reply = server.serve(message);
|
||||||
@@ -81,7 +81,8 @@ impl<'a, S, St> ConnectionHandler<'a, S, St>
|
|||||||
|
|
||||||
fn timed_out(error_kind: io::ErrorKind) -> bool {
|
fn timed_out(error_kind: io::ErrorKind) -> bool {
|
||||||
match error_kind {
|
match error_kind {
|
||||||
io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => true,
|
io::ErrorKind::TimedOut |
|
||||||
|
io::ErrorKind::WouldBlock => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,7 +265,6 @@ pub trait Serve: Send + Sync + Sized {
|
|||||||
dialer: dialer,
|
dialer: dialer,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P, S> Serve for P
|
impl<P, S> Serve for P
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// A transport for TCP.
|
/// A transport for TCP.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct TcpTransport<A: ToSocketAddrs>(pub A);
|
pub struct TcpTransport<A: ToSocketAddrs>(pub A);
|
||||||
|
|
||||||
impl<A: ToSocketAddrs> super::Transport for TcpTransport<A> {
|
impl<A: ToSocketAddrs> super::Transport for TcpTransport<A> {
|
||||||
@@ -54,6 +55,7 @@ impl super::Stream for TcpStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Connects to a socket address.
|
/// Connects to a socket address.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct TcpDialer<A = SocketAddr>(pub A) where A: ToSocketAddrs;
|
pub struct TcpDialer<A = SocketAddr>(pub A) where A: ToSocketAddrs;
|
||||||
|
|
||||||
impl<A> super::Dialer for TcpDialer<A>
|
impl<A> super::Dialer for TcpDialer<A>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use std::time::Duration;
|
|||||||
use unix_socket::{UnixListener, UnixStream};
|
use unix_socket::{UnixListener, UnixStream};
|
||||||
|
|
||||||
/// A transport for unix sockets.
|
/// A transport for unix sockets.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct UnixTransport<P>(pub P) where P: AsRef<Path>;
|
pub struct UnixTransport<P>(pub P) where P: AsRef<Path>;
|
||||||
|
|
||||||
impl<P> super::Transport for UnixTransport<P>
|
impl<P> super::Transport for UnixTransport<P>
|
||||||
@@ -17,6 +18,7 @@ impl<P> super::Transport for UnixTransport<P>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Connects to a unix socket address.
|
/// Connects to a unix socket address.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct UnixDialer<P>(pub P) where P: AsRef<Path>;
|
pub struct UnixDialer<P>(pub P) where P: AsRef<Path>;
|
||||||
|
|
||||||
impl<P> super::Dialer for UnixDialer<P>
|
impl<P> super::Dialer for UnixDialer<P>
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ authors = ["Adam Wright <adam.austin.wright@gmail.com>", "Tim Kuehn <timothy.j.k
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tarpc = { path = "../tarpc" }
|
tarpc = { path = "../tarpc" }
|
||||||
lazy_static = "^0.1.15"
|
lazy_static = "0.2"
|
||||||
env_logger = "^0.3.2"
|
env_logger = "0.3"
|
||||||
|
|||||||
Reference in New Issue
Block a user