Skip to main content

tfserver/codec/
length_delimited.rs

1use crate::codec::codec_trait::TfCodec;
2use crate::structures::transport::Transport;
3use async_trait::async_trait;
4use std::io;
5use tokio_util::bytes::{Bytes, BytesMut};
6use tokio_util::codec::{Decoder, Encoder};
7#[derive(Clone)]
8///The wrapper aroung existing codec, to be compatible with server
9pub struct LengthDelimitedCodec {
10    codec: tokio_util::codec::LengthDelimitedCodec,
11}
12
13impl LengthDelimitedCodec {
14    pub fn new(max_message_length: usize) -> Self {
15        Self {
16            codec: tokio_util::codec::LengthDelimitedCodec::builder()
17                .max_frame_length(max_message_length)
18                .new_codec(),
19        }
20    }
21}
22
23impl Decoder for LengthDelimitedCodec {
24    type Item = BytesMut;
25    type Error = io::Error;
26
27    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
28        self.codec.decode(src)
29    }
30}
31
32impl Encoder<Bytes> for LengthDelimitedCodec {
33    type Error = io::Error;
34    fn encode(&mut self, item: Bytes, dst: &mut BytesMut) -> Result<(), Self::Error> {
35        self.codec.encode(item, dst)
36    }
37}
38#[async_trait]
39impl TfCodec for LengthDelimitedCodec {
40    async fn initial_setup(&mut self, _: &mut Transport) -> bool {
41        true
42    }
43}