| 1 | use super::framed_read::{framed_read_2, FramedRead2}; |
| 2 | use super::framed_write::{framed_write_2, FramedWrite2}; |
| 3 | use super::fuse::Fuse; |
| 4 | use super::{Decoder, Encoder}; |
| 5 | use bytes::BytesMut; |
| 6 | use futures::io::{AsyncRead, AsyncWrite}; |
| 7 | use futures::{Sink, Stream, TryStreamExt}; |
| 8 | use pin_project::pin_project; |
| 9 | use std::marker::Unpin; |
| 10 | use std::ops::{Deref, DerefMut}; |
| 11 | use std::pin::Pin; |
| 12 | use std::task::{Context, Poll}; |
| 13 | |
| 14 | /// A unified `Stream` and `Sink` interface to an underlying I/O object, |
| 15 | /// using the `Encoder` and `Decoder` traits to encode and decode frames. |
| 16 | /// |
| 17 | /// # Example |
| 18 | /// ``` |
| 19 | /// use bytes::Bytes; |
| 20 | /// use futures::{SinkExt, TryStreamExt}; |
| 21 | /// use futures::io::Cursor; |
| 22 | /// use futures_codec::{BytesCodec, Framed}; |
| 23 | /// |
| 24 | /// # futures::executor::block_on(async move { |
| 25 | /// let cur = Cursor::new(vec![0u8; 12]); |
| 26 | /// let mut framed = Framed::new(cur, BytesCodec {}); |
| 27 | /// |
| 28 | /// // Send bytes to `buf` through the `BytesCodec` |
| 29 | /// let bytes = Bytes::from("Hello world!"); |
| 30 | /// framed.send(bytes).await?; |
| 31 | /// |
| 32 | /// // Release the I/O and codec |
| 33 | /// let (cur, _) = framed.release(); |
| 34 | /// assert_eq!(cur.get_ref(), b"Hello world!"); |
| 35 | /// # Ok::<_, std::io::Error>(()) |
| 36 | /// # }).unwrap(); |
| 37 | /// ``` |
| 38 | #[pin_project] |
| 39 | #[derive(Debug)] |
| 40 | pub struct Framed<T, U> { |
| 41 | #[pin] |
| 42 | inner: FramedRead2<FramedWrite2<Fuse<T, U>>>, |
| 43 | } |
| 44 | |
| 45 | impl<T, U> Deref for Framed<T, U> { |
| 46 | type Target = T; |
| 47 | |
| 48 | fn deref(&self) -> &T { |
| 49 | &self.inner |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl<T, U> DerefMut for Framed<T, U> { |
| 54 | fn deref_mut(&mut self) -> &mut T { |
| 55 | &mut self.inner |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<T, U> Framed<T, U> |
| 60 | where |
| 61 | T: AsyncRead + AsyncWrite, |
| 62 | U: Decoder + Encoder, |
| 63 | { |
| 64 | /// Creates a new `Framed` transport with the given codec. |
| 65 | /// A codec is a type which implements `Decoder` and `Encoder`. |
| 66 | pub fn new(inner: T, codec: U) -> Self { |
| 67 | Self { |
| 68 | inner: framed_read_2(framed_write_2(Fuse::new(inner, codec))), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Release the I/O and Codec |
| 73 | pub fn release(self: Self) -> (T, U) { |
| 74 | let fuse = self.inner.release().release(); |
| 75 | (fuse.t, fuse.u) |
| 76 | } |
| 77 | |
| 78 | /// Consumes the `Framed`, returning its underlying I/O stream. |
| 79 | /// |
| 80 | /// Note that care should be taken to not tamper with the underlying stream |
| 81 | /// of data coming in as it may corrupt the stream of frames otherwise |
| 82 | /// being worked with. |
| 83 | pub fn into_inner(self) -> T { |
| 84 | self.release().0 |
| 85 | } |
| 86 | |
| 87 | /// Returns a reference to the underlying codec wrapped by |
| 88 | /// `Framed`. |
| 89 | /// |
| 90 | /// Note that care should be taken to not tamper with the underlying codec |
| 91 | /// as it may corrupt the stream of frames otherwise being worked with. |
| 92 | pub fn codec(&self) -> &U { |
| 93 | &self.inner.u |
| 94 | } |
| 95 | |
| 96 | /// Returns a mutable reference to the underlying codec wrapped by |
| 97 | /// `Framed`. |
| 98 | /// |
| 99 | /// Note that care should be taken to not tamper with the underlying codec |
| 100 | /// as it may corrupt the stream of frames otherwise being worked with. |
| 101 | pub fn codec_mut(&mut self) -> &mut U { |
| 102 | &mut self.inner.u |
| 103 | } |
| 104 | |
| 105 | /// Returns a reference to the read buffer. |
| 106 | pub fn read_buffer(&self) -> &BytesMut { |
| 107 | self.inner.buffer() |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl<T, U> Stream for Framed<T, U> |
| 112 | where |
| 113 | T: AsyncRead + Unpin, |
| 114 | U: Decoder, |
| 115 | { |
| 116 | type Item = Result<U::Item, U::Error>; |
| 117 | |
| 118 | fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 119 | self.inner.try_poll_next_unpin(cx) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl<T, U> Sink<U::Item> for Framed<T, U> |
| 124 | where |
| 125 | T: AsyncWrite + Unpin, |
| 126 | U: Encoder, |
| 127 | { |
| 128 | type Error = U::Error; |
| 129 | |
| 130 | fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> { |
| 131 | self.project().inner.poll_ready(cx) |
| 132 | } |
| 133 | fn start_send(self: Pin<&mut Self>, item: U::Item) -> Result<(), Self::Error> { |
| 134 | self.project().inner.start_send(item) |
| 135 | } |
| 136 | fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> { |
| 137 | self.project().inner.poll_flush(cx) |
| 138 | } |
| 139 | fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> { |
| 140 | self.project().inner.poll_close(cx) |
| 141 | } |
| 142 | } |