Skip to main content

dope_session/
session.rs

1use dambi::Bytes;
2
3use crate::SlotId;
4use crate::pool::client::state::CLIENT_IOV_CAP;
5use crate::protocol::client::ClientProtocol;
6use crate::ring::frame::FrameRing;
7use crate::ring::send::SendRing;
8
9pub struct Egress<'a> {
10    inner: &'a mut SendRing<CLIENT_IOV_CAP>,
11}
12
13impl<'a> Egress<'a> {
14    #[inline(always)]
15    pub fn push(&mut self, bytes: Bytes) {
16        self.inner.push(bytes);
17    }
18
19    #[inline(always)]
20    pub fn is_empty(&self) -> bool {
21        self.inner.is_empty()
22    }
23}
24
25pub struct ClientSessionGeneric<P: ClientProtocol, const ING: usize, const IOV: usize> {
26    pub(crate) ingress: FrameRing<P::Framer, ING>,
27    pub(crate) framer: P::Framer,
28    pub(crate) egress: SendRing<IOV>,
29    pub(crate) state: P::ConnState,
30}
31
32impl<P: ClientProtocol, const ING: usize, const IOV: usize> Default
33    for ClientSessionGeneric<P, ING, IOV>
34{
35    #[inline(always)]
36    fn default() -> Self {
37        Self {
38            ingress: FrameRing::new(),
39            framer: P::Framer::default(),
40            egress: SendRing::new(),
41            state: P::ConnState::default(),
42        }
43    }
44}
45
46impl<P: ClientProtocol, const ING: usize> ClientSessionGeneric<P, ING, CLIENT_IOV_CAP> {
47    #[inline(always)]
48    pub fn enqueue(&mut self, bytes: Bytes) {
49        self.egress.push(bytes);
50    }
51
52    #[inline(always)]
53    pub fn state(&self) -> &P::ConnState {
54        &self.state
55    }
56
57    #[inline(always)]
58    pub fn state_mut(&mut self) -> &mut P::ConnState {
59        &mut self.state
60    }
61
62    #[inline(always)]
63    pub fn egress_is_empty(&self) -> bool {
64        self.egress.is_empty()
65    }
66
67    pub(crate) fn on_connect(&mut self, proto: &mut P, conn_id: SlotId) {
68        let Self { state, egress, .. } = self;
69        proto.on_connect(conn_id, state, Egress { inner: egress });
70    }
71
72    pub(crate) fn ingest(&mut self, src: &[u8], proto: &mut P, conn_id: SlotId) {
73        let _ = self.ingress.append(src);
74        let Self {
75            ingress,
76            framer,
77            state,
78            egress,
79            ..
80        } = self;
81        ingress.drain(framer, |head| {
82            proto.on_response(conn_id, state, head, Egress { inner: egress });
83        });
84    }
85
86    #[inline(always)]
87    pub(crate) fn fill_msghdr(&mut self, bytes_cap: usize) -> Option<&libc::msghdr> {
88        self.egress.fill_msghdr(bytes_cap)
89    }
90
91    #[inline(always)]
92    pub(crate) fn ack_send(&mut self, n: u32) {
93        self.egress.ack(n);
94    }
95}