Skip to content

Commit 9b89de7

Browse files
authored
feat(s2n-events): Add config option to generate C API (#2818)
1 parent 1a1bb54 commit 9b89de7

File tree

5 files changed

+32
-151
lines changed

5 files changed

+32
-151
lines changed

tools/event-generator/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ impl EventInfo<'_> {
6161
env!("CARGO_MANIFEST_DIR"),
6262
"/../../quic/s2n-quic-core/src/event"
6363
),
64-
generate_config: GenerateConfig {
65-
mode: OutputMode::Mut,
66-
},
64+
generate_config: GenerateConfig::default(),
6765
s2n_quic_core_path: quote!(crate),
6866
api: quote!(),
6967
builder: quote!(),
@@ -112,6 +110,7 @@ impl EventInfo<'_> {
112110
),
113111
generate_config: GenerateConfig {
114112
mode: OutputMode::Ref,
113+
..Default::default()
115114
},
116115
s2n_quic_core_path: quote!(s2n_quic_core),
117116
api: quote! {

tools/s2n-events/src/bin/generate_test_events.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
use proc_macro2::TokenStream;
55
use quote::quote;
6-
use s2n_events::{parser, validation, Output, Result};
6+
use s2n_events::{parser, validation, GenerateConfig, Output, OutputMode, Result};
77

88
struct EventInfo<'a> {
99
input_path: &'a str,
1010
output_path: &'a str,
1111
crate_name: &'a str,
1212
s2n_quic_core_path: TokenStream,
13-
builder: TokenStream,
1413
tracing_subscriber_def: TokenStream,
14+
config: GenerateConfig,
1515
}
1616

