pub fn write_command(stream : &mut TcpStream, command : &CommandMessage) -> Result<(), Error> {
    let data = command.get_payload();

    let payload_len = match data {
        Some(p) => match u16::try_from(p.len()) {
            Ok(l)  => l,
            Err(_) => return Err(Error::InvalidPayload)
        },
        None => 0
    };

    let mut payload : Vec<u8> = Vec::with_capacity(payload_len as usize + 3);

    payload.push(command.get_code_u8());
    payload.extend_from_slice(&payload_len.to_be_bytes());

    if payload_len != 0 {
        payload.extend_from_slice(data.unwrap());
    }

    write(stream, &payload)
}
/// Write a CommandMessage to stream
///
/// message format is: [command u8 - len u16 - payload]
Write a CommandMessage to stream
message format is: [command u8 - len u16 - payload]
Write a CommandMessage to stream message format is : [ command u8 - len u16 - payload ]
pub fn write_command(stream : &mut TcpStream, command : &CommandMessage) -> Result<(), Error> {
    let data = command.get_payload();
    let payload_len = match data {
        Some(p) => match u16::try_from(p.len()) {
            Ok(l)  => l,
            Err(_) => return Err(Error::InvalidPayload)
        },
        None => 0
    };
    let mut payload : Vec<u8> = Vec::with_capacity(payload_len as usize + 3);
    payload.push(command.get_code_u8());
    payload.extend_from_slice(&payload_len.to_be_bytes());
    if payload_len != 0 {
        payload.extend_from_slice(data.unwrap());
    }
    write(stream, &payload)
}
pub fn write_command ( stream : & mut TcpStream , command : & CommandMessage ) -> Result < ( ) , Error > { let data = command . get_payload ( ) ; let payload_len = match data { Some ( p ) => match u16 :: try_from ( p . len ( ) ) { Ok ( l ) => l , Err ( _ ) => return Err ( Error :: InvalidPayload ) } , None => 0 } ; let mut payload : Vec < u8 > = Vec :: with_capacity ( payload_len as usize + 3 ) ; payload . push ( command . get_code_u8 ( ) ) ; payload . extend_from_slice ( & payload_len . to_be_bytes ( ) ) ; if payload_len != 0 { payload . extend_from_slice ( data . unwrap ( ) ) ; } write ( stream , & payload ) }
Write a CommandMessage to stream
message format is: [command u8 - len u16 - payload]
Write a CommandMessage to stream message format is : [ command u8 - len u16 - payload ]
stream
&mut TcpStream
command
&CommandMessage
stream
&mut TcpStream
command
&CommandMessage