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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::{
    fmt::{Display, Error as FmtError, Formatter},
    io::Error as IoError,
};

use parity_scale_codec::DecodeAll;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use crate::Data;

// We allow sending up to 16MiB, that should be enough forever.
pub const MAX_DATA_SIZE: u32 = 16 * 1024 * 1024;

/// A general error when sending or receving data.
#[derive(Debug)]
pub enum Error {
    ConnectionClosed(IoError),
    DataTooLong(u32),
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        use Error::*;
        match self {
            ConnectionClosed(e) => write!(f, "connection unexpectedly closed: {e}"),
            DataTooLong(length) => write!(
                f,
                "encoded data too long - {length} bytes, the limit is {MAX_DATA_SIZE}"
            ),
        }
    }
}

/// An error when sending data.
#[derive(Debug)]
pub struct SendError(Error);

impl Display for SendError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        write!(f, "{}", self.0)
    }
}

impl From<Error> for SendError {
    fn from(e: Error) -> Self {
        SendError(e)
    }
}

/// An error when receiving data.
#[derive(Debug)]
pub enum ReceiveError {
    Error(Error),
    DataCorrupted,
}

impl Display for ReceiveError {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        use ReceiveError::*;
        match self {
            Error(e) => write!(f, "{e}"),
            DataCorrupted => write!(f, "received corrupted data"),
        }
    }
}

impl From<Error> for ReceiveError {
    fn from(e: Error) -> Self {
        ReceiveError::Error(e)
    }
}

/// Sends some data using the stream.
pub async fn send_data<S: AsyncWriteExt + Unpin, D: Data>(
    mut stream: S,
    data: D,
) -> Result<S, SendError> {
    let encoded = data.encode();
    let len = u32::try_from(encoded.len()).map_err(|_| Error::DataTooLong(u32::MAX))?;
    if len > MAX_DATA_SIZE {
        return Err(Error::DataTooLong(len).into());
    }
    let encoded_len = len.to_le_bytes().to_vec();
    stream
        .write_all(&encoded_len)
        .await
        .map_err(Error::ConnectionClosed)?;
    stream
        .write_all(&encoded)
        .await
        .map_err(Error::ConnectionClosed)?;
    Ok(stream)
}

/// Attempts to receive some data using the stream.
pub async fn receive_data<S: AsyncReadExt + Unpin, D: Data>(
    mut stream: S,
) -> Result<(S, D), ReceiveError> {
    let mut buf = [0; 4];
    stream
        .read_exact(&mut buf[..])
        .await
        .map_err(Error::ConnectionClosed)?;
    let len = u32::from_le_bytes(buf);
    if len > MAX_DATA_SIZE {
        return Err(Error::DataTooLong(len).into());
    }
    let mut buf: Vec<u8> = vec![0; len as usize];
    stream
        .read_exact(&mut buf[..])
        .await
        .map_err(Error::ConnectionClosed)?;
    let data = D::decode_all(&mut &buf[..]).map_err(|_| ReceiveError::DataCorrupted)?;
    Ok((stream, data))
}

#[cfg(test)]
mod tests {
    use tokio::io::{duplex, AsyncWriteExt};

    use super::{receive_data, send_data, Error, ReceiveError, SendError, MAX_DATA_SIZE};

    #[tokio::test]
    async fn sends_and_receives_correct_data() {
        let (sender, receiver) = duplex(4096);
        let data: Vec<i32> = vec![4, 3, 43];
        let _sender = send_data(sender, data.clone())
            .await
            .expect("data should send");
        let (_receiver, received_data) = receive_data(receiver).await.expect("should receive data");
        let received_data: Vec<i32> = received_data;
        assert_eq!(data, received_data);
    }

    #[tokio::test]
    async fn fails_to_receive_from_dropped_connection() {
        let (_, receiver) = duplex(4096);
        match receive_data::<_, i32>(receiver).await {
            Err(e) => match e {
                ReceiveError::Error(Error::ConnectionClosed(_)) => (),
                e => panic!("unexpected error: {e}"),
            },
            _ => panic!("received data from a dropped stream!"),
        }
    }

    #[tokio::test]
    async fn fails_to_send_to_dropped_connection() {
        let (sender, _) = duplex(4096);
        let data: Vec<i32> = vec![4, 3, 43];
        match send_data(sender, data.clone()).await {
            Err(e) => match e {
                SendError(Error::ConnectionClosed(_)) => (),
                e => panic!("unexpected error: {e}"),
            },
            _ => panic!("send data to a dropped stream!"),
        }
    }

    #[tokio::test]
    async fn fails_to_send_too_big_message() {
        let (sender, _) = duplex(4096);
        let data: Vec<u8> = vec![
            43;
            (MAX_DATA_SIZE + 1)
                .try_into()
                .expect("why are you running tests on a 16 bit machine? o.0")
        ];
        match send_data(sender, data.clone()).await {
            Err(e) => match e {
                SendError(Error::DataTooLong(_)) => (),
                e => panic!("unexpected error: {e}"),
            },
            _ => panic!("send data to a dropped stream!"),
        }
    }

    #[tokio::test]
    async fn fails_to_receive_too_much_data() {
        let (mut sender, receiver) = duplex(4096);
        let too_long = MAX_DATA_SIZE + 43;
        let payload = too_long.to_le_bytes().to_vec();
        sender
            .write_all(&payload)
            .await
            .expect("sending should work");
        match receive_data::<_, i32>(receiver).await {
            Err(e) => match e {
                ReceiveError::Error(Error::DataTooLong(long)) => assert_eq!(long, too_long),
                e => panic!("unexpected error: {e}"),
            },
            _ => panic!("received too long data!"),
        }
    }

    #[tokio::test]
    async fn fails_to_decode_empty_data() {
        let (mut sender, receiver) = duplex(4096);
        let payload = 0u32.to_le_bytes().to_vec();
        sender
            .write_all(&payload)
            .await
            .expect("sending should work");
        match receive_data::<_, i32>(receiver).await {
            Err(e) => match e {
                ReceiveError::DataCorrupted => (),
                e => panic!("unexpected error: {e}"),
            },
            _ => panic!("decoded no data into something?!"),
        }
    }
}