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
164
165
166
167
168
169
170
171
172
173
174
175
use std::{
    io,
    pin::Pin,
    task::{Context, Poll},
};

use crate::{error::NetworkError, AsyncSocket, UniversalStream};
use futures_util::{Future, Sink, SinkExt, Stream, StreamExt};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

pub struct AsyncToStream {
    socket: Box<dyn AsyncSocket>,
    buffer: Option<(usize, Vec<u8>)>,
}

impl AsyncToStream {
    pub fn new(socket: impl AsyncSocket) -> Self {
        Self {
            socket: Box::new(socket),
            buffer: None,
        }
    }
}

impl Stream for AsyncToStream {
    type Item = Result<Vec<u8>, NetworkError>;

    fn poll_next(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        let mut buf = [0u8; 65536];
        let mut buffer = ReadBuf::new(&mut buf);
        match Pin::new(&mut self.socket).poll_read(cx, &mut buffer)? {
            Poll::Ready(_) => {
                if buffer.filled().is_empty() {
                    Poll::Ready(None)
                } else {
                    Poll::Ready(Some(Ok(buffer.filled().to_vec())))
                }
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

impl Sink<Vec<u8>> for AsyncToStream {
    type Error = NetworkError;

    fn poll_ready(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        if let Some((mut len, buffer)) = self.buffer.take() {
            loop {
                len = match Pin::new(&mut self.socket).poll_write(cx, &buffer)? {
                    Poll::Ready(written) => written,
                    Poll::Pending => return Poll::Pending,
                };
                if len == buffer.len() {
                    break;
                }
            }
        }

        Poll::Ready(Ok(()))
    }

    fn start_send(mut self: Pin<&mut Self>, item: Vec<u8>) -> Result<(), Self::Error> {
        self.buffer = Some((0, item));
        Ok(())
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let _ = Pin::new(&mut self).poll_ready(cx)?;
        Pin::new(&mut self.socket)
            .poll_flush(cx)
            .map_err(|e| e.into())
    }

    fn poll_close(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<(), Self::Error>> {
        let _ = Pin::new(&mut self).poll_ready(cx)?;
        Pin::new(&mut self.socket)
            .poll_shutdown(cx)
            .map_err(|e| e.into())
    }
}

pub struct StreamToAsync {
    stream: Box<dyn UniversalStream<Vec<u8>, NetworkError>>,
    remaining_bytes: Option<Vec<u8>>,
}
impl StreamToAsync {
    pub fn new(socket: impl UniversalStream<Vec<u8>, NetworkError>) -> Self {
        Self {
            stream: Box::new(socket),
            remaining_bytes: None,
        }
    }
}
impl AsyncRead for StreamToAsync {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        loop {
            if let Some(mut remaining_buf) = self.remaining_bytes.take() {
                if buf.remaining() < remaining_buf.len() {
                    self.remaining_bytes = Some(remaining_buf.split_off(buf.remaining()));
                    buf.put_slice(&remaining_buf);
                } else {
                    buf.put_slice(&remaining_buf);
                    self.remaining_bytes = None;
                }
                return Poll::Ready(Ok(()));
            }

            match self.stream.poll_next_unpin(cx) {
                Poll::Ready(Some(Ok(d))) => {
                    self.remaining_bytes = Some(d);
                    continue;
                }
                Poll::Ready(Some(Err(e))) => {
                    return Poll::Ready(Err(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        e.to_string(),
                    )))
                }
                Poll::Ready(None) => return Poll::Ready(Ok(())),
                Poll::Pending => return Poll::Pending,
            };
        }
    }
}

impl AsyncWrite for StreamToAsync {
    fn poll_write(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, std::io::Error>> {
        match Pin::new(&mut self.stream.send(buf.to_vec()))
            .poll(cx)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?
        {
            Poll::Ready(_) => Poll::Ready(Ok(buf.len())),
            Poll::Pending => Poll::Pending,
        }
    }

    fn poll_flush(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), io::Error>> {
        self.stream
            .poll_flush_unpin(cx)
            .map_err(|_| io::Error::from(io::ErrorKind::UnexpectedEof))
    }

    fn poll_shutdown(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<(), io::Error>> {
        self.stream
            .poll_close_unpin(cx)
            .map_err(|_| io::Error::from(io::ErrorKind::UnexpectedEof))
    }
}