encoder/decoder impl
This commit is contained in:
parent
60aa584f43
commit
8a972cd660
1 changed files with 55 additions and 0 deletions
55
northbridge/src/framed_codec.rs
Normal file
55
northbridge/src/framed_codec.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use anyhow::anyhow;
|
||||
use common::Command;
|
||||
use framed::{BoxPayload, FRAME_END_SYMBOL};
|
||||
use tokio_util::{bytes::BytesMut, codec::{Decoder, Encoder}};
|
||||
|
||||
pub struct FramedCodec {
|
||||
codec: framed::bytes::Codec,
|
||||
}
|
||||
|
||||
impl FramedCodec {
|
||||
pub fn new() -> FramedCodec {
|
||||
let codec = framed::bytes::Config::default().to_codec();
|
||||
FramedCodec {codec}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder for FramedCodec {
|
||||
type Item = BoxPayload;
|
||||
type Error = framed::Error;
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||
|
||||
match src.iter().position(|e| *e==FRAME_END_SYMBOL) {
|
||||
Some(len) => {
|
||||
let data = src.split_to(len);
|
||||
|
||||
Ok(Some(self.codec.decode_to_box(&data)?))
|
||||
}
|
||||
None => {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder<Vec<u8>> for FramedCodec {
|
||||
type Error = framed::Error;
|
||||
|
||||
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
let encoded = self.codec.encode_to_box(&item)?;
|
||||
|
||||
dst.extend_from_slice(&encoded);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder<Command> for FramedCodec {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn encode(&mut self, item: Command, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||
let data = postcard::to_stdvec(&item)?;
|
||||
Ok(self.encode(data, dst).map_err(|e| anyhow!("encode fail: {e:?}"))?)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue