| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | use bytes::Bytes; |
| | use futures::{channel::oneshot, prelude::*, ready}; |
| |
|
| | use std::{ |
| | io, |
| | pin::Pin, |
| | task::{Context, Poll}, |
| | }; |
| |
|
| | use crate::proto::{Flag, Message}; |
| | use crate::{ |
| | stream::drop_listener::GracefullyClosed, |
| | stream::framed_dc::FramedDc, |
| | stream::state::{Closing, State}, |
| | }; |
| |
|
| | mod drop_listener; |
| | mod framed_dc; |
| | mod state; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | pub const MAX_MSG_LEN: usize = 16 * 1024; |
| | |
| | const VARINT_LEN: usize = 2; |
| | |
| | const PROTO_OVERHEAD: usize = 5; |
| | |
| | const MAX_DATA_LEN: usize = MAX_MSG_LEN - VARINT_LEN - PROTO_OVERHEAD; |
| |
|
| | pub use drop_listener::DropListener; |
| | |
| | |
| | |
| | |
| | pub struct Stream<T> { |
| | io: FramedDc<T>, |
| | state: State, |
| | read_buffer: Bytes, |
| | |
| | drop_notifier: Option<oneshot::Sender<GracefullyClosed>>, |
| | } |
| |
|
| | impl<T> Stream<T> |
| | where |
| | T: AsyncRead + AsyncWrite + Unpin + Clone, |
| | { |
| | |
| | pub fn new(data_channel: T) -> (Self, DropListener<T>) { |
| | let (sender, receiver) = oneshot::channel(); |
| |
|
| | let stream = Self { |
| | io: framed_dc::new(data_channel.clone()), |
| | state: State::Open, |
| | read_buffer: Bytes::default(), |
| | drop_notifier: Some(sender), |
| | }; |
| | let listener = DropListener::new(framed_dc::new(data_channel), receiver); |
| |
|
| | (stream, listener) |
| | } |
| |
|
| | |
| | pub fn poll_close_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| | loop { |
| | match self.state.close_read_barrier()? { |
| | Some(Closing::Requested) => { |
| | ready!(self.io.poll_ready_unpin(cx))?; |
| |
|
| | self.io.start_send_unpin(Message { |
| | flag: Some(Flag::STOP_SENDING), |
| | message: None, |
| | })?; |
| | self.state.close_read_message_sent(); |
| |
|
| | continue; |
| | } |
| | Some(Closing::MessageSent) => { |
| | ready!(self.io.poll_flush_unpin(cx))?; |
| |
|
| | self.state.read_closed(); |
| |
|
| | return Poll::Ready(Ok(())); |
| | } |
| | None => return Poll::Ready(Ok(())), |
| | } |
| | } |
| | } |
| | } |
| |
|
| | impl<T> AsyncRead for Stream<T> |
| | where |
| | T: AsyncRead + AsyncWrite + Unpin, |
| | { |
| | fn poll_read( |
| | mut self: Pin<&mut Self>, |
| | cx: &mut Context<'_>, |
| | buf: &mut [u8], |
| | ) -> Poll<io::Result<usize>> { |
| | loop { |
| | self.state.read_barrier()?; |
| |
|
| | if !self.read_buffer.is_empty() { |
| | let n = std::cmp::min(self.read_buffer.len(), buf.len()); |
| | let data = self.read_buffer.split_to(n); |
| | buf[0..n].copy_from_slice(&data[..]); |
| |
|
| | return Poll::Ready(Ok(n)); |
| | } |
| |
|
| | let Self { |
| | read_buffer, |
| | io, |
| | state, |
| | .. |
| | } = &mut *self; |
| |
|
| | match ready!(io_poll_next(io, cx))? { |
| | Some((flag, message)) => { |
| | if let Some(flag) = flag { |
| | state.handle_inbound_flag(flag, read_buffer); |
| | } |
| |
|
| | debug_assert!(read_buffer.is_empty()); |
| | match message { |
| | Some(msg) if !msg.is_empty() => { |
| | *read_buffer = msg.into(); |
| | } |
| | _ => { |
| | tracing::debug!("poll_read buffer is empty, received None"); |
| | return Poll::Ready(Ok(0)); |
| | } |
| | } |
| | } |
| | None => { |
| | state.handle_inbound_flag(Flag::FIN, read_buffer); |
| | return Poll::Ready(Ok(0)); |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | impl<T> AsyncWrite for Stream<T> |
| | where |
| | T: AsyncRead + AsyncWrite + Unpin, |
| | { |
| | fn poll_write( |
| | mut self: Pin<&mut Self>, |
| | cx: &mut Context<'_>, |
| | buf: &[u8], |
| | ) -> Poll<io::Result<usize>> { |
| | while self.state.read_flags_in_async_write() { |
| | |
| | |
| |
|
| | let Self { |
| | read_buffer, |
| | io, |
| | state, |
| | .. |
| | } = &mut *self; |
| |
|
| | match io_poll_next(io, cx)? { |
| | Poll::Ready(Some((Some(flag), message))) => { |
| | |
| | drop(message); |
| | |
| | state.handle_inbound_flag(flag, read_buffer) |
| | } |
| | Poll::Ready(Some((None, message))) => drop(message), |
| | Poll::Ready(None) | Poll::Pending => break, |
| | } |
| | } |
| |
|
| | self.state.write_barrier()?; |
| |
|
| | ready!(self.io.poll_ready_unpin(cx))?; |
| |
|
| | let n = usize::min(buf.len(), MAX_DATA_LEN); |
| |
|
| | Pin::new(&mut self.io).start_send(Message { |
| | flag: None, |
| | message: Some(buf[0..n].into()), |
| | })?; |
| |
|
| | Poll::Ready(Ok(n)) |
| | } |
| |
|
| | fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| | self.io.poll_flush_unpin(cx).map_err(Into::into) |
| | } |
| |
|
| | fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| | loop { |
| | match self.state.close_write_barrier()? { |
| | Some(Closing::Requested) => { |
| | ready!(self.io.poll_ready_unpin(cx))?; |
| |
|
| | self.io.start_send_unpin(Message { |
| | flag: Some(Flag::FIN), |
| | message: None, |
| | })?; |
| | self.state.close_write_message_sent(); |
| |
|
| | continue; |
| | } |
| | Some(Closing::MessageSent) => { |
| | ready!(self.io.poll_flush_unpin(cx))?; |
| |
|
| | self.state.write_closed(); |
| | let _ = self |
| | .drop_notifier |
| | .take() |
| | .expect("to not close twice") |
| | .send(GracefullyClosed {}); |
| |
|
| | return Poll::Ready(Ok(())); |
| | } |
| | None => return Poll::Ready(Ok(())), |
| | } |
| | } |
| | } |
| | } |
| |
|
| | fn io_poll_next<T>( |
| | io: &mut FramedDc<T>, |
| | cx: &mut Context<'_>, |
| | ) -> Poll<io::Result<Option<(Option<Flag>, Option<Vec<u8>>)>>> |
| | where |
| | T: AsyncRead + AsyncWrite + Unpin, |
| | { |
| | match ready!(io.poll_next_unpin(cx)) |
| | .transpose() |
| | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))? |
| | { |
| | Some(Message { flag, message }) => Poll::Ready(Ok(Some((flag, message)))), |
| | None => Poll::Ready(Ok(None)), |
| | } |
| | } |
| |
|
| | #[cfg(test)] |
| | mod tests { |
| | use super::*; |
| | use crate::stream::framed_dc::codec; |
| | use asynchronous_codec::Encoder; |
| | use bytes::BytesMut; |
| |
|
| | #[test] |
| | fn max_data_len() { |
| | |
| | let message = [0; MAX_DATA_LEN]; |
| |
|
| | let protobuf = Message { |
| | flag: Some(Flag::FIN), |
| | message: Some(message.to_vec()), |
| | }; |
| |
|
| | let mut codec = codec(); |
| |
|
| | let mut dst = BytesMut::new(); |
| | codec.encode(protobuf, &mut dst).unwrap(); |
| |
|
| | |
| | |
| | assert_eq!(dst.len(), MAX_MSG_LEN); |
| | } |
| | } |
| |
|