pushwire-core 0.1.1

Shared types and codecs for push-wire multiplexed push protocol
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use serde::{Serialize, de::DeserializeOwned};
use std::io::{Cursor, Read, Write};
use thiserror::Error;
use zstd::bulk;

use crate::ChannelKind;

/// Magic bytes prefixing every binary frame ("RP").
pub const MAGIC_HEADER: [u8; 2] = [0x52, 0x50];
/// Version byte for future evolution.
pub const VERSION_BYTE: u8 = 1;

/// Flags describing frame properties.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, serde::Deserialize)]
pub struct BinaryFlags {
    pub compressed: bool,
    pub fragmented: bool,
    pub ack_required: bool,
}

impl BinaryFlags {
    pub fn to_byte(self) -> u8 {
        (self.compressed as u8) | ((self.fragmented as u8) << 1) | ((self.ack_required as u8) << 2)
    }

    pub fn from_byte(byte: u8) -> Self {
        Self {
            compressed: byte & 0b0000_0001 != 0,
            fragmented: byte & 0b0000_0010 != 0,
            ack_required: byte & 0b0000_0100 != 0,
        }
    }
}

/// Encoding used for the payload body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PayloadEncoding {
    MessagePack,
    Cbor,
}

/// Compression options for encoding/decoding.
#[derive(Debug, Clone)]
pub struct CompressionConfig {
    pub threshold: usize,
    pub dictionary: Option<CompressionDictionary>,
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            threshold: 512,
            dictionary: None,
        }
    }
}

/// Pre-trained zstd dictionary with a numeric ID.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompressionDictionary {
    pub id: u32,
    pub bytes: Vec<u8>,
}

impl CompressionDictionary {
    pub fn new(id: u32, bytes: Vec<u8>) -> Self {
        Self { id, bytes }
    }
}

/// Binary frame format:
/// `[magic:2][version:1][flags:1][channel:1][sequence:4][payload:var]`
///
/// The channel byte is determined by [`ChannelKind::wire_id()`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinaryFrame<C: ChannelKind, T = Vec<u8>> {
    pub channel: C,
    pub flags: BinaryFlags,
    pub sequence: u32,
    pub payload: T,
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum BinaryError {
    #[error("invalid magic header")]
    InvalidMagic,
    #[error("unsupported version byte {0}")]
    UnsupportedVersion(u8),
    #[error("unknown channel id {0}")]
    UnknownChannel(u8),
    #[error("serialization error")]
    Serialization,
    #[error("deserialization error")]
    Deserialization,
    #[error("frame too short")]
    FrameTooShort,
    #[error("compression error")]
    Compression,
    #[error("decompression error")]
    Decompression,
    #[error("missing compression dictionary {0}")]
    MissingDictionary(u32),
}

/// Encode without compression using the default settings.
pub fn encode_frame<C: ChannelKind, T: Serialize>(
    frame: &BinaryFrame<C, T>,
    encoding: PayloadEncoding,
) -> Result<Vec<u8>, BinaryError> {
    encode_frame_with_compression(frame, encoding, &CompressionConfig::default())
}

/// Encode with optional per-frame compression.
pub fn encode_frame_with_compression<C: ChannelKind, T: Serialize>(
    frame: &BinaryFrame<C, T>,
    encoding: PayloadEncoding,
    compression: &CompressionConfig,
) -> Result<Vec<u8>, BinaryError> {
    let mut flags = frame.flags;
    let mut out = Vec::with_capacity(16);
    out.extend_from_slice(&MAGIC_HEADER);
    out.push(VERSION_BYTE);

    let payload_bytes = serialize_payload(&frame.payload, encoding)?;
    let payload_len = payload_bytes.len();
    let compressed_attempt: Option<(Vec<u8>, Option<u32>)> = if payload_len < compression.threshold
    {
        None
    } else if let Some(dict) = &compression.dictionary {
        compress_with_dictionary(&payload_bytes, dict)
            .ok()
            .map(|c| (prepend_dict_id(c, dict.id), Some(dict.id)))
    } else {
        bulk::compress(&payload_bytes, 3)
            .ok()
            .map(|c| (prepend_dict_id(c, 0), None))
    };

    let (body, _dict_used) = match compressed_attempt {
        Some((c, id)) if c.len() < payload_len => (c, id),
        _ => (payload_bytes.clone(), None),
    };

    if body.len() < payload_len {
        flags.compressed = true;
        out.push(flags.to_byte());
        out.push(frame.channel.wire_id());
        out.extend_from_slice(&frame.sequence.to_be_bytes());
        out.extend_from_slice(&body);
        Ok(out)
    } else {
        flags.compressed = false;
        out.push(flags.to_byte());
        out.push(frame.channel.wire_id());
        out.extend_from_slice(&frame.sequence.to_be_bytes());
        out.extend_from_slice(&payload_bytes);
        Ok(out)
    }
}

