Skip to main content

dope_session/pool/
mod.rs

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