codec.rs 2.48 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
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
/*
 * Copyright 2024-2025 NVIDIA CORPORATION & AFFILIATES
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

//! Codec Module
//!
//! Codec map structure into blobs of bytes and streams of bytes.
//!
//! In this module, we define three primary codec used to issue single, two-part or multi-part messages,
//! on a byte stream.

use tokio_util::{
    bytes::{Buf, BufMut, BytesMut},
    codec::{Decoder, Encoder},
};

mod two_part;

pub use two_part::{TwoPartCodec, TwoPartMessage, TwoPartMessageType};

// // Custom codec that reads a u64 length header and the message of that length
// #[derive(Default)]
// pub struct LengthPrefixedCodec;

// impl LengthPrefixedCodec {
//     pub fn new() -> Self {
//         LengthPrefixedCodec {}
//     }
// }

// impl Decoder for LengthPrefixedCodec {
//     type Item = Vec<u8>;
//     type Error = tokio::io::Error;

//     fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
//         // Check if enough bytes are available to read the length (u64 = 8 bytes)
//         if src.len() < 8 {
//             return Ok(None); // Not enough data to read the length
//         }

//         // Read the u64 length header
//         let len = src.get_u64() as usize;

//         // Check if enough bytes are available to read the full message
//         if src.len() < len {
//             src.reserve(len - src.len()); // Reserve space for the remaining bytes
//             return Ok(None);
//         }

//         // Read the actual message bytes of the specified length
//         let data = src.split_to(len).to_vec();
//         Ok(Some(data))
//     }
// }

// impl Encoder<Vec<u8>> for LengthPrefixedCodec {
//     type Error = tokio::io::Error;

//     fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
//         // Write the length of the message as a u64 header
//         dst.put_u64(item.len() as u64);

//         // Write the actual message bytes
//         dst.put_slice(&item);
//         Ok(())
//     }
// }