Skip to main content

dope_core/io/socket/
peer.rs

1use dope_sys::engine::admit;
2use dope_sys::io::reactor::event::{Entry, kind};
3use o3::mem::slot;
4
5use crate::{
6    Backend, Errno,
7    driver::{self, life::native::host::Host, retained::flight, route},
8    io::{
9        fd::handles::Descriptor,
10        socket::{option, pending},
11    },
12};
13
14/// The same descriptor and its observed peer UID, or its pending native owner.
15pub enum Query<'d, P = pending::raw::Owner<'d, kind::Tuning>> {
16    Ready(Descriptor<'d>, Option<u32>),
17    Pending(P),
18}
19
20impl<'d> Query<'d> {
21    pub fn complete<'slot>(
22        owner: slot::Occupied<'slot, pending::raw::Owner<'d, kind::Tuning>>,
23        done: &Entry<flight::Complete<'d>, kind::Tuning, Result<u32, Errno>>,
24    ) -> pending::raw::Completion<
25        'slot,
26        'd,
27        pending::raw::Owner<'d, kind::Tuning>,
28        Result<Option<u32>, Errno>,
29    > {
30        let (fd, result) = pending::raw::Owner::complete(owner, done)?;
31        Ok((fd, result.map(Self::uid)))
32    }
33
34    fn uid(value: u32) -> Option<u32> {
35        (value != u32::MAX).then_some(value)
36    }
37
38    pub fn try_new<Tag: route::Tag + route::Select<kind::Tuning>>(
39        driver: &mut driver::Local<'d>,
40        slots: &flight::raw::Slots<'d, Tag>,
41        target: route::Target<'d, Tag>,
42        fd: Descriptor<'d>,
43    ) -> Result<Self, pending::Rejection<'d>> {
44        let slot = fd.slot();
45        // SAFETY: these exact driver-branded owners retain the native slot and
46        // flight until terminal completion; native admission cannot reenter slots.
47        unsafe {
48            admit::raw::Socket::<Host<Backend>, { option::MAX_STREAM_OPTIONS }>::peer(
49                &mut *driver.backend,
50                (slots, target.operation::<kind::Tuning>()),
51                (fd, driver.driver.brand),
52                slot,
53                &mut driver.retry,
54            )
55        }
56    }
57}
58
59impl<'d> From<((Descriptor<'d>, driver::Invariant<'d>), u32)> for Query<'d> {
60    fn from(((fd, _), value): ((Descriptor<'d>, driver::Invariant<'d>), u32)) -> Self {
61        Self::Ready(fd, Self::uid(value))
62    }
63}
64
65impl<'d, P>
66    From<(
67        (Descriptor<'d>, driver::Invariant<'d>),
68        flight::Flight<'d, kind::Tuning>,
69        P,
70    )> for Query<'d, pending::raw::Owner<'d, kind::Tuning, P>>
71{
72    fn from(
73        ((fd, _), flight, permit): (
74            (Descriptor<'d>, driver::Invariant<'d>),
75            flight::Flight<'d, kind::Tuning>,
76            P,
77        ),
78    ) -> Self {
79        Self::Pending(pending::raw::Owner::new(fd, flight, permit))
80    }
81}