mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-28 23:27:25 +01:00
Rename module framed -> protocol, and clarify some type parameters.
This commit is contained in:
@@ -3,9 +3,10 @@
|
||||
// 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.
|
||||
|
||||
use {WireError, framed};
|
||||
use WireError;
|
||||
use bincode::serde::DeserializeError;
|
||||
use futures::{self, Future};
|
||||
use protocol::Proto;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
@@ -18,7 +19,7 @@ type WireResponse<Resp, E> = Result<Result<Resp, WireError<E>>, DeserializeError
|
||||
type ResponseFuture<Req, Resp, E> = futures::Map<<BindClient<Req, Resp, E> as Service>::Future,
|
||||
fn(WireResponse<Resp, E>) -> Result<Resp, ::Error<E>>>;
|
||||
type BindClient<Req, Resp, E> =
|
||||
<framed::Proto<Req, Result<Resp, WireError<E>>> as ProtoBindClient<Multiplex, TcpStream>>::BindClient;
|
||||
<Proto<Req, Result<Resp, WireError<E>>> as ProtoBindClient<Multiplex, TcpStream>>::BindClient;
|
||||
|
||||
/// A client that impls `tokio_service::Service` that writes and reads bytes.
|
||||
///
|
||||
@@ -81,8 +82,9 @@ impl<Req, Resp, E> fmt::Debug for Client<Req, Resp, E>
|
||||
|
||||
/// Exposes a trait for connecting asynchronously to servers.
|
||||
pub mod future {
|
||||
use {REMOTE, framed};
|
||||
use REMOTE;
|
||||
use futures::{self, Async, Future};
|
||||
use protocol::Proto;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
use std::marker::PhantomData;
|
||||
@@ -175,7 +177,7 @@ pub mod future {
|
||||
type Output = Client<Req, Resp, E>;
|
||||
|
||||
extern "rust-call" fn call_once(self, (tcp,): (TcpStream,)) -> Client<Req, Resp, E> {
|
||||
Client::new(framed::Proto::new().bind_client(self.0, tcp))
|
||||
Client::new(Proto::new().bind_client(self.0, tcp))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +195,7 @@ pub mod future {
|
||||
remote.spawn(move |handle| {
|
||||
let handle2 = handle.clone();
|
||||
TcpStream::connect(&addr, handle)
|
||||
.map(move |tcp| Client::new(framed::Proto::new().bind_client(&handle2, tcp)))
|
||||
.map(move |tcp| Client::new(Proto::new().bind_client(&handle2, tcp)))
|
||||
.then(move |result| {
|
||||
tx.complete(result);
|
||||
Ok(())
|
||||
|
||||
@@ -109,9 +109,9 @@ mod macros;
|
||||
mod client;
|
||||
/// Provides the base server boilerplate used by service implementations.
|
||||
mod server;
|
||||
/// Provides an implementation of `FramedIo` that implements the tarpc protocol.
|
||||
/// The tarpc protocol is defined by the `FramedIo` implementation.
|
||||
mod framed;
|
||||
/// Provides implementations of `ClientProto` and `ServerProto` that implement the tarpc protocol.
|
||||
/// The tarpc protocol is a length-delimited, bincode-serialized payload.
|
||||
mod protocol;
|
||||
/// Provides a few different error types.
|
||||
mod errors;
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ use tokio_proto::multiplex::{ClientProto, ServerProto};
|
||||
use util::Debugger;
|
||||
|
||||
// `T` is the type that `Codec` parses.
|
||||
pub struct Codec<Req, Resp> {
|
||||
pub struct Codec<Encode, Decode> {
|
||||
state: CodecState,
|
||||
_phantom_data: PhantomData<(Req, Resp)>,
|
||||
_phantom_data: PhantomData<(Encode, Decode)>,
|
||||
}
|
||||
|
||||
enum CodecState {
|
||||
@@ -26,7 +26,7 @@ enum CodecState {
|
||||
Payload { id: u64, len: u64 },
|
||||
}
|
||||
|
||||
impl<Req, Resp> Codec<Req, Resp> {
|
||||
impl<Encode, Decode> Codec<Encode, Decode> {
|
||||
fn new() -> Self {
|
||||
Codec {
|
||||
state: CodecState::Id,
|
||||
@@ -35,12 +35,12 @@ impl<Req, Resp> Codec<Req, Resp> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Req, Resp> tokio_core::io::Codec for Codec<Req, Resp>
|
||||
where Req: serde::Deserialize,
|
||||
Resp: serde::Serialize,
|
||||
impl<Encode, Decode> tokio_core::io::Codec for Codec<Encode, Decode>
|
||||
where Encode: serde::Serialize,
|
||||
Decode: serde::Deserialize,
|
||||
{
|
||||
type Out = (RequestId, Resp);
|
||||
type In = (RequestId, Result<Req, bincode::DeserializeError>);
|
||||
type Out = (RequestId, Encode);
|
||||
type In = (RequestId, Result<Decode, bincode::DeserializeError>);
|
||||
|
||||
fn encode(&mut self, (id, message): Self::Out, buf: &mut Vec<u8>) -> io::Result<()> {
|
||||
buf.write_u64::<BigEndian>(id).unwrap();
|
||||
@@ -105,24 +105,24 @@ impl<Req, Resp> tokio_core::io::Codec for Codec<Req, Resp>
|
||||
}
|
||||
|
||||
/// Implements the `multiplex::ServerProto` trait.
|
||||
pub struct Proto<Req, Resp>(PhantomData<(Req, Resp)>);
|
||||
pub struct Proto<Encode, Decode>(PhantomData<(Encode, Decode)>);
|
||||
|
||||
impl<Req, Resp> Proto<Req, Resp> {
|
||||
impl<Encode, Decode> Proto<Encode, Decode> {
|
||||
/// Returns a new `Proto`.
|
||||
pub fn new() -> Self {
|
||||
Proto(PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Req, Resp> ServerProto<T> for Proto<Req, Resp>
|
||||
impl<T, Encode, Decode> ServerProto<T> for Proto<Encode, Decode>
|
||||
where T: Io + 'static,
|
||||
Req: serde::Deserialize + 'static,
|
||||
Resp: serde::Serialize + 'static,
|
||||
Encode: serde::Serialize + 'static,
|
||||
Decode: serde::Deserialize + 'static,
|
||||
{
|
||||
type Response = Resp;
|
||||
type Request = Result<Req, bincode::DeserializeError>;
|
||||
type Response = Encode;
|
||||
type Request = Result<Decode, bincode::DeserializeError>;
|
||||
type Error = io::Error;
|
||||
type Transport = Framed<T, Codec<Req, Resp>>;
|
||||
type Transport = Framed<T, Codec<Encode, Decode>>;
|
||||
type BindTransport = Result<Self::Transport, io::Error>;
|
||||
|
||||
fn bind_transport(&self, io: T) -> Self::BindTransport {
|
||||
@@ -130,15 +130,15 @@ impl<T, Req, Resp> ServerProto<T> for Proto<Req, Resp>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Req, Resp> ClientProto<T> for Proto<Req, Resp>
|
||||
impl<T, Encode, Decode> ClientProto<T> for Proto<Encode, Decode>
|
||||
where T: Io + 'static,
|
||||
Req: serde::Serialize + 'static,
|
||||
Resp: serde::Deserialize + 'static,
|
||||
Encode: serde::Serialize + 'static,
|
||||
Decode: serde::Deserialize + 'static,
|
||||
{
|
||||
type Response = Result<Resp, bincode::DeserializeError>;
|
||||
type Request = Req;
|
||||
type Response = Result<Decode, bincode::DeserializeError>;
|
||||
type Request = Encode;
|
||||
type Error = io::Error;
|
||||
type Transport = Framed<T, Codec<Resp, Req>>;
|
||||
type Transport = Framed<T, Codec<Encode, Decode>>;
|
||||
type BindTransport = Result<Self::Transport, io::Error>;
|
||||
|
||||
fn bind_transport(&self, io: T) -> Self::BindTransport {
|
||||
@@ -6,7 +6,7 @@
|
||||
use {REMOTE, net2};
|
||||
use bincode::serde::DeserializeError;
|
||||
use errors::WireError;
|
||||
use framed::Proto;
|
||||
use protocol::Proto;
|
||||
use futures::{self, Async, Future, Stream};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
|
||||
Reference in New Issue
Block a user