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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/// A framed codec that uses bincode to serialize and deserialize messages.
use std::{any::type_name, fmt::Debug, io::Cursor};

use bincode::Options;
use bytes::{Buf, BufMut, BytesMut};
use color_eyre::{eyre::Context, Result};
use futures::{Sink, Stream};
use serde::{de::DeserializeOwned, Serialize};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::{Decoder, Encoder, FramedRead, FramedWrite};
use tracing::{instrument, trace, warn};

use crate::Envelope;

pub type EnvelopeStream<R: AsyncRead> = impl Stream<Item = Result<Envelope>>;
pub type EnvelopeSink<W: AsyncWrite> = impl Sink<Envelope, Error = color_eyre::eyre::Error>;

pub type EnvelopeStreamWithOption<R: AsyncRead, O: Options + Copy> =
    impl Stream<Item = Result<Envelope>>;
pub type EnvelopeSinkWithOption<W: AsyncWrite, O: Options + Copy> =
    impl Sink<Envelope, Error = color_eyre::eyre::Error>;

pub use bincode_option_mod::{bincode_option, BincodeOptions};

/// Workaround for rust resolving `BincodeOptions` to two different types
mod bincode_option_mod {
    use bincode::{DefaultOptions, Options};

    pub type BincodeOptions = impl Options + Copy;

    #[inline(always)]
    pub fn bincode_option() -> BincodeOptions {
        DefaultOptions::new()
            .with_fixint_encoding()
            .with_little_endian()
            .with_limit(1 << 12)
    }
}

/// Helper function to construct a pair of stream and sink with
/// codec deserialize to [`Envelope`] from given pair of reader and writer.
pub fn adapt<R, W>(stream: (R, W)) -> (EnvelopeStream<R>, EnvelopeSink<W>)
where
    R: AsyncRead,
    W: AsyncWrite,
{
    let (r, w) = stream;
    let codec = SerdeBincodeCodec::new();
    let stream = FramedRead::new(r, codec);
    let sink = FramedWrite::new(w, codec);
    (stream, sink)
}

/// Helper function to construct a pair of stream and sink with
/// codec deserialize to [`Envelope`] from given pair of reader and writer. This
/// is like [`adapt`] but with a custom bincode option.
pub fn adapt_with_option<R, W, O>(
    stream: (R, W),
    option: O,
) -> (EnvelopeStreamWithOption<R, O>, EnvelopeSinkWithOption<W, O>)
where
    R: AsyncRead,
    W: AsyncWrite,
    O: Options + Copy,
{
    let (r, w) = stream;
    let codec = SerdeBincodeCodec::with_option(option);
    let stream = FramedRead::new(r, codec);
    let sink = FramedWrite::new(w, codec);
    (stream, sink)
}

/// A codec that uses consecutive bincode to serialize and deserialize
/// messages.
#[must_use]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SerdeBincodeCodec<T, O> {
    option: O,
    _marker: std::marker::PhantomData<T>,
}

impl<T, O> Clone for SerdeBincodeCodec<T, O>
where
    O: Clone,
{
    fn clone(&self) -> Self {
        Self {
            option: self.option.clone(),
            _marker: std::marker::PhantomData,
        }
    }
}

impl<T, O> Copy for SerdeBincodeCodec<T, O> where O: Copy {}

impl<T> SerdeBincodeCodec<T, BincodeOptions> {
    pub fn new() -> Self {
        Self::with_option(bincode_option())
    }
}

impl<T, O> SerdeBincodeCodec<T, O> {
    pub fn with_option(option: O) -> Self {
        Self {
            option,
            _marker: std::marker::PhantomData,
        }
    }
}

impl<T> Default for SerdeBincodeCodec<T, BincodeOptions> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Serialize, O: Options + Clone> Encoder<T> for SerdeBincodeCodec<T, O> {
    type Error = color_eyre::eyre::Error;

    fn encode(&mut self, item: T, dst: &mut BytesMut) -> std::result::Result<(), Self::Error> {
        let actual_size = self.option.clone().serialized_size(&item)?;
        dst.reserve(actual_size.try_into().expect("Message too large"));

        self.option
            .clone()
            .serialize_into(dst.writer(), &item)
            .wrap_err_with(|| format!("Failed to serialize `{}`", type_name::<T>()))?;

        Ok(())
    }
}

