1use std::marker::PhantomData;
2
3use crate::Codec;
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
20 fn has_pending(&self) -> bool;
21
22 fn tick<C: Codec>(
23 &mut self,
24 core: &mut PoolCore<Self::Transport, C, Self::SlotUser, B>,
25 driver: &mut B::Driver,
26 );
27
28 #[inline(always)]
29 fn on_accept_event(&mut self, _token: B::Token, _result: i32, _more: bool) {}
30}
31
32pub struct Pool<D: Direction<B>, C: Codec, F: Profile, B: PoolDriver> {
33 pub(crate) direction: D,
34 pub(crate) core: PoolCore<D::Transport, C, D::SlotUser, B>,
35 pub(crate) profile: PhantomData<F>,
36}
37
38pub trait PoolTick<B: PoolDriver> {
39 fn tick(&mut self, driver: &mut B::Driver);
40}
41
42pub(crate) trait PoolRecv<B: PoolDriver> {
43 fn on_recv_event(
44 &mut self,
45 driver: &mut B::Driver,
46 token: B::Token,
47 result: i32,
48 more: bool,
49 bid: Option<u16>,
50 );
51}
52
53pub(crate) trait PoolWrite<B: PoolDriver> {
54 fn on_write_event(&mut self, driver: &mut B::Driver, token: B::Token, result: i32, notif: bool);
55}
56
57impl<D, C, F, B> Manifold for Pool<D, C, F, B>
58where
59 D: Direction<B>,
60 C: Codec,
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, C, F, B> CompletionRouter<B> for Pool<D, C, F, B>
81where
82 D: Direction<B>,
83 C: Codec,
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}