evoke_core/
channel.rs

1use std::{error::Error, future::Future};
2
3use alkahest::{Pack, Schema, Unpacked};
4use scoped_arena::Scope;
5
6#[cfg(feature = "tcp")]
7pub mod tcp;
8
9pub trait ChannelError {
10    type Error: Error + 'static;
11}
12
13pub trait ChannelFuture<'a>: ChannelError {
14    type Send: Future<Output = Result<(), Self::Error>>;
15    type Ready: Future<Output = Result<(), Self::Error>>;
16}
17
18/// Abstract raw bytes channel that can be used by `evoke` sessions.
19pub trait Channel: ChannelError + for<'a> ChannelFuture<'a> {
20    /// Attempts to send packet through the channel unreliably.
21    /// Packet either arrives complete or lost.
22    fn send<'a, S, P>(
23        &'a mut self,
24        packet: P,
25        scope: &'a Scope,
26    ) -> <Self as ChannelFuture<'a>>::Send
27    where
28        S: Schema,
29        P: Pack<S>;
30
31    /// Attempts to send packet through the channel reliabliy.
32    /// Packet will arrive unless channel connection is lost.
33    fn send_reliable<'a, S, P>(
34        &'a mut self,
35        packet: P,
36        scope: &'a Scope,
37    ) -> <Self as ChannelFuture<'a>>::Send
38    where
39        S: Schema,
40        P: Pack<S>;
41
42    /// Waits until channel is ready for `recv` call.
43    fn recv_ready(&mut self) -> <Self as ChannelFuture<'_>>::Ready;
44
45    /// Attempts to receive packet from the channel.
46    /// If no new packets arrived `Ok(None)` is returned.
47    fn recv<'a, S>(&mut self, scope: &'a Scope) -> Result<Option<Unpacked<'a, S>>, Self::Error>
48    where
49        S: Schema;
50}
51
52pub trait Listener {
53    type Error: Error + 'static;
54    type Channel: Channel;
55
56    fn try_accept(&mut self) -> Result<Option<Self::Channel>, Self::Error>;
57}