2019-12-20 13:56:01 +00:00
|
|
|
use bincode;
|
2020-02-04 15:42:04 +00:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2020-01-13 16:53:28 +00:00
|
|
|
//use std::collections::VecDeque;
|
2020-02-21 13:08:34 +00:00
|
|
|
use crate::types::{Mid, Sid};
|
2020-01-13 16:53:28 +00:00
|
|
|
use std::sync::Arc;
|
2019-12-20 13:56:01 +00:00
|
|
|
|
2020-04-24 10:56:04 +00:00
|
|
|
pub 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
|
2020-02-04 15:42:04 +00:00
|
|
|
pub data: Vec<u8>,
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct OutGoingMessage {
|
2020-02-04 15:42:04 +00:00
|
|
|
pub buffer: Arc<MessageBuffer>,
|
|
|
|
pub cursor: u64,
|
2020-03-22 13:47:21 +00:00
|
|
|
pub mid: Mid,
|
2020-02-04 15:42:04 +00:00
|
|
|
pub sid: Sid,
|
2019-12-20 13:56:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:53:28 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct InCommingMessage {
|
2020-02-04 15:42:04 +00:00
|
|
|
pub buffer: MessageBuffer,
|
|
|
|
pub length: u64,
|
|
|
|
pub mid: Mid,
|
|
|
|
pub sid: Sid,
|
2020-01-13 16:53:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 15:42:04 +00:00
|
|
|
pub(crate) fn serialize<M: Serialize>(message: &M) -> MessageBuffer {
|
2020-04-08 14:26:42 +00:00
|
|
|
let writer = bincode::serialize(message).unwrap();
|
2019-12-20 13:56:01 +00:00
|
|
|
MessageBuffer { data: writer }
|
|
|
|
}
|
|
|
|
|
2020-02-04 15:42:04 +00:00
|
|
|
pub(crate) fn deserialize<M: DeserializeOwned>(buffer: MessageBuffer) -> M {
|
|
|
|
let span = buffer.data;
|
|
|
|
let decoded: M = bincode::deserialize(span.as_slice()).unwrap();
|
|
|
|
decoded
|
|
|
|
}
|
|
|
|
|
2020-02-25 18:30:50 +00:00
|
|
|
impl std::fmt::Debug for MessageBuffer {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
//TODO: small messages!
|
|
|
|
let len = self.data.len();
|
|
|
|
if len > 20 {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"MessageBuffer(len: {}, {}, {}, {}, {:?}..{:?})",
|
|
|
|
len,
|
2020-04-24 10:56:04 +00:00
|
|
|
u32::from_le_bytes([self.data[0], self.data[1], self.data[2], self.data[3]]),
|
|
|
|
u32::from_le_bytes([self.data[4], self.data[5], self.data[6], self.data[7]]),
|
|
|
|
u32::from_le_bytes([self.data[8], self.data[9], self.data[10], self.data[11]]),
|
2020-02-25 18:30:50 +00:00
|
|
|
&self.data[13..16],
|
|
|
|
&self.data[len - 8..len]
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
write!(f, "MessageBuffer(len: {}, {:?})", len, &self.data[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:56:01 +00:00
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|