mirror of https://github.com/Trivernis/bromine.git
commit
e3860618f5
@ -0,0 +1,94 @@
|
||||
# Specification
|
||||
|
||||
This specification is split into two parts. The first one explaining how each type is represented in binary form and the
|
||||
second one specifying the behaviour of event passing and handling.
|
||||
|
||||
## Binary Representation of Events
|
||||
|
||||
Events store contain two types of data. The header data with the id, name, namespace and referenced event id and the
|
||||
payload with the associated data. Both the header and the payload have a dynamic length and should be extendable while
|
||||
ensuring backwards compatibility. The binary layout looks like this:
|
||||
|
||||
| Name | Type | Length (bytes) | Description |
|
||||
|---------------|-----------|----------------------------|------------------------------------|
|
||||
| total_length | `u64` | 8 | total length of the event in bytes |
|
||||
| header_length | `u16` | 2 | length of the event header |
|
||||
| header | `Header` | header_length | the header of the event |
|
||||
| data | `Vec<u8>` | total_length - data_length | the payload of the event |
|
||||
|
||||
### Header
|
||||
|
||||
| Name | Type | Length (bytes) | Description |
|
||||
|------------------|----------|------------------|------------------------------------------------------|
|
||||
| format_version | `[u8]` | 3 | version of the specification |
|
||||
| id | `u64` | 8 | id of the event |
|
||||
| ref_id_exists | `u8` | 1 | 0xFF indicates that a ref id exists and must be read |
|
||||
| ref_id | `u64` | 8 | ref id. only when the indicator is 0xFF |
|
||||
| namespace_length | `u16` | 2 | length of the namespace. 0 means there's none |
|
||||
| namespace | `String` | namespace_length | namespace of the event |
|
||||
| name_length | `u16` | 2 | length of the event name |
|
||||
| name | `String` | name_length | name of the event |
|
||||
|
||||
The header format ensures that it can be read without knowing its length.
|
||||
That means that a valid header can be deserialized even if the length of the header bytes
|
||||
is longer. Additional header fields can therefore be appended without having to worry about
|
||||
backwards compatibility of the format.
|
||||
|
||||
|
||||
## Binary Representation of Special Payloads
|
||||
|
||||
### Raw Payload
|
||||
|
||||
The raw payload is a `Vec<u8>` and written as is without serialization or deserialization.
|
||||
|
||||
|
||||
### Tandem Payload
|
||||
|
||||
The tandem payload contains two inner payloads which can be serialized and deserialized
|
||||
independently.
|
||||
Its layout is as follows:
|
||||
|
||||
| Name | Type | Length (bytes) | Description |
|
||||
|-----------------|-------|-----------------|------------------------------|
|
||||
| payload1_length | `u64` | 8 | length of the first payload |
|
||||
| payload1 | `T1` | payload1_length | the first payload |
|
||||
| payload2_length | `u64` | 8 | length of the second payload |
|
||||
| payload2 | `T2` | payload2_length | the second payload |
|
||||
|
||||
|
||||
### Serde Payload
|
||||
|
||||
The serde payload stores an encoded payload with additional information about the format
|
||||
the data was serialized as.
|
||||
|
||||
| Name | Type | Length (bytes) | Description |
|
||||
|-----------|------|----------------|------------------------------------------|
|
||||
| format_id | `u8` | 1 | the format the payload was serialized as |
|
||||
| payload | `T` | ~ | the serialized payload |
|
||||
|
||||
|
||||
## Behaviour
|
||||
|
||||
### Receiving events
|
||||
|
||||
When receiving an event the handler registered for the name of the event is called.
|
||||
The event will be ignored if no handler is registered.
|
||||
|
||||
|
||||
### Receiving namespaced events
|
||||
|
||||
Namespaced events are handled similar to regular event handling. Instead of searching
|
||||
for a handler that handles the event with the given name, first the namespace for the
|
||||
event is retrieved. On the namespace the handler registered for that specific event is called.
|
||||
If no namespace for the event namespace is registered or no handler is registered for
|
||||
the event name, the event will be ignored.
|
||||
|
||||
|
||||
### Receiving answers to emitted events
|
||||
|
||||
When emitting an event to a peer, the emitter can wait for an answer to that event.
|
||||
This is achieved by emitting events as a response to a specific event id.
|
||||
When an event with a reference event id (ref_id) is received, first the registry is
|
||||
searched for handlers waiting for a response (by trying to receive from a channel).
|
||||
If a handler can be found, the event is passed to the handler waiting for the response.
|
||||
Otherwise, the event will be processed as a regular event.
|
@ -1,23 +1,158 @@
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
mod serialize_rmp;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
pub use serialize_rmp::*;
|
||||
mod serialize_rmp;
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
mod serialize_bincode;
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
pub use serialize_bincode::*;
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
mod serialize_postcard;
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
pub use serialize_postcard::*;
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
mod serialize_json;
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
pub use serialize_json::*;
|
||||
pub type SerializationResult<T> = std::result::Result<T, SerializationError>;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializationError {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
#[error("failed to serialize messagepack payload: {0}")]
|
||||
SerializeRmp(#[from] rmp_serde::encode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
#[error("failed to deserialize messagepack payload: {0}")]
|
||||
DeserializeRmp(#[from] rmp_serde::decode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
#[error("failed to de/serialize bincode payload: {0}")]
|
||||
Bincode(#[from] bincode::Error),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
#[error("failed to de/serialize postcard payload: {0}")]
|
||||
Postcard(#[from] postcard::Error),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
#[error("failed to de/serialize json payload: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
|
||||
#[error("io error occurred on de/serialization: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("the format {0:?} is not available")]
|
||||
UnavailableFormat(DynamicSerializer),
|
||||
|
||||
#[error("tried to create serializer for unknown format {0}")]
|
||||
UnknownFormat(usize),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||
pub enum DynamicSerializer {
|
||||
Messagepack,
|
||||
Bincode,
|
||||
Postcard,
|
||||
Json,
|
||||
}
|
||||
|
||||
impl DynamicSerializer {
|
||||
pub fn first_available() -> Self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
{
|
||||
Self::Messagepack
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "serialize_bincode", not(feature = "serialize_rmp")))]
|
||||
{
|
||||
Self::Bincode
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "serialize_postcard",
|
||||
not(any(feature = "serialize_rmp", feature = "serialize_bincode"))
|
||||
))]
|
||||
{
|
||||
Self::Postcard
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "serialize_json",
|
||||
not(any(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard"
|
||||
))
|
||||
))]
|
||||
{
|
||||
Self::Json
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_primitive(num: usize) -> SerializationResult<Self> {
|
||||
match num {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
0 => Ok(Self::Messagepack),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
1 => Ok(Self::Bincode),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
2 => Ok(Self::Postcard),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
3 => Ok(Self::Json),
|
||||
|
||||
n => Err(SerializationError::UnknownFormat(n)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialize<T: Serialize>(&self, data: T) -> SerializationResult<Vec<u8>> {
|
||||
match self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
DynamicSerializer::Messagepack => serialize_rmp::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
DynamicSerializer::Bincode => serialize_bincode::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
DynamicSerializer::Postcard => serialize_postcard::serialize(data),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
DynamicSerializer::Json => serialize_json::serialize(data),
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard",
|
||||
feature = "serialize_json"
|
||||
)))]
|
||||
_ => Err(SerializationError::UnavailableFormat(self.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize<T: DeserializeOwned, R: Read>(&self, reader: R) -> SerializationResult<T> {
|
||||
match self {
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
DynamicSerializer::Messagepack => serialize_rmp::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
DynamicSerializer::Bincode => serialize_bincode::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
DynamicSerializer::Postcard => serialize_postcard::deserialize(reader),
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
DynamicSerializer::Json => serialize_json::deserialize(reader),
|
||||
|
||||
#[cfg(not(all(
|
||||
feature = "serialize_rmp",
|
||||
feature = "serialize_bincode",
|
||||
feature = "serialize_postcard",
|
||||
feature = "serialize_json"
|
||||
)))]
|
||||
_ => Err(SerializationError::UnavailableFormat(self.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,15 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = bincode::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = bincode::serialize(&data)?;
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = bincode::serialize(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = bincode::deserialize_from(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = bincode::deserialize_from(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,29 +1,16 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = serde_json::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = serde_json::to_vec(&data)?;
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = serde_json::to_vec(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = serde_json::from_reader(reader)?;
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = serde_json::from_reader(reader)?;
|
||||
|
||||
Ok(type_data)
|
||||
}
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,32 +1,19 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::IPCResult;
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
|
||||
pub type SerializationError = postcard::Error;
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = postcard::to_allocvec(&data)?.to_vec();
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = postcard::to_allocvec(&self)?.to_vec();
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(mut reader: R) -> IPCResult<Self> {
|
||||
let mut buf = Vec::new();
|
||||
// reading to end means reading the full size of the provided data
|
||||
reader.read_to_end(&mut buf)?;
|
||||
let type_data = postcard::from_bytes(&buf)?;
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(mut reader: R) -> SerializationResult<T> {
|
||||
let mut buf = Vec::new();
|
||||
// reading to end means reading the full size of the provided data
|
||||
reader.read_to_end(&mut buf)?;
|
||||
let type_data = postcard::from_bytes(&buf)?;
|
||||
|
||||
Ok(type_data)
|
||||
}
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -1,48 +1,15 @@
|
||||
use crate::payload::{EventReceivePayload, EventSendPayload};
|
||||
use crate::prelude::{IPCError, IPCResult};
|
||||
use crate::payload::SerializationResult;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SerializationError {
|
||||
#[error("failed to serialize with rmp: {0}")]
|
||||
Serialize(#[from] rmp_serde::encode::Error),
|
||||
pub fn serialize<T: Serialize>(data: T) -> SerializationResult<Vec<u8>> {
|
||||
let bytes = rmp_serde::to_vec(&data)?;
|
||||
|
||||
#[error("failed to deserialize with rmp: {0}")]
|
||||
Deserialize(#[from] rmp_serde::decode::Error),
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
impl From<rmp_serde::decode::Error> for IPCError {
|
||||
fn from(e: rmp_serde::decode::Error) -> Self {
|
||||
IPCError::Serialization(SerializationError::Deserialize(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp_serde::encode::Error> for IPCError {
|
||||
fn from(e: rmp_serde::encode::Error) -> Self {
|
||||
IPCError::Serialization(SerializationError::Serialize(e))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> EventSendPayload for T
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn to_payload_bytes(self) -> IPCResult<Vec<u8>> {
|
||||
let bytes = rmp_serde::to_vec(&self)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> EventReceivePayload for T
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn from_payload_bytes<R: Read>(reader: R) -> IPCResult<Self> {
|
||||
let type_data = rmp_serde::from_read(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
pub fn deserialize<R: Read, T: DeserializeOwned>(reader: R) -> SerializationResult<T> {
|
||||
let type_data = rmp_serde::from_read(reader)?;
|
||||
Ok(type_data)
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
use bromine::prelude::*;
|
||||
|
||||
#[cfg(feature = "serialize_rmp")]
|
||||
#[test]
|
||||
fn it_serializes_messagepack() {
|
||||
test_serialization(DynamicSerializer::Messagepack)
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize_bincode")]
|
||||
#[test]
|
||||
fn it_serializes_bincode() {
|
||||
test_serialization(DynamicSerializer::Bincode)
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize_postcard")]
|
||||
#[test]
|
||||
fn it_serializes_postcard() {
|
||||
test_serialization(DynamicSerializer::Postcard)
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize_json")]
|
||||
#[test]
|
||||
fn it_serializes_json() {
|
||||
test_serialization(DynamicSerializer::Json)
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
fn test_serialization(serializer: DynamicSerializer) {
|
||||
let test_payload = get_test_payload(serializer);
|
||||
let payload_bytes = test_payload.clone().to_payload_bytes().unwrap();
|
||||
let payload = TestSerdePayload::from_payload_bytes(&payload_bytes[..]).unwrap();
|
||||
assert_eq!(payload.data(), test_payload.data())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
pub mod payload {
|
||||
use bromine::payload::{DynamicSerializer, SerdePayload};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub type TestSerdePayload = SerdePayload<TestPayload>;
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
||||
pub struct TestPayload {
|
||||
items: Vec<u64>,
|
||||
variant: TestPayloadEnum,
|
||||
string: String,
|
||||
signed: i32,
|
||||
maps: HashMap<String, i64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
|
||||
pub enum TestPayloadEnum {
|
||||
First,
|
||||
Second,
|
||||
Third(usize),
|
||||
}
|
||||
|
||||
pub fn get_test_payload(serializer: DynamicSerializer) -> SerdePayload<TestPayload> {
|
||||
let mut maps = HashMap::new();
|
||||
maps.insert("Hello".to_string(), 12);
|
||||
|
||||
maps.insert("Wäüörld".to_string(), -12380);
|
||||
let inner_payload = TestPayload {
|
||||
items: vec![0u64, 12452u64, u64::MAX],
|
||||
variant: TestPayloadEnum::Third(12),
|
||||
string: String::from("Hello World ſð"),
|
||||
signed: -12,
|
||||
maps,
|
||||
};
|
||||
|
||||
SerdePayload::new(serializer, inner_payload)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "serialize")]
|
||||
pub use payload::*;
|
Loading…
Reference in New Issue