1use bornera_core::{
4 ConnectionCore, FrameDecodeError, FrameDecoder, FrameDriver, OperationId, RetainedBytes,
5};
6use calandria::{Deadline, EventBatch, Retained, TimerQueue, TimerToken};
7
8use crate::{
9 ConnectionEvent, ConnectionSlotConfig, ConnectionSlotLimits, EngineOutcome, EngineState,
10 InboundClassifier, OutboundFrame, TcpSocketPolicy, TransportDiagnostic, TransportPressure,
11 TransportState,
12};
13
14#[derive(Debug)]
16pub struct ConnectionSlot<D, C>
17where
18 D: FrameDecoder,
19{
20 pub(crate) core: ConnectionCore<OutboundFrame>,
21 pub(crate) decoder: FrameDriver<D>,
22 pub(crate) classifier: C,
23 pub(crate) limits: ConnectionSlotLimits,
24 pub(crate) connect_deadline: Deadline,
25 pub(crate) socket_policy: TcpSocketPolicy,
26 pub(crate) timers: TimerQueue<DeadlineEvent>,
27 pub(crate) deadlines: Vec<DeadlineEntry>,
28 pub(crate) outcomes: EventBatch<EngineOutcome<D::Frame>>,
29 pub(crate) lifecycle: EventBatch<ConnectionEvent>,
30 pub(crate) recovery_outcomes: EventBatch<EngineOutcome<D::Frame>>,
31 pub(crate) recovery_events: EventBatch<ConnectionEvent>,
32 pub(crate) event_sequence: u64,
33 pub(crate) read_buffer: Vec<u8>,
34 pub(crate) decoder_pending: bool,
35 pub(crate) io_preference: IoPreference,
36 pub(crate) transport_state: TransportState,
37 pub(crate) transport_diagnostic: Option<TransportDiagnostic>,
38 pub(crate) transport_pressure: Option<TransportPressure>,
39 pub(crate) transport_retained_limit: Option<RetainedBytes>,
40 pub(crate) transport_contract_diverged: bool,
41 pub(crate) drain_deadline: Option<Deadline>,
42 pub(crate) close_request: Option<CloseDirective>,
43 pub(crate) state: EngineState,
44}
45
46impl<D, C> ConnectionSlot<D, C>
47where
48 D: FrameDecoder,
49 D::Frame: Retained,
50 C: InboundClassifier<D::Frame>,
51{
52 pub fn new(
54 config: ConnectionSlotConfig,
55 limits: ConnectionSlotLimits,
56 decoder: D,
57 classifier: C,
58 ) -> Result<Self, FrameDecodeError<D::Error>> {
59 let decoder = FrameDriver::new(decoder, limits.decoder_bytes())?;
60 let identity = config.identity();
61 let read_buffer = std::iter::repeat_n(0_u8, limits.io_chunk_bytes().get()).collect();
62 Ok(Self {
63 core: ConnectionCore::new(
64 identity.endpoint(),
65 identity.lane(),
66 identity.connection(),
67 identity.epoch(),
68 limits.connection(),
69 ),
70 decoder,
71 classifier,
72 limits,
73 connect_deadline: config.connect_deadline(),
74 socket_policy: config.tcp_policy(),
75 timers: TimerQueue::new(config.timer_owner(), limits.timers()),
76 deadlines: Vec::with_capacity(limits.connection().max_operations()),
77 outcomes: EventBatch::new(limits.outcomes()),
78 lifecycle: EventBatch::new(limits.lifecycle()),
79 recovery_outcomes: EventBatch::new(limits.outcomes()),
80 recovery_events: EventBatch::new(limits.recovery_lifecycle()),
81 event_sequence: 0,
82 read_buffer,
83 decoder_pending: false,
84 io_preference: IoPreference::Transport,
85 transport_state: TransportState::Connecting,
86 transport_diagnostic: None,
87 transport_pressure: None,
88 transport_retained_limit: None,
89 transport_contract_diverged: false,
90 drain_deadline: None,
91 close_request: None,
92 state: EngineState::Running,
93 })
94 }
95}
96
97#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98pub(crate) struct DeadlineEvent {
99 pub(crate) epoch: bornera_core::ConnectionEpoch,
100 pub(crate) operation: OperationId,
101}
102
103impl Retained for DeadlineEvent {
104 fn retained_bytes(&self) -> RetainedBytes {
105 RetainedBytes::ZERO
106 }
107}
108
109#[derive(Clone, Copy, Debug, Eq, PartialEq)]
110pub(crate) struct DeadlineEntry {
111 pub(crate) operation: OperationId,
112 pub(crate) token: TimerToken,
113}
114
115#[derive(Clone, Copy, Debug, Eq, PartialEq)]
116pub(crate) enum IoPreference {
117 Transport,
118 Decode,
119 Read,
120 Write,
121}
122
123impl IoPreference {
124 pub(crate) const fn next(self) -> Self {
125 match self {
126 Self::Transport => Self::Decode,
127 Self::Decode => Self::Read,
128 Self::Read => Self::Write,
129 Self::Write => Self::Transport,
130 }
131 }
132}
133
134#[derive(Clone, Copy, Debug, Eq, PartialEq)]
135pub(crate) enum CloseDirective {
136 Core {
137 reason: bornera_core::CloseReason,
138 shutdown: ShutdownState,
139 },
140 Abort,
141}
142
143#[derive(Clone, Copy, Debug, Eq, PartialEq)]
144pub(crate) enum ShutdownState {
145 Immediate,
146 Pending { deadline: Deadline },
147 Started { deadline: Deadline },
148 Complete,
149}
150
151impl CloseDirective {
152 pub(crate) const fn settlement_ready(self) -> bool {
153 matches!(
154 self,
155 Self::Core {
156 shutdown: ShutdownState::Immediate | ShutdownState::Complete,
157 ..
158 } | Self::Abort
159 )
160 }
161
162 pub(crate) const fn shutdown_deadline(self) -> Option<Deadline> {
163 match self {
164 Self::Core {
165 shutdown: ShutdownState::Pending { deadline } | ShutdownState::Started { deadline },
166 ..
167 } => Some(deadline),
168 Self::Core { .. } | Self::Abort => None,
169 }
170 }
171}
172
173pub(crate) fn to_u64(value: usize) -> u64 {
174 u64::try_from(value).unwrap_or(u64::MAX)
175}