impl<T: DeserializeOwned + Debug, O: Options + Clone> Decoder for SerdeBincodeCodec<T, O> {
    type Error = color_eyre::eyre::Error;
    type Item = T;

    fn decode(
        &mut self,
        src: &mut BytesMut,
    ) -> std::result::Result<Option<Self::Item>, Self::Error> {
        try_decode(src, self.option.clone())
            .wrap_err_with(|| format!("Failed to deserialize `{}`", type_name::<T>()))
    }
}

/// Try to decode a message from the given buffer and update buffer's cursor if
/// bytes are filled. Otherwise, this will return a `Ok(None)` indicating that
/// the buffer is not filled yet and leave the buffer unchanged. However if
/// other errors happen, this will return a `Err` indicating that the buffer is
/// corrupted.
#[instrument(level = "trace", skip(data, option), fields(bytes = data.chunk().len()))]
pub fn try_decode<T: DeserializeOwned + Debug>(
    data: &mut impl Buf,
    option: impl Options,
) -> Result<Option<T>, bincode::Error> {
    if data.chunk().is_empty() {
        return Ok(None);
    }
    let mut cur = Cursor::new(data.chunk());

    let res = option.deserialize_from::<_, T>(&mut cur);

    trace!("Read {} bytes", cur.position());

    match res {
        Ok(val) => {
            data.advance(cur.position() as usize);

            trace!(?val, "Decoded");

            Ok(Some(val))
        }

        Err(e) => match *e {
            // Buffer is not filled (yet), not an error. Leave the cursor untouched so that
            // remaining bytes can be used in the next decode attempt.
            bincode::ErrorKind::Io(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => Ok(None),
            _ => Err(e),
        },
    }
}

#[test]
fn test_codec() {
    use serde::Deserialize;
    use tap::Pipe;
    use tracing::info;

    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .try_init()
        .pipe(drop);

    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
    struct A {
        a: String,
        num: u32,
    }
    let a = A {
        a: "hello\n\n123".to_string(),
        num: 10,
    };

    let mut enc = SerdeBincodeCodec::<A, _>::new();
    let mut w = BytesMut::new();

    info!("Encoding");
    enc.encode(a, &mut w).unwrap();

    info!("{:#?}", &w[..]);
    info!("Decoding");
    let a2 = enc.decode(&mut w).unwrap();
    info!("{a2:#?}");

    // assert_eq!(a, a2);
}

#[tokio::test]
async fn test_framed() -> Result<()> {
    use futures::{SinkExt, StreamExt};
    use tap::Pipe;
    use tracing::info;
    use uuid7::uuid7;

    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .try_init()
        .pipe(drop);

    let enc = SerdeBincodeCodec::<Envelope, _>::new();
    let mut w = vec![];

    let a = Envelope {
        addr: "127.0.0.1:114".parse().unwrap(),
        body: crate::Message::Swim(vec![1, 1, 4, 5, 1, 4].into()),
        id: uuid7(),
        topic: "test".to_string(),
    };

    let b = Envelope {
        addr: "127.0.0.2:514".parse().unwrap(),
        body: crate::Message::Swim(vec![1, 9, 1, 9, 8, 1, 0].into()),
        id: uuid7(),
        topic: "test2".to_string(),
    };

    {
        let mut w = FramedWrite::new(&mut w, enc);
        w.send(a.clone()).await?;
        w.send(b.clone()).await?;
    }
    info!("Written: {w:?}");
    let mut r = FramedRead::new(&w[..], enc);

    assert_eq!(r.next().await.unwrap()?, a);
    assert_eq!(r.next().await.unwrap()?, b);
    assert!(r.next().await.is_none());

    Ok(())
}

#[test]
fn test_bincode_ser() {
    use uuid7::Uuid;
    use Options;

    #[derive(Debug, Serialize, PartialEq, Eq, Clone)]
    struct Meta {
        id: uuid7::Uuid,
        topic: String,
    }
    #[derive(Debug, Serialize, PartialEq, Eq, Clone)]
    struct Ser {
        meta: Meta,
        data: BytesMut,
    }

    let a = Ser {
        meta: Meta {
            id: Uuid::MAX,
            topic: "111".to_owned(),
        },
        data: BytesMut::from([1, 1, 0, 1, 1, 0].as_slice()),
    };

    let b = bincode_option().serialize(&a).unwrap();

    println!("Len {}", b.len());
    println!("{b:?}");
}