1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate bytes;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate rustc_serialize;
extern crate bincode;
extern crate time;
extern crate crossbeam;
extern crate tokio_core;
extern crate futures;
extern crate serde_json;
extern crate rand;
extern crate tokio_io;
extern crate uuid;

use bincode::SizeLimit;
use bincode::rustc_serialize::{encode, decode, DecodingResult};
use rustc_serialize::{Encodable, Decodable};

mod clock;
mod crc16;
mod transport;
mod kernel;
mod fsm;
mod model;
// pub mod net;

pub use clock::{Clock, RealClock, TestClock};
use crc16::{crc16_arr, crc16};

#[inline(always)]
pub fn to_binary<T: Encodable>(s: &T) -> Vec<u8> {
    encode(s, SizeLimit::Infinite).unwrap()
}

#[inline(always)]
pub fn to_framed_binary<T: Encodable>(s: &T) -> Vec<u8> {
    let mut bytes = to_binary(s);
    let mut size = usize_to_array(bytes.len()).to_vec();
    let mut ret = Vec::with_capacity(bytes.len() + 4);
    ret.append(&mut size);
    ret.append(&mut bytes);
    ret
}

#[inline(always)]
pub fn from_binary<T: Decodable>(encoded: Vec<u8>) -> DecodingResult<T> {
    decode(&encoded[..])
}

#[inline(always)]
pub fn usize_to_array(u: usize) -> [u8; 4] {
    [(u >> 24) as u8, (u >> 16) as u8, (u >> 8) as u8, u as u8]
}

#[inline(always)]
pub fn array_to_usize(ip: [u8; 4]) -> usize {
    ((ip[0] as usize) << 24) as usize + ((ip[1] as usize) << 16) as usize +
    ((ip[2] as usize) << 8) as usize + (ip[3] as usize)
}