use dope_sys::{
engine::ring::Backend as _,
io::reactor::complete::{Entry as _, Flags as _, Outcome as _, Record as _, flags::Meta as _},
io::socket::option::tuning,
ring::ready::{Complete as _, Ready as _},
table::{
token,
token::{Codec as _, frame, frame::Decode as _, host, host::Drain as _},
},
};
use crate::{
driver::{
life::{Lifecycle, Local},
receive,
receive::tokens,
result::poll::fault,
retained::flight,
},
io::{self, event::batch::Flow, fd::domain::retire},
ring::cqe::{Entry, Frame, NativeEntry, Observation, Scalar, Selected, Token, Tune, Tuning},
};
use dope_uring::Progress as _;
use fault::{Raw, Violation};
use o3::mem::slot;
pub(crate) struct Envelope<'d> {
pub(crate) parts: Entry,
pub(crate) buffer: Option<tokens::Token<'d, Selected<'d>>>,
}
impl<'d> Envelope<'d> {
pub(crate) fn next<'event>(
output: slot::Vacant<'event, Self>,
local: &mut Local<'d>,
) -> Option<Option<slot::Occupied<'event, Self>>> {
let (ring, tuning) = local.backend.split();
let parts = match tuning.next(ring.submission()) {
Some(Some(tune)) => Entry {
token: Token::tuning(tune),
outcome: Scalar::from_error(dope_sys::Errno::CANCELED),
flags: <NativeEntry as dope_sys::io::reactor::complete::Entry>::Flags::new(0),
},
Some(None) => return Some(None),
None => ring.pop()?.into_parts(),
};
Some(Some(output.insert(Self {
parts,
buffer: None,
})))
}
pub(crate) fn reject(&self, subject: fault::Subject, cause: Violation) -> fault::Fault {
fault::Fault {
observation: fault::Observation::new(Observation::new(
self.parts.token,
self.parts.outcome,
self.parts.flags,
)),
subject,
violation: cause,
}
}
fn retire(&self, echo: retire::Echo<'_>) -> bool {
use host::Echo as _;
if self.is_plain() && self.parts.outcome.is_zero() {
echo.complete();
true
} else {
echo.retry();
false
}
}
fn reject_raw(&self, local: &Local<'d>, cause: Raw) -> fault::Fault {
use host::Echo as _;
let retire = local.driver.descriptors().retirement();
let closed = match self.parts.token.closed() {
Some(returned) => retire.bind(returned),
None => host::Closed::Missing,
};
match closed {
host::Closed::Ready(echo) => {
self.retire(echo);
self.reject(fault::Subject::Unbound, Violation::Raw(cause))
}
host::Closed::Class {
echo,
expected,
observed,
} => {
echo.retry();
self.reject(
fault::Subject::Unbound,
Violation::RawClass {
cause,
expected,
observed,
},
)
}
host::Closed::Missing => self.reject(fault::Subject::Unbound, Violation::Raw(cause)),
}
}
pub(crate) fn flow(&self) -> Flow {
self.parts.flags.meta().flow()
}
pub(super) fn is_plain(&self) -> bool {
self.parts.flags.0 == 0
}
pub(crate) fn admit(&mut self, local: &Local<'d>) -> Result<(), fault::Fault> {
if let Some(bid) = self.parts.flags.meta().buffer() {
self.buffer = Some(
match unsafe { tokens::Token::select(local.driver, bid.0) } {
Ok(buffer) => buffer,
Err(error) => {
return Err(self.reject_raw(
local,
Raw::BufferIndex(receive::Selection::from_native(error)),
));
}
},
);
}
if !self.parts.flags.is_valid() {
return Err(self.reject_raw(local, Raw::Flags));
}
Ok(())
}
pub(crate) fn returned(
&self,
returned: retire::Returned,
local: &Local<'d>,
) -> Result<(), fault::Fault> {
use host::Echo as _;
let drain = flight::Drain::new(local.flights, local.driver);
match drain.retired(returned) {
host::Closed::Ready(echo) => {
if self.retire(echo) {
Ok(())
} else {
Err(self.reject(fault::Subject::Unbound, Violation::Close))
}
}
host::Closed::Class {
echo,
expected,
observed,
} => {
echo.retry();
Err(self.reject(
fault::Subject::Unbound,
Violation::Class { expected, observed },
))
}
host::Closed::Missing => Err(self.unbound()),
}
}
pub(crate) fn unbound(&self) -> fault::Fault {
self.reject(fault::Subject::Unbound, Violation::UserData)
}
pub(crate) fn frame(&self, control: Frame) -> frame::Control<Tune> {
if self.is_plain() {
control.decode()
} else {
frame::Control::Unknown
}
}
pub(crate) fn control(&self, frame: Frame) -> fault::Fault {
self.reject(fault::Subject::Control(frame.control()), Violation::Control)
}
pub(crate) fn cancel(
&self,
state: &mut Tuning,
cancel: host::Cancel<flight::raw::Echo, Tune>,
) -> Result<(), fault::Fault> {
use token::Tuning as _;
let subject = match &cancel {
host::Cancel::Flight(echo) => fault::Subject::CancelFlight(fault::Echo::new(*echo)),
host::Cancel::Tuning(tune) => fault::Subject::CancelTuning(tune.slot()),
};
if !self.is_plain() {
return Err(self.reject(subject, Violation::Control));
}
let outcome = self.parts.outcome;
if outcome.is_error()
&& let host::Cancel::Tuning(tune) = &cancel
{
tuning::Table::retry_cancel(state, tune.slot().raw());
}
match outcome.canceled() {
Ok(()) => Ok(()),
Err(cause) => Err(self.reject(subject, Violation::Outcome(cause))),
}
}
pub(in crate::ring) fn reclaim(
&mut self,
lifecycle: &mut Lifecycle<'d, impl io::Report>,
) -> Result<(), fault::Fault> {
let control = match self.parts.token.decode() {
token::Decoded::Flight(key) => {
let drain = flight::Drain::new(lifecycle.local.flights, lifecycle.local.driver);
let Some(flight) = drain.bind(key) else {
return Err(self.unbound());
};
return self.reclaim_flight(lifecycle, flight);
}
token::Decoded::Framework(control) => control,
token::Decoded::Close(returned) => return self.returned(returned, &lifecycle.local),
token::Decoded::CancelFlight(key) => {
return self.cancel(lifecycle.local.backend.tuning(), host::Cancel::Flight(key));
}
token::Decoded::CancelTuning(tune) => {
return self.cancel(lifecycle.local.backend.tuning(), host::Cancel::Tuning(tune));
}
token::Decoded::Invalid => return Err(self.unbound()),
};
match self.frame(control) {
frame::Control::Tuning(tune) => {
return self.reclaim_tuning(lifecycle, tune, control.control());
}
frame::Control::Shutdown => {
let outcome = self.parts.outcome;
if let Err(cause) = outcome.stopped() {
return Err(self.reject(
fault::Subject::Control(control.control()),
Violation::Outcome(cause),
));
}
if lifecycle.source.is_some() && outcome.is_error() {
return Ok(());
}
lifecycle.shutdown();
}
frame::Control::ClosePrep => {
return match self.parts.outcome.aborted() {
Ok(()) => Ok(()),
Err(cause) => Err(self.reject(
fault::Subject::Control(control.control()),
Violation::Outcome(cause),
)),
};
}
frame::Control::Unknown => return Err(self.control(control)),
}
Ok(())
}
}