<commit_before>use bytes::{BufMut, BytesMut, BigEndian};
use tokio_io::codec::{Encoder, Decoder};

pub use super::error::IpcError;

pub const MAGIC: i16 = ((b'1' as i16) << 8) as i16 + b'w' as i16;
pub const HEADER_SIZE: usize = 8;

#[derive(Debug, Clone)]
pub enum IpcMessage {
    ClientHello,
    ServerHello,
    ClientBye,
    RequestAuthentication(String, String)
}

#[derive(Debug)]
pub struct IpcMessageCodec;

type DecodeResult = Result<Option<(IpcMessage, usize)>, IpcError>;

impl IpcMessage {
    pub fn message_type(&self) -> &[u8; 2] {
        match *self {
            IpcMessage::ClientHello => b"CH",
            IpcMessage::ServerHello => b"SH",
            IpcMessage::ClientBye => b"CB",
            IpcMessage::RequestAuthentication(ref _a, ref _b) => b"RA"
        }
    }
}

impl Encoder for IpcMessageCodec {
    type Item = IpcMessage;
    type Error = IpcError;

    fn encode(&mut self, msg: Self::Item, buf: &mut BytesMut) -> Result<(), Self::Error> {
        let len = match msg {
            IpcMessage::ClientHello => 0,
            IpcMessage::ServerHello => 0,
            IpcMessage::ClientBye => 0,
            IpcMessage::RequestAuthentication(ref user, ref secret) => user.len() + secret.len(),
            _   => return Err(IpcError::UnknownMessageType)
        };

        buf.reserve(HEADER_SIZE + len);
        buf.put_i16::<BigEndian>(MAGIC);
        buf.extend(msg.message_type());
        buf.put_u32::<BigEndian>(len as u32);

        match msg {
            IpcMessage::RequestAuthentication(ref user, ref secret) => {
                buf.put_u32::<BigEndian>(user.len() as u32);
                buf.extend(user.as_bytes());
                buf.put_u32::<BigEndian>(secret.len() as u32);
                buf.extend(secret.as_bytes());
            },
            _   => ()
        };<commit_msg>[libcommon] Fix invalid length calculation in IpcMessageCodec::encode<commit_after>use bytes::{BufMut, BytesMut, BigEndian};
use tokio_io::codec::{Encoder, Decoder};

pub use super::error::IpcError;

pub const MAGIC: i16 = ((b'1' as i16) << 8) as i16 + b'w' as i16;
pub const HEADER_SIZE: usize = 8;

#[derive(Debug, Clone)]
pub enum IpcMessage {
    ClientHello,
    ServerHello,
    ClientBye,
    RequestAuthentication(String, String)
}

#[derive(Debug)]
pub struct IpcMessageCodec;

type DecodeResult = Result<Option<(IpcMessage, usize)>, IpcError>;

impl IpcMessage {
    pub fn message_type(&self) -> &[u8; 2] {
        match *self {
            IpcMessage::ClientHello => b"CH",
            IpcMessage::ServerHello => b"SH",
            IpcMessage::ClientBye => b"CB",
            IpcMessage::RequestAuthentication(ref _a, ref _b) => b"RA"
        }
    }
}

impl Encoder for IpcMessageCodec {
    type Item = IpcMessage;
    type Error = IpcError;

    fn encode(&mut self, msg: Self::Item, buf: &mut BytesMut) -> Result<(), Self::Error> {
        let len = match msg {
            IpcMessage::ClientHello => 0,
            IpcMessage::ServerHello => 0,
            IpcMessage::ClientBye => 0,
            IpcMessage::RequestAuthentication(ref user, ref secret) => user.len() + secret.len() + 8,
            _   => return Err(IpcError::UnknownMessageType)
        };

        buf.reserve(HEADER_SIZE + len);
        buf.put_i16::<BigEndian>(MAGIC);
        buf.extend(msg.message_type());
        buf.put_u32::<BigEndian>(len as u32);

        match msg {
            IpcMessage::RequestAuthentication(ref user, ref secret) => {
                buf.put_u32::<BigEndian>(user.len() as u32);
                buf.extend(user.as_bytes());
                buf.put_u32::<BigEndian>(secret.len() as u32);
                buf.extend(secret.as_bytes());
            },
            _   => ()
        };
{'fraction_non_alphanumeric': 0.14144385026737968, 'fraction_numerical': 0.016310160427807488, 'mean_word_length': 2.5527065527065527, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3265503', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1205, 'n_words': 259}