pub mod stream;
pub mod crypto;
use async_trait::async_trait;
use crate::message::common::Message;
use crate::serialization::deserializable::Deserializable;
use crate::serialization::error::SerializationError;
use crate::serialization::serializable::{Serializable, Serialized};
pub const TRANSPORT_TARGET_SERVER: u128 = 1;
pub trait TransportTransformer: Send{
fn detransform(&self, data: &Serialized) -> Result<Serialized, SerializationError>;
fn transform(&self, data: &Serialized) -> Serialized;
}
#[async_trait]
pub trait Transport{
async fn send_raw(&mut self, data: Serialized) -> Result<usize, tokio::io::Error>;
async fn receive_raw(&mut self, timeout: Option<u64>) -> Option<Serialized>;
#[inline]
async fn send<T: Serializable + Send>(&mut self, object: T) -> Result<usize, tokio::io::Error>{
self.send_raw(object.serialize()).await
}
async fn receive<T: Deserializable>(&mut self,
timeout: Option<u64>) -> Option<Result<T, SerializationError>>{
let serialized_data = self.receive_raw(timeout).await;
if serialized_data.is_none(){
return None;
}
let result = T::from_serialized(&serialized_data.unwrap());
if result.is_err(){
return Some(Err(result.err().unwrap()));
}
let (obj, _) = result.unwrap();
Some(Ok(obj))
}
fn add_transformer<'a>(&'a mut self, transformer: Box<dyn TransportTransformer>) -> &'a Self;
}
#[async_trait]
pub trait TransportService{
async fn send_message(&self, target: u128, msg: &Message);
}