/// Decode a frame without any pre-shared dictionaries.
pub fn decode_frame<C: ChannelKind, T: DeserializeOwned>(
    bytes: &[u8],
    encoding: PayloadEncoding,
) -> Result<BinaryFrame<C, T>, BinaryError> {
    decode_frame_with_dictionaries(bytes, encoding, &[])
}

/// Decode a frame using a set of pre-shared dictionaries.
pub fn decode_frame_with_dictionaries<C: ChannelKind, T: DeserializeOwned>(
    bytes: &[u8],
    encoding: PayloadEncoding,
    dictionaries: &[CompressionDictionary],
) -> Result<BinaryFrame<C, T>, BinaryError> {
    if bytes.len() < 9 {
        return Err(BinaryError::FrameTooShort);
    }

    if bytes[0..2] != MAGIC_HEADER {
        return Err(BinaryError::InvalidMagic);
    }
    let version = bytes[2];
    if version != VERSION_BYTE {
        return Err(BinaryError::UnsupportedVersion(version));
    }

    let flags = BinaryFlags::from_byte(bytes[3]);
    let channel = C::from_wire_id(bytes[4]).ok_or(BinaryError::UnknownChannel(bytes[4]))?;
    let sequence = u32::from_be_bytes([bytes[5], bytes[6], bytes[7], bytes[8]]);

    let payload_bytes = &bytes[9..];
    let payload = deserialize_payload(payload_bytes, encoding, flags, dictionaries)?;

    Ok(BinaryFrame {
        channel,
        flags,
        sequence,
        payload,
    })
}

/// Train a zstd dictionary from samples.
pub fn train_dictionary(
    samples: &[&[u8]],
    dict_size: usize,
    id: u32,
) -> Result<CompressionDictionary, BinaryError> {
    let dict =
        zstd::dict::from_samples(samples, dict_size).map_err(|_| BinaryError::Compression)?;
    Ok(CompressionDictionary::new(id, dict))
}

fn serialize_payload<T: Serialize>(
    payload: &T,
    encoding: PayloadEncoding,
) -> Result<Vec<u8>, BinaryError> {
    match encoding {
        PayloadEncoding::MessagePack => {
            rmp_serde::to_vec(payload).map_err(|_| BinaryError::Serialization)
        }
        PayloadEncoding::Cbor => {
            serde_cbor::to_vec(payload).map_err(|_| BinaryError::Serialization)
        }
    }
}

fn deserialize_payload<T: DeserializeOwned>(
    bytes: &[u8],
    encoding: PayloadEncoding,
    flags: BinaryFlags,
    dictionaries: &[CompressionDictionary],
) -> Result<T, BinaryError> {
    let data = if flags.compressed {
        let (dict_id, start) = extract_dict_id(bytes);
        let compressed = &bytes[start..];
        if let Some(id) = dict_id {
            let dict = dictionaries
                .iter()
                .find(|d| d.id == id)
                .ok_or(BinaryError::MissingDictionary(id))?;
            let mut decoder =
                zstd::stream::Decoder::with_dictionary(Cursor::new(compressed), &dict.bytes)
                    .map_err(|_| BinaryError::Decompression)?;
            let mut buf = Vec::new();
            decoder
                .read_to_end(&mut buf)
                .map_err(|_| BinaryError::Decompression)?;
            buf
        } else {
            zstd::stream::decode_all(Cursor::new(compressed))
                .map_err(|_| BinaryError::Decompression)?
        }
    } else {
        bytes.to_vec()
    };

    match encoding {
        PayloadEncoding::MessagePack => {
            rmp_serde::from_slice(&data).map_err(|_| BinaryError::Deserialization)
        }
        PayloadEncoding::Cbor => {
            serde_cbor::from_slice(&data).map_err(|_| BinaryError::Deserialization)
        }
    }
}

fn prepend_dict_id(mut data: Vec<u8>, id: u32) -> Vec<u8> {
    let mut out = Vec::with_capacity(data.len() + 4);
    out.extend_from_slice(&id.to_be_bytes());
    out.append(&mut data);
    out
}

fn extract_dict_id(bytes: &[u8]) -> (Option<u32>, usize) {
    if bytes.len() < 4 {
        return (None, 0);
    }
    let id = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
    if id == 0 { (None, 4) } else { (Some(id), 4) }
}

