|
| 1 | +use std::{ |
| 2 | + future::Future, |
| 3 | + io, mem, |
| 4 | + pin::Pin, |
| 5 | + task::{Context, Poll}, |
| 6 | +}; |
| 7 | + |
| 8 | +use tokio::io::{AsyncRead, ReadBuf}; |
| 9 | + |
| 10 | +use crate::{status, DataStream, FtpStream}; |
| 11 | + |
| 12 | +pub struct FileReader<'a> { |
| 13 | + state: State<'a>, |
| 14 | +} |
| 15 | + |
| 16 | +enum State<'a> { |
| 17 | + Stream { |
| 18 | + data_stream: DataStream, |
| 19 | + ftp_stream: &'a mut FtpStream, |
| 20 | + }, |
| 21 | + FinalRead(Pin<Box<dyn 'a + Future<Output = io::Result<()>>>>), |
| 22 | + Finished, |
| 23 | +} |
| 24 | + |
| 25 | +impl FileReader<'_> { |
| 26 | + pub(crate) fn new(data_stream: DataStream, ftp_stream: &mut FtpStream) -> FileReader { |
| 27 | + FileReader { |
| 28 | + state: State::Stream { |
| 29 | + data_stream, |
| 30 | + ftp_stream, |
| 31 | + }, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl AsyncRead for FileReader<'_> { |
| 37 | + fn poll_read( |
| 38 | + mut self: Pin<&mut Self>, |
| 39 | + cx: &mut Context<'_>, |
| 40 | + buf: &mut ReadBuf<'_>, |
| 41 | + ) -> Poll<io::Result<()>> { |
| 42 | + let bytes_read_before = buf.filled().len(); |
| 43 | + let (state, result) = match mem::replace(&mut self.state, State::Finished) { |
| 44 | + State::Stream { |
| 45 | + mut data_stream, |
| 46 | + ftp_stream, |
| 47 | + } => match Pin::new(&mut data_stream).poll_read(cx, buf) { |
| 48 | + Poll::Ready(result) => { |
| 49 | + let bytes_read_after = buf.filled().len(); |
| 50 | + if bytes_read_after == bytes_read_before { |
| 51 | + // finished reading the file, wait for a status message from the server |
| 52 | + let mut status_fut = Box::pin(async move { |
| 53 | + ftp_stream |
| 54 | + .read_response_in(&[ |
| 55 | + status::CLOSING_DATA_CONNECTION, |
| 56 | + status::REQUESTED_FILE_ACTION_OK, |
| 57 | + ]) |
| 58 | + .await |
| 59 | + .map(|_| ()) |
| 60 | + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) |
| 61 | + .and(result) |
| 62 | + }); |
| 63 | + match Pin::new(&mut status_fut).poll(cx) { |
| 64 | + Poll::Ready(r) => (State::Finished, Poll::Ready(r)), |
| 65 | + Poll::Pending => (State::FinalRead(status_fut), Poll::Pending), |
| 66 | + } |
| 67 | + } else { |
| 68 | + ( |
| 69 | + State::Stream { |
| 70 | + data_stream, |
| 71 | + ftp_stream, |
| 72 | + }, |
| 73 | + Poll::Ready(result), |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + Poll::Pending => ( |
| 78 | + State::Stream { |
| 79 | + data_stream, |
| 80 | + ftp_stream, |
| 81 | + }, |
| 82 | + Poll::Pending, |
| 83 | + ), |
| 84 | + }, |
| 85 | + State::FinalRead(mut status_fut) => match Pin::new(&mut status_fut).poll(cx) { |
| 86 | + Poll::Ready(r) => (State::Finished, Poll::Ready(r)), |
| 87 | + Poll::Pending => (State::FinalRead(status_fut), Poll::Pending), |
| 88 | + }, |
| 89 | + State::Finished => panic!("poll called on finished FileReader"), |
| 90 | + }; |
| 91 | + |
| 92 | + self.state = state; |
| 93 | + result |
| 94 | + } |
| 95 | +} |
0 commit comments