use crate::core;
use crate::packet::Code;
use crate::stream::{RecvStream, RecvWriteStream, WriteStream};
pub mod future {
pub use crate::core::CallFuture as Call;
pub use crate::core::InfoRecvFuture as InfoRecv;
pub use crate::core::InfoSendFuture as InfoSend;
}
#[derive(Debug)]
pub enum RegistrationError {
NameAlreadyRegistered,
TooManyServices,
}
pub struct Service {
code: Code,
}
impl Service {
pub fn register(name: &'static str) -> Self {
match Self::try_register(name) {
Ok(this) => this,
Err(e) => panic!("{}: {:?}", name, e),
}
}
pub fn try_register(name: &'static str) -> Result<Self, RegistrationError> {
Ok(Self {
code: core::register_service(name)?,
})
}
pub fn call<'a, R, T>(&self, content: &'a [u8], receptor: R) -> future::Call<'a, R, T>
where
R: FnOnce(&[u8]) -> T + Unpin,
{
future::Call::new(self.code, content, receptor)
}
pub fn recv_info<R>(&self, receptor: R) -> future::InfoRecv<R>
where
R: Fn(&[u8]) + Unpin,
{
future::InfoRecv::new(self.code, receptor)
}
pub fn send_info<'a>(&self, content: &'a [u8]) -> future::InfoSend<'a> {
future::InfoSend::new(self.code, content)
}
pub fn stream(&self, id: i32) -> RecvWriteStream {
RecvWriteStream::new(core::init_stream(
self.code,
id,
core::STREAM_SELF_FLOW
| core::STREAM_SELF_DATA
| core::STREAM_PEER_FLOW
| core::STREAM_PEER_DATA,
))
}
pub fn input_stream(&self, id: i32) -> RecvStream {
RecvStream::new(core::init_stream(
self.code,
id,
core::STREAM_SELF_FLOW | core::STREAM_PEER_DATA,
))
}
pub fn output_stream(&self, id: i32) -> WriteStream {
WriteStream::new(core::init_stream(
self.code,
id,
core::STREAM_SELF_DATA | core::STREAM_PEER_FLOW,
))
}
}