fn compress_with_dictionary(
    payload_bytes: &[u8],
    dict: &CompressionDictionary,
) -> Result<Vec<u8>, BinaryError> {
    let mut encoder = zstd::stream::Encoder::with_dictionary(Vec::new(), 3, &dict.bytes)
        .map_err(|_| BinaryError::Compression)?;
    encoder
        .write_all(payload_bytes)
        .map_err(|_| BinaryError::Compression)?;
    encoder.finish().map_err(|_| BinaryError::Compression)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ChannelKind;
    use serde::{Deserialize, Serialize};

    /// Test channel used across binary codec tests.
    #[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
    #[serde(rename_all = "lowercase")]
    enum Ch {
        Data,
        Ui,
    }

    impl ChannelKind for Ch {
        fn priority(&self) -> u8 {
            0
        }
        fn wire_id(&self) -> u8 {
            match self {
                Ch::Data => 0x07,
                Ch::Ui => 0x01,
            }
        }
        fn from_wire_id(id: u8) -> Option<Self> {
            match id {
                0x07 => Some(Ch::Data),
                0x01 => Some(Ch::Ui),
                _ => None,
            }
        }
        fn from_name(s: &str) -> Option<Self> {
            match s {
                "data" => Some(Ch::Data),
                "ui" => Some(Ch::Ui),
                _ => None,
            }
        }
        fn name(&self) -> &'static str {
            match self {
                Ch::Data => "data",
                Ch::Ui => "ui",
            }
        }
        fn is_system(&self) -> bool {
            false
        }
        fn all() -> &'static [Self] {
            &[Self::Data, Self::Ui]
        }
    }

    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
    struct Payload {
        id: u32,
        msg: String,
    }

    fn base_frame() -> BinaryFrame<Ch, Payload> {
        BinaryFrame {
            channel: Ch::Data,
            flags: BinaryFlags {
                compressed: false,
                fragmented: false,
                ack_required: true,
            },
            sequence: 42,
            payload: Payload {
                id: 1,
                msg: "hello".into(),
            },
        }
    }

    #[test]
    fn flags_roundtrip_bits() {
        let flags = BinaryFlags {
            compressed: true,
            fragmented: true,
            ack_required: false,
        };
        let byte = flags.to_byte();
        assert_eq!(BinaryFlags::from_byte(byte), flags);
    }

    #[test]
    fn messagepack_roundtrip() {
        let frame = base_frame();
        let bytes = encode_frame(&frame, PayloadEncoding::MessagePack).unwrap();
        let decoded: BinaryFrame<Ch, Payload> =
            decode_frame(&bytes, PayloadEncoding::MessagePack).unwrap();
        assert_eq!(decoded, frame);
    }

    #[test]
    fn cbor_roundtrip() {
        let frame = base_frame();
        let bytes = encode_frame(&frame, PayloadEncoding::Cbor).unwrap();
        let decoded: BinaryFrame<Ch, Payload> =
            decode_frame(&bytes, PayloadEncoding::Cbor).unwrap();
        assert_eq!(decoded, frame);
    }

    #[test]
    fn compresses_when_beneficial() {
        let frame = BinaryFrame {
            channel: Ch::Ui,
            flags: BinaryFlags {
                compressed: false,
                fragmented: false,
                ack_required: false,
            },
            sequence: 1,
            payload: Payload {
                id: 1,
                msg: "x".repeat(2048),
            },
        };
        let cfg = CompressionConfig {
            threshold: 256,
            dictionary: None,
        };
        let bytes =
            encode_frame_with_compression(&frame, PayloadEncoding::MessagePack, &cfg).unwrap();
        assert!(BinaryFlags::from_byte(bytes[3]).compressed);

        let decoded: BinaryFrame<Ch, Payload> =
            decode_frame_with_dictionaries(&bytes, PayloadEncoding::MessagePack, &[]).unwrap();
        assert_eq!(decoded.payload.msg.len(), 2048);
    }

    #[test]
    fn dictionary_training_and_use() {
        let samples_raw: Vec<Vec<u8>> = (0..10)
            .map(|i| format!("{{\"content\":\"sample_{i}_payload_data\"}}").into_bytes())
            .collect();
        let sample_refs: Vec<&[u8]> = samples_raw.iter().map(|b| b.as_slice()).collect();
        let dict = train_dictionary(&sample_refs, 256, 7).unwrap();
        let cfg = CompressionConfig {
            threshold: 1,
            dictionary: Some(dict.clone()),
        };
        let frame = base_frame();
        let bytes =
            encode_frame_with_compression(&frame, PayloadEncoding::MessagePack, &cfg).unwrap();
        let decoded: BinaryFrame<Ch, Payload> =
            decode_frame_with_dictionaries(&bytes, PayloadEncoding::MessagePack, &[dict]).unwrap();
        assert_eq!(decoded, frame);
    }

    #[test]
    fn rejects_bad_magic() {
        let mut bytes = encode_frame(&base_frame(), PayloadEncoding::MessagePack).unwrap();
        bytes[0] = 0x00;
        let err = decode_frame::<Ch, Payload>(&bytes, PayloadEncoding::MessagePack).unwrap_err();
        assert_eq!(err, BinaryError::InvalidMagic);
    }

    #[test]
    fn rejects_unknown_channel() {
        let mut bytes = encode_frame(&base_frame(), PayloadEncoding::MessagePack).unwrap();
        bytes[4] = 0xFF;
        let err = decode_frame::<Ch, Payload>(&bytes, PayloadEncoding::MessagePack).unwrap_err();
        assert_eq!(err, BinaryError::UnknownChannel(0xFF));
    }

    #[test]
    fn wire_id_preserved_in_encoding() {
        let frame = base_frame();
        let bytes = encode_frame(&frame, PayloadEncoding::MessagePack).unwrap();
        // Channel byte at offset 4 should be Ch::Data wire_id = 0x07
        assert_eq!(bytes[4], 0x07);
    }
}