Debian Codegraph @

third_party/rust/neqo-http3/src/features/mod.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::{fmt::Debug, mem};
8
9 use neqo_common::qtrace;
10
11 use crate::{
12 client_events::Http3ClientEvents,
13 settings::{HSettingType, HSettings},
14 };
15
16 pub mod extended_connect;
17
18 /// States:
19 /// - `Disable` - it is not turned on for this connection.
20 /// - `Negotiating` - the feature is enabled locally, but settings from the peer have not been
21 /// received yet.
22 /// - `Negotiated` - the settings have been received and both sides support the feature.
23 /// - `NegotiationFailed` - the settings have been received and the peer does not support the
24 /// feature.
25 #[derive(Debug)]
26 pub enum NegotiationState {
27 Disabled,
28 Negotiating {
29 feature_type: HSettingType,
30 listener: Option<Http3ClientEvents>,
31 },
32 Negotiated,
33 NegotiationFailed,
34 }
35
36 impl NegotiationState {
37 #[must_use]
38 pub const fn new(enable: bool, feature_type: HSettingType) -> Self {
39 if enable {
40 Self::Negotiating {
41 feature_type,
42 listener: None,
43 }
44 } else {
45 Self::Disabled
46 }
47 }
48
49 pub fn set_listener(&mut self, new_listener: Http3ClientEvents) {
50 if let Self::Negotiating { listener, .. } = self {
51 *listener = Some(new_listener);
52 }
53 }
54
55 pub fn handle_settings(&mut self, settings: &HSettings) {
56 if !self.locally_enabled() {
57 return;
58 }
59
60 if let Self::Negotiating {
61 feature_type,
62 listener,
63 } = self
64 {
65 qtrace!(
66 "set_negotiated {feature_type:?} to {}",
67 settings.get(*feature_type)
68 );
69 let cb = mem::take(listener);
70 let ft = *feature_type;
71 *self = if settings.get(ft) == 1 {
72 Self::Negotiated
73 } else {
74 Self::NegotiationFailed
75 };
76 if let Some(l) = cb {
77 l.negotiation_done(ft, self.enabled());
78 }
79 }
80 }
81
82 #[must_use]
83 pub const fn enabled(&self) -> bool {
84 matches!(self, &Self::Negotiated)
85 }
86
87 #[must_use]
88 pub const fn locally_enabled(&self) -> bool {
89 !matches!(self, &Self::Disabled)
90 }
91 }