1717
impl EventInfo<'_> {
@@ -48,10 +48,11 @@ impl EventInfo<'_> {
4848
),
4949
output_path: concat!(env!("CARGO_MANIFEST_DIR"), "/tests/c_ffi_events/event"),
5050
s2n_quic_core_path: quote!(s2n_quic_core),
51-
builder: quote! {
52-
pub use s2n_quic_core::event::builder::SocketAddress;
53-
},
5451
tracing_subscriber_def,
52+
config: GenerateConfig {
53+
mode: OutputMode::Mut,
54+
c_api: true,
55+
},
5556
}
5657
}
5758
}
@@ -83,10 +84,10 @@ fn main() -> Result<()> {
8384

8485
let mut output = Output {
8586
s2n_quic_core_path: event_info.s2n_quic_core_path,
86-
builders: event_info.builder,
8787
tracing_subscriber_def: event_info.tracing_subscriber_def,
8888
crate_name: event_info.crate_name,
8989
root,
90+
config: event_info.config,
9091
..Default::default()
9192
};
9293

tools/s2n-events/src/generate_config.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl OutputMode {
3232
#[derive(Debug, Default)]
3333
pub struct GenerateConfig {
3434
pub mode: OutputMode,
35+
pub c_api: bool,
3536
}
3637

3738
impl GenerateConfig {
@@ -143,10 +144,23 @@ impl GenerateConfig {
143144
}
144145
}
145146

146-
pub fn supervisor(&self) -> TokenStream {
147+
fn supervisor_supported(&self) -> bool {
148+
// The supervisor feature will require additional effort to expose a C API for. For now,
149+
// this feature is disabled for simplicity.
150+
if self.c_api {
151+
return false;
152+
}
153+
147154
match self.mode {
148-
OutputMode::Ref => quote!(),
149-
OutputMode::Mut => quote!(
155+
OutputMode::Ref => false,
156+
OutputMode::Mut => true,
157+
}
158+
}
159+
160+
pub fn supervisor(&self) -> TokenStream {
161+
match self.supervisor_supported() {
162+
false => quote!(),
163+
true => quote!(
150164
pub mod supervisor {
151165
//! This module contains the `supervisor::Outcome` and `supervisor::Context` for use
152166
//! when implementing [`Subscriber::supervisor_timeout`](crate::event::Subscriber::supervisor_timeout) and
@@ -214,9 +228,9 @@ impl GenerateConfig {
214228
}
215229

216230
pub fn supervisor_timeout(&self) -> TokenStream {
217-
match self.mode {
218-
OutputMode::Ref => quote!(),
219-
OutputMode::Mut => quote!(
231+
match self.supervisor_supported() {
232+
false => quote!(),
233+
true => quote!(
220234
/// The period at which `on_supervisor_timeout` is called
221235
///
222236
/// If multiple `event::Subscriber`s are composed together, the minimum `supervisor_timeout`
@@ -257,9 +271,9 @@ impl GenerateConfig {
257271
}
258272

259273
pub fn supervisor_timeout_tuple(&self) -> TokenStream {
260-
match self.mode {
261-
OutputMode::Ref => quote!(),
262-
OutputMode::Mut => quote!(
274+
match self.supervisor_supported() {
275+
false => quote!(),
276+
true => quote!(
263277
#[inline]
264278
fn supervisor_timeout(
265279
&mut self,

tools/s2n-events/tests/c_ffi_events/event/generated.rs

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ pub mod tracing {
217217
}
218218
pub mod builder {
219219
use super::*;
220-
pub use s2n_quic_core::event::builder::SocketAddress;
221220
#[derive(Clone, Debug)]
222221
pub struct ConnectionMeta {
223222
pub id: u64,
@@ -323,58 +322,6 @@ pub mod builder {
323322
}
324323
}
325324
}
326-
pub mod supervisor {
327-
#![doc = r" This module contains the `supervisor::Outcome` and `supervisor::Context` for use"]
328-
#![doc = r" when implementing [`Subscriber::supervisor_timeout`](crate::event::Subscriber::supervisor_timeout) and"]
329-
#![doc = r" [`Subscriber::on_supervisor_timeout`](crate::event::Subscriber::on_supervisor_timeout)"]
330-
#![doc = r" on a Subscriber."]
331-
use crate::{
332-
application,
333-
event::{builder::SocketAddress, IntoEvent},
334-
};
335-
#[non_exhaustive]
336-
#[derive(Clone, Debug, Eq, PartialEq)]
337-
pub enum Outcome {
338-
#[doc = r" Allow the connection to remain open"]
339-
Continue,
340-
#[doc = r" Close the connection and notify the peer"]
341-
Close { error_code: application::Error },
342-
#[doc = r" Close the connection without notifying the peer"]
343-
ImmediateClose { reason: &'static str },
344-
}
345-
impl Default for Outcome {
346-
fn default() -> Self {
347-
Self::Continue
348-
}
349-
}
350-
#[non_exhaustive]
351-
#[derive(Debug)]
352-
pub struct Context<'a> {
353-
#[doc = r" Number of handshakes that have begun but not completed"]
354-
pub inflight_handshakes: usize,
355-
#[doc = r" Number of open connections"]
356-
pub connection_count: usize,
357-
#[doc = r" The address of the peer"]
358-
pub remote_address: SocketAddress<'a>,
359-
#[doc = r" True if the connection is in the handshake state, false otherwise"]
360-
pub is_handshaking: bool,
361-
}
362-
impl<'a> Context<'a> {
363-
pub fn new(
364-
inflight_handshakes: usize,
365-
connection_count: usize,
366-
remote_address: &'a crate::inet::SocketAddress,
367-
is_handshaking: bool,
368-
) -> Self {
369-
Self {
370-
inflight_handshakes,
371-
connection_count,
372-
remote_address: remote_address.into_event(),
373-
is_handshaking,
374-
}
375-
}
376-
}
377-
}
378325
pub use traits::*;
379326
mod traits {
380327
use super::*;
@@ -432,40 +379,6 @@ mod traits {
432379
meta: &api::ConnectionMeta,
433380
info: &api::ConnectionInfo,
434381
) -> Self::ConnectionContext;
435-
#[doc = r" The period at which `on_supervisor_timeout` is called"]
436-
#[doc = r""]
437-
#[doc = r" If multiple `event::Subscriber`s are composed together, the minimum `supervisor_timeout`"]
438-
#[doc = r" across all `event::Subscriber`s will be used."]
439-
#[doc = r""]
440-
#[doc = r" If the `supervisor_timeout()` is `None` across all `event::Subscriber`s, connection supervision"]
441-
#[doc = r" will cease for the remaining lifetime of the connection and `on_supervisor_timeout` will no longer"]
442-
#[doc = r" be called."]
443-
#[doc = r""]
444-
#[doc = r" It is recommended to avoid setting this value less than ~100ms, as short durations"]
445-
#[doc = r" may lead to higher CPU utilization."]
446-
#[allow(unused_variables)]
447-
fn supervisor_timeout(
448-
&mut self,
449-
conn_context: &mut Self::ConnectionContext,
450-
meta: &api::ConnectionMeta,
451-
context: &supervisor::Context,
452-
) -> Option<Duration> {
453-
None
454-
}
455-
#[doc = r" Called for each `supervisor_timeout` to determine any action to take on the connection based on the `supervisor::Outcome`"]
456-
#[doc = r""]
457-
#[doc = r" If multiple `event::Subscriber`s are composed together, the minimum `supervisor_timeout`"]
458-
#[doc = r" across all `event::Subscriber`s will be used, and thus `on_supervisor_timeout` may be called"]
459-
#[doc = r" earlier than the `supervisor_timeout` for a given `event::Subscriber` implementation."]
460-
#[allow(unused_variables)]
461-
fn on_supervisor_timeout(
462-
&mut self,
463-
conn_context: &mut Self::ConnectionContext,
464-
meta: &api::ConnectionMeta,
465-
context: &supervisor::Context,
466-
) -> supervisor::Outcome {
467-
supervisor::Outcome::default()
468-
}
469382
#[doc = "Called when the `ByteArrayEvent` event is triggered"]
470383
#[inline]
471384
fn on_byte_array_event(
@@ -551,50 +464,6 @@ mod traits {
551464
)
552465
}
553466
#[inline]
554-
fn supervisor_timeout(
555-
&mut self,
556-
conn_context: &mut Self::ConnectionContext,
557-
meta: &api::ConnectionMeta,
558-
context: &supervisor::Context,
559-
) -> Option<Duration> {
560-
let timeout_a = self
561-
.0
562-
.supervisor_timeout(&mut conn_context.0, meta, context);
563-
let timeout_b = self
564-
.1
565-
.supervisor_timeout(&mut conn_context.1, meta, context);
566-
match (timeout_a, timeout_b) {
567-
(None, None) => None,
568-
(None, Some(timeout)) | (Some(timeout), None) => Some(timeout),
569-
(Some(a), Some(b)) => Some(a.min(b)),
570-
}
571-
}
572-
#[inline]
573-
fn on_supervisor_timeout(
574-
&mut self,
575-
conn_context: &mut Self::ConnectionContext,
576-
meta: &api::ConnectionMeta,
577-
context: &supervisor::Context,
578-
) -> supervisor::Outcome {
579-
let outcome_a = self
580-
.0
581-
.on_supervisor_timeout(&mut conn_context.0, meta, context);
582-
let outcome_b = self
583-
.1
584-
.on_supervisor_timeout(&mut conn_context.1, meta, context);
585-
match (outcome_a, outcome_b) {
586-
(supervisor::Outcome::ImmediateClose { reason }, _)
587-
| (_, supervisor::Outcome::ImmediateClose { reason }) => {
588-
supervisor::Outcome::ImmediateClose { reason }
589-
}
590-
(supervisor::Outcome::Close { error_code }, _)
591-
| (_, supervisor::Outcome::Close { error_code }) => {
592-
supervisor::Outcome::Close { error_code }
593-
}
594-
_ => supervisor::Outcome::Continue,
595-
}
596-
}
597-
#[inline]
598467
fn on_byte_array_event(
599468
&mut self,
600469
context: &mut Self::ConnectionContext,

tools/s2n-events/tests/test_c_ffi_events.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ mod c_ffi_events;
66
pub use c_ffi_events::event;
77
use c_ffi_events::event::ConnectionPublisher;
88
use s2n_quic_core::{
9-
application,
109
event::IntoEvent,
11-
inet,
1210
time::{testing::Clock as MockClock, Clock},
1311
};
1412

0 commit comments

Comments
 (0)