1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::{
    frame::{FlowKind, Kind},
    Frame,
};
use async_hal::io::AsyncRead;
use core::{
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};
use futures::{ready, Sink, SinkExt, Stream, StreamExt};
use pin_project_lite::pin_project;

enum State {
    Empty,
    Single {
        frame: Option<Frame>,
    },
    Consecutive {
        remaining_bytes: u16,
        remaining_frames: u8,
        index: u8,
        is_flushing: bool,
    },
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error<T, R> {
    Transmit(T),
    Receive(R),
    InvalidFrame,
    UnknownFrameKind,
    UnexpectedEOF,
}

pin_project! {
    pub struct Reader<T, E> {
        #[pin]
        transport: T,

        state: State,
        block_len: u8,
        st: u8,
        _marker: PhantomData<E>
    }
}

impl<T, E> Reader<T, E> {
    /// Create a new reader from a socket.
    pub fn new(transport: T) -> Self {
        Self {
            transport,
            state: State::Empty,
            block_len: 10,
            st: 0,
            _marker: PhantomData,
        }
    }

    /// Abort the current read.
    pub async fn abort(mut self) -> Result<(), T::Error>
    where
        T: Sink<Frame> + Unpin,
    {
        let frame = Frame::flow(FlowKind::Abort, 0, 0);
        self.transport.send(frame).await
    }
}

impl<T, E> AsyncRead for Reader<T, E>
where
    T: Sink<Frame> + Stream<Item = Result<Frame, E>>,
{
    type Error = Error<T::Error, E>;

    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context,
        buf: &mut [u8],
    ) -> Poll<Result<usize, Self::Error>> {
        let mut me = self.project();
        loop {
            match me.state {
                State::Empty => {
                    let frame = ready!(me.transport.poll_next_unpin(cx))
                        .ok_or(Error::UnexpectedEOF)?
                        .map_err(Error::Receive)?;

                    match frame.kind().ok_or(Error::UnknownFrameKind)? {
                        Kind::Single => *me.state = State::Single { frame: Some(frame) },
                        Kind::First => {
                            let data = frame.first_data();
                            *me.state = State::Consecutive {
                                remaining_bytes: frame.first_len() - data.len() as u16,
                                remaining_frames: 0,
                                index: 0,
                                is_flushing: false,
                            };
                            buf[..data.len()].copy_from_slice(data);
                            break Poll::Ready(Ok(data.len()));
                        }
                        Kind::Consecutive | Kind::Flow => {
                            break Poll::Ready(Err(Error::InvalidFrame))
                        }
                    }
                }
                State::Single { frame } => {
                    let used = if let Some(frame) = frame.take() {
                        let data = frame.single_data();
                        buf[..data.len()].copy_from_slice(data);

                        data.len()
                    } else {
                        0
                    };
                    break Poll::Ready(Ok(used));
                }
                State::Consecutive {
                    remaining_bytes,
                    remaining_frames,
                    index,
                    is_flushing,
                } => {
                    if *remaining_bytes == 0 {
                        break Poll::Ready(Ok(0));
                    }

                    while *remaining_frames == 0 {
                        if *is_flushing {
                            ready!(me.transport.poll_flush_unpin(cx)).map_err(Error::Transmit)?;

                            *remaining_frames = *me.block_len;
                            *is_flushing = false;
                        } else {
                            ready!(me.transport.poll_ready_unpin(cx)).map_err(Error::Transmit)?;
                            let frame = Frame::flow(FlowKind::Continue, *me.block_len, *me.st);
                            me.transport
                                .start_send_unpin(frame)
                                .map_err(Error::Transmit)?;
                            *is_flushing = true;
                        }
                    }

                    let frame = ready!(me.transport.poll_next_unpin(cx))
                        .ok_or(Error::UnexpectedEOF)?
                        .map_err(Error::Receive)?;
                    if frame.kind().ok_or(Error::UnknownFrameKind)? != Kind::Consecutive {
                        return Poll::Ready(Err(Error::InvalidFrame));
                    }

                    let data = frame.consecutive_data();
                    let used = core::cmp::min(data.len(), *remaining_bytes as _);
                    buf[..used].copy_from_slice(&data[..used]);

                    *index += 1;
                    *remaining_frames -= 1;

                    break Poll::Ready(Ok(used));
                }
            }
        }
    }
}