Skip to main content

dope_session/pool/
mod.rs

1use std::marker::PhantomData;
2
3use crate::{CodecLayer, WriteBufStorage};
4use dope_core::driver::{Completion, CompletionRouter, DriverLifecycle, PoolDriver};
5use dope_core::profile::Profile;
6use dope_runtime::runtime::Manifold;
7use dope_transport::Transport;
8
9pub mod client;
10pub(crate) mod core;
11pub mod server;
12pub mod slot;
13
14use self::core::PoolCore;
15
16pub trait Direction<B: PoolDriver>: 'static {
17    type Transport: Transport;
18    type SlotUser: Default + 'static;
19    type CodecLayer: CodecLayer;
20    type WriteBuf: WriteBufStorage;
21
22    fn has_pending(&self) -> bool;
23
24    fn tick(
25        &mut self,
26        core: &mut PoolCore<Self::Transport, Self::CodecLayer, Self::WriteBuf, Self::SlotUser, B>,
27        driver: &mut B::Driver,
28    );
29
30    #[inline(always)]
31    fn on_accept_event(&mut self, _token: B::Token, _result: i32, _more: bool) {}
32}
33
34pub struct Pool<D: Direction<B>, F: Profile, B: PoolDriver> {
35    pub(crate) direction: D,
36    pub(crate) core: PoolCore<D::Transport, D::CodecLayer, D::WriteBuf, D::SlotUser, B>,
37    pub(crate) profile: PhantomData<F>,
38}
39
40pub trait PoolTick<B: PoolDriver> {
41    fn tick(&mut self, driver: &mut B::Driver);
42}
43
44pub(crate) trait PoolRecv<B: PoolDriver> {
45    fn on_recv_event(
46        &mut self,
47        driver: &mut B::Driver,
48        token: B::Token,
49        result: i32,
50        more: bool,
51        bid: Option<u16>,
52    );
53}
54
55pub(crate) trait PoolWrite<B: PoolDriver> {
56    fn on_write_event(&mut self, driver: &mut B::Driver, token: B::Token, result: i32, notif: bool);
57}
58
59impl<D, F, B> Manifold for Pool<D, F, B>
60where
61    D: Direction<B>,
62    F: Profile,
63    B: PoolDriver,
64    Self: PoolTick<B> + CompletionRouter<B>,
65{
66    type Backend = B;
67
68    #[inline(always)]
69    fn has_pending(&self, _driver: &mut B::Driver) -> bool {
70        F::HYBRID_PARK && self.direction.has_pending()
71    }
72
73    #[inline(always)]
74    fn drive(&mut self, driver: &mut B::Driver) {
75        B::dispatch_completions(driver, self);
76        <Self as PoolTick<B>>::tick(self, driver);
77        let _ = driver.submit_only();
78    }
79}
80
81impl<D, F, B> CompletionRouter<B> for Pool<D, F, B>
82where
83    D: Direction<B>,
84    F: Profile,
85    B: PoolDriver,
86    Self: PoolRecv<B> + PoolWrite<B>,
87{
88    #[inline(always)]
89    fn on_complete(&mut self, driver: &mut B::Driver, ev: Completion<B>) {
90        match ev {
91            Completion::Recv {
92                token,
93                result,
94                more,
95                bid,
96            } => self.on_recv_event(driver, token, result, more, bid),
97            Completion::Write {
98                token,
99                result,
100                notif,
101            } => self.on_write_event(driver, token, result, notif),
102            Completion::Accept {
103                token,
104                result,
105                more,
106            } => self.direction.on_accept_event(token, result, more),
107        }
108    }
109}