Struct mles_utils::MsgHdr

source ·
pub struct MsgHdr { /* private fields */ }
Expand description

MsgHdr structure

This structure defines the header of the Mles message including first ‘M’ byte, length of the encoded data, connection id and SipHash key. Encoded message will always be in network byte order.

Implementations§

source§

impl MsgHdr

source

pub fn new(len: u32, cid: u32, key: u64) -> MsgHdr

Create a new MsgHdr object with length, cid and key.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let msghdr = MsgHdr::new(len, cid, key);
source

pub fn get_type(&self) -> u8

Get type of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.get_type();
assert_eq!('M' as u8, msghdr.get_type());
source

pub fn get_hdrkey_len() -> usize

Get MsgHdr length on the line.

source

pub fn set_len(&mut self, len: u32)

Set length of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_len(515);
source

pub fn get_len(&self) -> u32

Get length of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_len(515);
assert_eq!(515, msghdr.get_len());
source

pub fn set_cid(&mut self, cid: u32)

Set cid of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_cid(515);
source

pub fn get_cid(&self) -> u32

Get cid of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_cid(515);
assert_eq!(515, msghdr.get_cid());
source

pub fn set_key(&mut self, key: u64)

Set key of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_key(515);
source

pub fn get_key(&self) -> u64

Get key of MsgHdr.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
msghdr.set_key(515);
assert_eq!(515, msghdr.get_key());
source

pub fn encode(&self) -> Vec<u8>

Encode MsgHdr to line format.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 0;

let mut msghdr = MsgHdr::new(len, cid, key);
let msgv: Vec<u8> = msghdr.encode();
source

pub fn decode(buf: Vec<u8>) -> MsgHdr

Decode MsgHdr from line format.

Example
use mles_utils::{MsgHdr};

let key = 0xf00f;
let cid = MsgHdr::select_cid(key);
let len = 16;

let mut msghdr = MsgHdr::new(len, cid, key);
let msgv: Vec<u8> = msghdr.encode();
let msgh = MsgHdr::decode(msgv);
assert_eq!(key, msgh.get_key());
assert_eq!(cid, msgh.get_cid());
assert_eq!(len, msgh.get_len());
source

pub fn do_hash(t: &[String]) -> u64

Do a valid hash for Mles over provided UTF-8 String list.

Example
use mles_utils::MsgHdr;

let hashstr1 = "A string".to_string();
let hashstr2 = "Another string".to_string();
let hashable = vec![hashstr1, hashstr2];
let key: u64 = MsgHdr::do_hash(&hashable);
source

pub fn select_cid(key: u64) -> u32

Return a connection id from key.

Example
use mles_utils::MsgHdr;

let cid = MsgHdr::select_cid(0x1000000100000001);
assert_eq!(cid, 0x00000001);
source

pub fn addr2str(addr: &SocketAddr) -> String

Do a valid UTF-8 string from a SocketAddr.

For IPv4 the format is “x.x.x.x:y”, where x is u8 and y is u16 For IPv6 the format is “[z:z:z:z:z:z:z:z]:y”, where z is u16 in hexadecimal format and y is u16

Example

use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};
use mles_utils::MsgHdr;

let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
let addrstr = MsgHdr::addr2str(&addr);

assert_eq!("127.0.0.1:8080", addrstr);

let addr = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0xff03, 0, 0, 0, 0, 0, 0, 1)), 8077);
let addrstr = MsgHdr::addr2str(&addr);

assert_eq!("[ff03:0:0:0:0:0:0:1]:8077", addrstr);

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.