tokio_proto/streaming/multiplex/
mod.rs

1//! Pipelined, multiplexed protocols.
2//!
3//! See the crate-level docs for an overview.
4
5use std::io;
6use futures::{Stream, Sink, Async};
7use tokio_core::io as old_io;
8use tokio_io as new_io;
9
10mod frame_buf;
11
12mod client;
13pub use self::client::ClientProto;
14
15mod server;
16pub use self::server::ServerProto;
17
18mod frame;
19pub use self::frame::Frame;
20
21
22pub mod advanced;
23
24/// Identifies a request / response thread
25pub type RequestId = u64;
26
27/// A marker used to flag protocols as being streaming and multiplexed.
28///
29/// This is an implementation detail; to actually implement a protocol,
30/// implement the `ClientProto` or `ServerProto` traits in this module.
31#[derive(Debug)]
32pub struct StreamingMultiplex<B>(B);
33
34/// Additional transport details relevant to streaming, multiplexed protocols.
35///
36/// All methods added in this trait have default implementations.
37pub trait Transport<ReadBody>: 'static +
38    Stream<Error = io::Error> +
39    Sink<SinkError = io::Error>
40{
41    /// Allow the transport to do miscellaneous work (e.g., sending ping-pong
42    /// messages) that is not directly connected to sending or receiving frames.
43    ///
44    /// This method should be called every time the task using the transport is
45    /// executing.
46    fn tick(&mut self) {}
47
48    /// Cancel interest in the exchange identified by RequestId
49    fn cancel(&mut self, request_id: RequestId) -> io::Result<()> {
50        drop(request_id);
51        Ok(())
52    }
53
54    /// Tests to see if this I/O object may accept a body frame for the given
55    /// request ID
56    fn poll_write_body(&mut self, id: RequestId) -> Async<()> {
57        drop(id);
58        Async::Ready(())
59    }
60
61    /// Invoked before the multiplexer dispatches the body chunk to the body
62    /// stream.
63    fn dispatching_body(&mut self, id: RequestId, body: &ReadBody) {
64        drop(id);
65        drop(body);
66    }
67}
68
69impl<T, C, ReadBody> Transport<ReadBody> for old_io::Framed<T,C>
70    where T: old_io::Io + 'static,
71          C: old_io::Codec + 'static,
72{}
73
74impl<T, C, ReadBody> Transport<ReadBody> for new_io::codec::Framed<T,C>
75    where T: new_io::AsyncRead + new_io::AsyncWrite + 'static,
76          C: new_io::codec::Encoder<Error=io::Error> +
77                new_io::codec::Decoder<Error=io::Error> + 'static,
78{}