|
|
@ -2,6 +2,7 @@ use crate::error::{Error, Result};
|
|
|
|
use crate::events::generate_event_id;
|
|
|
|
use crate::events::generate_event_id;
|
|
|
|
use crate::events::payload::FromPayload;
|
|
|
|
use crate::events::payload::FromPayload;
|
|
|
|
use byteorder::{BigEndian, ReadBytesExt};
|
|
|
|
use byteorder::{BigEndian, ReadBytesExt};
|
|
|
|
|
|
|
|
use bytes::{BufMut, Bytes, BytesMut};
|
|
|
|
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
|
|
|
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Debug;
|
|
|
@ -16,7 +17,7 @@ pub const FORMAT_VERSION: [u8; 3] = [0, 9, 0];
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Event {
|
|
|
|
pub struct Event {
|
|
|
|
header: EventHeader,
|
|
|
|
header: EventHeader,
|
|
|
|
data: Vec<u8>,
|
|
|
|
data: Bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
@ -41,21 +42,21 @@ impl Event {
|
|
|
|
/// Creates a new event that acts as an initiator for further response events
|
|
|
|
/// Creates a new event that acts as an initiator for further response events
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn initiator(namespace: Option<String>, name: String, data: Vec<u8>) -> Self {
|
|
|
|
pub fn initiator(namespace: Option<String>, name: String, data: Bytes) -> Self {
|
|
|
|
Self::new(namespace, name, data, None, EventType::Initiator)
|
|
|
|
Self::new(namespace, name, data, None, EventType::Initiator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new event that is a response to a previous event
|
|
|
|
/// Creates a new event that is a response to a previous event
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn response(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self {
|
|
|
|
pub fn response(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Response)
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new error event as a response to a previous event
|
|
|
|
/// Creates a new error event as a response to a previous event
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn error(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self {
|
|
|
|
pub fn error(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Error)
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -63,7 +64,7 @@ impl Event {
|
|
|
|
/// and might contain a final response payload
|
|
|
|
/// and might contain a final response payload
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(data))]
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn end(namespace: Option<String>, name: String, data: Vec<u8>, ref_id: u64) -> Self {
|
|
|
|
pub fn end(namespace: Option<String>, name: String, data: Bytes, ref_id: u64) -> Self {
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Response)
|
|
|
|
Self::new(namespace, name, data, Some(ref_id), EventType::Response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -72,7 +73,7 @@ impl Event {
|
|
|
|
pub(crate) fn new(
|
|
|
|
pub(crate) fn new(
|
|
|
|
namespace: Option<String>,
|
|
|
|
namespace: Option<String>,
|
|
|
|
name: String,
|
|
|
|
name: String,
|
|
|
|
data: Vec<u8>,
|
|
|
|
data: Bytes,
|
|
|
|
ref_id: Option<u64>,
|
|
|
|
ref_id: Option<u64>,
|
|
|
|
event_type: EventType,
|
|
|
|
event_type: EventType,
|
|
|
|
) -> Self {
|
|
|
|
) -> Self {
|
|
|
@ -145,57 +146,59 @@ impl Event {
|
|
|
|
// additional header fields can be added a the end because when reading they will just be ignored
|
|
|
|
// additional header fields can be added a the end because when reading they will just be ignored
|
|
|
|
let header: EventHeader = EventHeader::from_read(&mut Cursor::new(header_bytes))?;
|
|
|
|
let header: EventHeader = EventHeader::from_read(&mut Cursor::new(header_bytes))?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut data = vec![0u8; data_length as usize];
|
|
|
|
let mut buf = vec![0u8; data_length as usize];
|
|
|
|
reader.read_exact(&mut data).await?;
|
|
|
|
reader.read_exact(&mut buf).await?;
|
|
|
|
let event = Event { header, data };
|
|
|
|
let event = Event {
|
|
|
|
|
|
|
|
header,
|
|
|
|
|
|
|
|
data: Bytes::from(buf),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Ok(event)
|
|
|
|
Ok(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Encodes the event into bytes
|
|
|
|
/// Encodes the event into bytes
|
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
|
|
|
pub fn into_bytes(mut self) -> Result<Vec<u8>> {
|
|
|
|
pub fn into_bytes(self) -> Result<Bytes> {
|
|
|
|
let mut header_bytes = self.header.into_bytes();
|
|
|
|
let header_bytes = self.header.into_bytes();
|
|
|
|
let header_length = header_bytes.len() as u16;
|
|
|
|
let header_length = header_bytes.len() as u16;
|
|
|
|
let data_length = self.data.len();
|
|
|
|
let data_length = self.data.len();
|
|
|
|
let total_length = header_length as u64 + data_length as u64;
|
|
|
|
let total_length = header_length as u64 + data_length as u64;
|
|
|
|
tracing::trace!(total_length, header_length, data_length);
|
|
|
|
tracing::trace!(total_length, header_length, data_length);
|
|
|
|
|
|
|
|
|
|
|
|
let mut buf = Vec::with_capacity(total_length as usize);
|
|
|
|
let mut buf = BytesMut::with_capacity(total_length as usize);
|
|
|
|
buf.append(&mut total_length.to_be_bytes().to_vec());
|
|
|
|
buf.put_u64(total_length);
|
|
|
|
buf.append(&mut header_length.to_be_bytes().to_vec());
|
|
|
|
buf.put_u16(header_length);
|
|
|
|
buf.append(&mut header_bytes);
|
|
|
|
buf.put(header_bytes);
|
|
|
|
buf.append(&mut self.data);
|
|
|
|
buf.put(self.data);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(buf)
|
|
|
|
Ok(buf.freeze())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl EventHeader {
|
|
|
|
impl EventHeader {
|
|
|
|
/// Serializes the event header into bytes
|
|
|
|
/// Serializes the event header into bytes
|
|
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
|
|
pub fn into_bytes(self) -> Bytes {
|
|
|
|
let mut buf = FORMAT_VERSION.to_vec();
|
|
|
|
let mut buf = BytesMut::with_capacity(256);
|
|
|
|
buf.append(&mut self.id.to_be_bytes().to_vec());
|
|
|
|
buf.put_slice(&FORMAT_VERSION);
|
|
|
|
buf.push(self.event_type.into());
|
|
|
|
buf.put_u64(self.id);
|
|
|
|
|
|
|
|
buf.put_u8(u8::from(self.event_type));
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(ref_id) = self.ref_id {
|
|
|
|
if let Some(ref_id) = self.ref_id {
|
|
|
|
buf.push(0xFF);
|
|
|
|
buf.put_u8(0xFF);
|
|
|
|
buf.append(&mut ref_id.to_be_bytes().to_vec());
|
|
|
|
buf.put_u64(ref_id);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
buf.push(0x00);
|
|
|
|
buf.put_u8(0x00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(namespace) = self.namespace {
|
|
|
|
if let Some(namespace) = self.namespace {
|
|
|
|
let namespace_len = namespace.len() as u16;
|
|
|
|
buf.put_u16(namespace.len() as u16);
|
|
|
|
buf.append(&mut namespace_len.to_be_bytes().to_vec());
|
|
|
|
buf.put(Bytes::from(namespace));
|
|
|
|
buf.append(&mut namespace.into_bytes());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
buf.append(&mut 0u16.to_be_bytes().to_vec());
|
|
|
|
buf.put_u16(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let name_len = self.name.len() as u16;
|
|
|
|
buf.put_u16(self.name.len() as u16);
|
|
|
|
buf.append(&mut name_len.to_be_bytes().to_vec());
|
|
|
|
buf.put(Bytes::from(self.name));
|
|
|
|
buf.append(&mut self.name.into_bytes());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buf
|
|
|
|
buf.freeze()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Parses an event header from an async reader
|
|
|
|
/// Parses an event header from an async reader
|
|
|
|