2019-12-20 13:56:01 +00:00
|
|
|
use bincode;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-01-13 16:53:28 +00:00
|
|
|
//use std::collections::VecDeque;
|
|
|
|
use std::sync::Arc;
|
2019-12-20 13:56:01 +00:00
|
|
|
pub trait Message<'a> = Serialize + Deserialize<'a>;
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct MessageBuffer {
|
2019-12-20 13:56:01 +00:00
|
|
|
// use VecDeque for msg storage, because it allows to quickly remove data from front.
|
|
|
|
//however VecDeque needs custom bincode code, but it's possible
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct OutGoingMessage {
|
2019-12-20 13:56:01 +00:00
|
|
|
buffer: Arc<MessageBuffer>,
|
|
|
|
cursor: u64,
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct InCommingMessage {
|
|
|
|
buffer: MessageBuffer,
|
|
|
|
cursor: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn serialize<'a, M: Message<'a>>(message: &M) -> MessageBuffer {
|
2019-12-20 13:56:01 +00:00
|
|
|
let mut writer = {
|
|
|
|
let actual_size = bincode::serialized_size(message).unwrap();
|
|
|
|
Vec::<u8>::with_capacity(actual_size as usize)
|
|
|
|
};
|
|
|
|
if let Err(e) = bincode::serialize_into(&mut writer, message) {
|
|
|
|
println!("Oh nooo {}", e);
|
|
|
|
};
|
|
|
|
MessageBuffer { data: writer }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::message::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serialize_test() {
|
|
|
|
let msg = "abc";
|
|
|
|
let mb = serialize(&msg);
|
|
|
|
assert_eq!(mb.data.len(), 11);
|
|
|
|
assert_eq!(mb.data[0], 3);
|
|
|
|
assert_eq!(mb.data[1], 0);
|
|
|
|
assert_eq!(mb.data[7], 0);
|
|
|
|
assert_eq!(mb.data[8], 'a' as u8);
|
|
|
|
assert_eq!(mb.data[8], 97);
|
|
|
|
assert_eq!(mb.data[9], 'b' as u8);
|
|
|
|
assert_eq!(mb.data[10], 'c' as u8);
|
|
|
|
}
|
|
|
|
}
|