Skip to main content

Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }

Implementations§

Source§

impl Connection

Source

pub fn connect( cfg: Arc<Config>, now: Instant, remote: SocketAddr, server_name: &str, token: &[u8], ) -> Result<Self, TransportError>

Starts a client connection to remote. token is a NEW_TOKEN value from an earlier connection, or empty.

Source

pub fn is_server(&self) -> bool

Source

pub fn is_established(&self) -> bool

Examples found in repository?
examples/bench_quic.rs (line 45)
32fn pair(real: bool) -> (Pair, ConnHandle, ConnHandle) {
33    let (cp, sp) = providers(real);
34    let mut c = Config::new(cp);
35    let mut s = Config::new(sp);
36    for x in [&mut c, &mut s] {
37        x.transport.max_data = 16 << 20;
38        x.transport.stream_window_bidi_local = 8 << 20;
39        x.transport.stream_window_bidi_remote = 8 << 20;
40        x.transport.send_buffer = 8 << 20;
41    }
42    let mut p = Pair::new(Arc::new(c), Arc::new(s), link(), 7);
43    p.batch = 16;
44    let ch = p.connect();
45    assert!(p.run_until(Duration::from_secs(10), |p| p.client.conn(ch).is_some_and(|c| c.is_established())
46        && p.server_conn().and_then(|s| p.server.conn(s)).is_some_and(|c| c.is_established())));
47    let sh = p.server_conn().unwrap();
48    p.run_for(Duration::from_millis(50));
49    (p, ch, sh)
50}
Source

pub fn is_handshaking(&self) -> bool

Source

pub fn is_closed(&self) -> bool

Closing or draining (no new streams or data).

Source

pub fn is_drained(&self) -> bool

The connection can be dropped.

Source

pub fn error(&self) -> Option<&ConnectionError>

Source

pub fn alpn(&self) -> Option<&[u8]>

Source

pub fn tls(&self) -> &dyn CryptoSession

The TLS session: SNI, cipher suite, the peer’s certificate chain.

Source

pub fn remote_address(&self) -> SocketAddr

Source

pub fn path_validated(&self) -> bool

Source

pub fn peer_params(&self) -> Option<&TransportParams>

Source

pub fn version(&self) -> u32

Source

pub fn stats(&self) -> Stats

Examples found in repository?
examples/bench_quic.rs (line 107)
53fn small(real: bool, n: usize, size: usize, conc: usize) -> f64 {
54    let (mut p, c, s) = pair(real);
55    let req = vec![7u8; size];
56    let mut buf = vec![0u8; 65536];
57    let mut open: Vec<(u64, usize)> = Vec::new(); // (stream, bytes received back)
58    let mut started = 0;
59    let mut done = 0;
60    let mut server_got: std::collections::HashMap<u64, usize> = Default::default();
61    let t = Instant::now();
62    while done < n {
63        while open.len() < conc && started < n {
64            let Ok(id) = p.client.conn_mut(c).unwrap().open_bi() else { break };
65            p.client.conn_mut(c).unwrap().stream_send(id, &req, true).unwrap();
66            open.push((id, 0));
67            started += 1;
68        }
69        p.step();
70        // Server: read requests, answer when complete.
71        while let Some(e) = p.server.poll_event(s) {
72            if let simoom_quic::Event::StreamReadable(id) = e {
73                let conn = p.server.conn_mut(s).unwrap();
74                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
75                    *server_got.entry(id).or_default() += k;
76                    if fin {
77                        server_got.remove(&id);
78                        conn.stream_send(id, &req, true).unwrap();
79                        break;
80                    }
81                    if k == 0 {
82                        break;
83                    }
84                }
85            }
86        }
87        while let Some(e) = p.client.poll_event(c) {
88            if let simoom_quic::Event::StreamReadable(id) = e {
89                let conn = p.client.conn_mut(c).unwrap();
90                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
91                    if let Some(o) = open.iter_mut().find(|o| o.0 == id) {
92                        o.1 += k;
93                    }
94                    if fin {
95                        open.retain(|o| o.0 != id);
96                        done += 1;
97                        break;
98                    }
99                    if k == 0 {
100                        break;
101                    }
102                }
103            }
104        }
105    }
106    let us = t.elapsed().as_secs_f64() * 1e6 / n as f64;
107    let pk = p.client.conn(c).unwrap().stats().sent_packets + p.server.conn(s).unwrap().stats().sent_packets;
108    eprintln!("    ({n} exchanges, {:.2} packets per exchange)", pk as f64 / n as f64);
109    us
110}
111
112/// One stream of `mb` MiB client to server; returns MB/s.
113fn bulk(real: bool, mb: usize) -> (f64, u64) {
114    let (mut p, c, s) = pair(real);
115    let chunk = vec![1u8; 256 << 10];
116    let total = mb << 20;
117    let id = p.client.conn_mut(c).unwrap().open_uni().unwrap();
118    let mut sent = 0;
119    let mut got = 0;
120    let mut buf = vec![0u8; 256 << 10];
121    let t = Instant::now();
122    while got < total {
123        if sent < total {
124            let conn = p.client.conn_mut(c).unwrap();
125            let want = (total - sent).min(chunk.len());
126            if let Ok(k) = conn.stream_send(id, &chunk[..want], sent + want == total) {
127                sent += k;
128            }
129        }
130        p.step();
131        if let Some(conn) = p.server.conn_mut(s) {
132            while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
133                got += k;
134                if fin || k == 0 {
135                    break;
136                }
137            }
138        }
139        while p.server.poll_event(s).is_some() {}
140        while p.client.poll_event(c).is_some() {}
141    }
142    let secs = t.elapsed().as_secs_f64();
143    (total as f64 / secs / 1e6, p.client.conn(c).unwrap().stats().lost_packets)
144}
Source

pub fn local_cids(&self) -> impl Iterator<Item = Cid> + '_

Our connection IDs currently active.

Source

pub fn remote_cid(&self) -> Cid

The destination connection ID in use.

Source

pub fn original_dcid(&self) -> Cid

The original destination connection ID (client’s first DCID).

Source

pub fn new_token(&self) -> Option<&[u8]>

Client: a token received in NEW_TOKEN, for a later connection.

Source

pub fn poll_event(&mut self) -> Option<Event>

Source

pub fn poll_endpoint_event(&mut self) -> Option<EndpointEvent>

Source

pub fn stream_count(&self) -> usize

Streams currently tracked.

Source

pub fn open_bi(&mut self) -> Result<u64, StreamError>

Opens a bidirectional stream.

Examples found in repository?
examples/bench_quic.rs (line 64)
53fn small(real: bool, n: usize, size: usize, conc: usize) -> f64 {
54    let (mut p, c, s) = pair(real);
55    let req = vec![7u8; size];
56    let mut buf = vec![0u8; 65536];
57    let mut open: Vec<(u64, usize)> = Vec::new(); // (stream, bytes received back)
58    let mut started = 0;
59    let mut done = 0;
60    let mut server_got: std::collections::HashMap<u64, usize> = Default::default();
61    let t = Instant::now();
62    while done < n {
63        while open.len() < conc && started < n {
64            let Ok(id) = p.client.conn_mut(c).unwrap().open_bi() else { break };
65            p.client.conn_mut(c).unwrap().stream_send(id, &req, true).unwrap();
66            open.push((id, 0));
67            started += 1;
68        }
69        p.step();
70        // Server: read requests, answer when complete.
71        while let Some(e) = p.server.poll_event(s) {
72            if let simoom_quic::Event::StreamReadable(id) = e {
73                let conn = p.server.conn_mut(s).unwrap();
74                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
75                    *server_got.entry(id).or_default() += k;
76                    if fin {
77                        server_got.remove(&id);
78                        conn.stream_send(id, &req, true).unwrap();
79                        break;
80                    }
81                    if k == 0 {
82                        break;
83                    }
84                }
85            }
86        }
87        while let Some(e) = p.client.poll_event(c) {
88            if let simoom_quic::Event::StreamReadable(id) = e {
89                let conn = p.client.conn_mut(c).unwrap();
90                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
91                    if let Some(o) = open.iter_mut().find(|o| o.0 == id) {
92                        o.1 += k;
93                    }
94                    if fin {
95                        open.retain(|o| o.0 != id);
96                        done += 1;
97                        break;
98                    }
99                    if k == 0 {
100                        break;
101                    }
102                }
103            }
104        }
105    }
106    let us = t.elapsed().as_secs_f64() * 1e6 / n as f64;
107    let pk = p.client.conn(c).unwrap().stats().sent_packets + p.server.conn(s).unwrap().stats().sent_packets;
108    eprintln!("    ({n} exchanges, {:.2} packets per exchange)", pk as f64 / n as f64);
109    us
110}
Source

pub fn open_uni(&mut self) -> Result<u64, StreamError>

Opens a unidirectional stream.

Examples found in repository?
examples/bench_quic.rs (line 117)
113fn bulk(real: bool, mb: usize) -> (f64, u64) {
114    let (mut p, c, s) = pair(real);
115    let chunk = vec![1u8; 256 << 10];
116    let total = mb << 20;
117    let id = p.client.conn_mut(c).unwrap().open_uni().unwrap();
118    let mut sent = 0;
119    let mut got = 0;
120    let mut buf = vec![0u8; 256 << 10];
121    let t = Instant::now();
122    while got < total {
123        if sent < total {
124            let conn = p.client.conn_mut(c).unwrap();
125            let want = (total - sent).min(chunk.len());
126            if let Ok(k) = conn.stream_send(id, &chunk[..want], sent + want == total) {
127                sent += k;
128            }
129        }
130        p.step();
131        if let Some(conn) = p.server.conn_mut(s) {
132            while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
133                got += k;
134                if fin || k == 0 {
135                    break;
136                }
137            }
138        }
139        while p.server.poll_event(s).is_some() {}
140        while p.client.poll_event(c).is_some() {}
141    }
142    let secs = t.elapsed().as_secs_f64();
143    (total as f64 / secs / 1e6, p.client.conn(c).unwrap().stats().lost_packets)
144}
Source

pub fn stream_send( &mut self, id: u64, data: &[u8], fin: bool, ) -> Result<usize, StreamError>

Queues data; returns the number of bytes accepted (backpressure: fewer than given).

Examples found in repository?
examples/bench_quic.rs (line 65)
53fn small(real: bool, n: usize, size: usize, conc: usize) -> f64 {
54    let (mut p, c, s) = pair(real);
55    let req = vec![7u8; size];
56    let mut buf = vec![0u8; 65536];
57    let mut open: Vec<(u64, usize)> = Vec::new(); // (stream, bytes received back)
58    let mut started = 0;
59    let mut done = 0;
60    let mut server_got: std::collections::HashMap<u64, usize> = Default::default();
61    let t = Instant::now();
62    while done < n {
63        while open.len() < conc && started < n {
64            let Ok(id) = p.client.conn_mut(c).unwrap().open_bi() else { break };
65            p.client.conn_mut(c).unwrap().stream_send(id, &req, true).unwrap();
66            open.push((id, 0));
67            started += 1;
68        }
69        p.step();
70        // Server: read requests, answer when complete.
71        while let Some(e) = p.server.poll_event(s) {
72            if let simoom_quic::Event::StreamReadable(id) = e {
73                let conn = p.server.conn_mut(s).unwrap();
74                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
75                    *server_got.entry(id).or_default() += k;
76                    if fin {
77                        server_got.remove(&id);
78                        conn.stream_send(id, &req, true).unwrap();
79                        break;
80                    }
81                    if k == 0 {
82                        break;
83                    }
84                }
85            }
86        }
87        while let Some(e) = p.client.poll_event(c) {
88            if let simoom_quic::Event::StreamReadable(id) = e {
89                let conn = p.client.conn_mut(c).unwrap();
90                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
91                    if let Some(o) = open.iter_mut().find(|o| o.0 == id) {
92                        o.1 += k;
93                    }
94                    if fin {
95                        open.retain(|o| o.0 != id);
96                        done += 1;
97                        break;
98                    }
99                    if k == 0 {
100                        break;
101                    }
102                }
103            }
104        }
105    }
106    let us = t.elapsed().as_secs_f64() * 1e6 / n as f64;
107    let pk = p.client.conn(c).unwrap().stats().sent_packets + p.server.conn(s).unwrap().stats().sent_packets;
108    eprintln!("    ({n} exchanges, {:.2} packets per exchange)", pk as f64 / n as f64);
109    us
110}
111
112/// One stream of `mb` MiB client to server; returns MB/s.
113fn bulk(real: bool, mb: usize) -> (f64, u64) {
114    let (mut p, c, s) = pair(real);
115    let chunk = vec![1u8; 256 << 10];
116    let total = mb << 20;
117    let id = p.client.conn_mut(c).unwrap().open_uni().unwrap();
118    let mut sent = 0;
119    let mut got = 0;
120    let mut buf = vec![0u8; 256 << 10];
121    let t = Instant::now();
122    while got < total {
123        if sent < total {
124            let conn = p.client.conn_mut(c).unwrap();
125            let want = (total - sent).min(chunk.len());
126            if let Ok(k) = conn.stream_send(id, &chunk[..want], sent + want == total) {
127                sent += k;
128            }
129        }
130        p.step();
131        if let Some(conn) = p.server.conn_mut(s) {
132            while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
133                got += k;
134                if fin || k == 0 {
135                    break;
136                }
137            }
138        }
139        while p.server.poll_event(s).is_some() {}
140        while p.client.poll_event(c).is_some() {}
141    }
142    let secs = t.elapsed().as_secs_f64();
143    (total as f64 / secs / 1e6, p.client.conn(c).unwrap().stats().lost_packets)
144}
Source

pub fn stream_finish(&mut self, id: u64) -> Result<(), StreamError>

Ends the send side.

Source

pub fn stream_recv( &mut self, id: u64, out: &mut [u8], ) -> Result<(usize, bool), StreamError>

Reads data; returns (bytes, fin).

Examples found in repository?
examples/bench_quic.rs (line 74)
53fn small(real: bool, n: usize, size: usize, conc: usize) -> f64 {
54    let (mut p, c, s) = pair(real);
55    let req = vec![7u8; size];
56    let mut buf = vec![0u8; 65536];
57    let mut open: Vec<(u64, usize)> = Vec::new(); // (stream, bytes received back)
58    let mut started = 0;
59    let mut done = 0;
60    let mut server_got: std::collections::HashMap<u64, usize> = Default::default();
61    let t = Instant::now();
62    while done < n {
63        while open.len() < conc && started < n {
64            let Ok(id) = p.client.conn_mut(c).unwrap().open_bi() else { break };
65            p.client.conn_mut(c).unwrap().stream_send(id, &req, true).unwrap();
66            open.push((id, 0));
67            started += 1;
68        }
69        p.step();
70        // Server: read requests, answer when complete.
71        while let Some(e) = p.server.poll_event(s) {
72            if let simoom_quic::Event::StreamReadable(id) = e {
73                let conn = p.server.conn_mut(s).unwrap();
74                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
75                    *server_got.entry(id).or_default() += k;
76                    if fin {
77                        server_got.remove(&id);
78                        conn.stream_send(id, &req, true).unwrap();
79                        break;
80                    }
81                    if k == 0 {
82                        break;
83                    }
84                }
85            }
86        }
87        while let Some(e) = p.client.poll_event(c) {
88            if let simoom_quic::Event::StreamReadable(id) = e {
89                let conn = p.client.conn_mut(c).unwrap();
90                while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
91                    if let Some(o) = open.iter_mut().find(|o| o.0 == id) {
92                        o.1 += k;
93                    }
94                    if fin {
95                        open.retain(|o| o.0 != id);
96                        done += 1;
97                        break;
98                    }
99                    if k == 0 {
100                        break;
101                    }
102                }
103            }
104        }
105    }
106    let us = t.elapsed().as_secs_f64() * 1e6 / n as f64;
107    let pk = p.client.conn(c).unwrap().stats().sent_packets + p.server.conn(s).unwrap().stats().sent_packets;
108    eprintln!("    ({n} exchanges, {:.2} packets per exchange)", pk as f64 / n as f64);
109    us
110}
111
112/// One stream of `mb` MiB client to server; returns MB/s.
113fn bulk(real: bool, mb: usize) -> (f64, u64) {
114    let (mut p, c, s) = pair(real);
115    let chunk = vec![1u8; 256 << 10];
116    let total = mb << 20;
117    let id = p.client.conn_mut(c).unwrap().open_uni().unwrap();
118    let mut sent = 0;
119    let mut got = 0;
120    let mut buf = vec![0u8; 256 << 10];
121    let t = Instant::now();
122    while got < total {
123        if sent < total {
124            let conn = p.client.conn_mut(c).unwrap();
125            let want = (total - sent).min(chunk.len());
126            if let Ok(k) = conn.stream_send(id, &chunk[..want], sent + want == total) {
127                sent += k;
128            }
129        }
130        p.step();
131        if let Some(conn) = p.server.conn_mut(s) {
132            while let Ok((k, fin)) = conn.stream_recv(id, &mut buf) {
133                got += k;
134                if fin || k == 0 {
135                    break;
136                }
137            }
138        }
139        while p.server.poll_event(s).is_some() {}
140        while p.client.poll_event(c).is_some() {}
141    }
142    let secs = t.elapsed().as_secs_f64();
143    (total as f64 / secs / 1e6, p.client.conn(c).unwrap().stats().lost_packets)
144}
Source

pub fn stream_reset(&mut self, id: u64, code: u64) -> Result<(), StreamError>

Source

pub fn stream_stop(&mut self, id: u64, code: u64) -> Result<(), StreamError>

Source

pub fn stream_writable(&self, id: u64) -> Option<u64>

Bytes stream_send would accept now.

Source

pub fn stream_unacked(&self, id: u64) -> Option<u64>

Bytes written to the stream but not yet acknowledged.

Source

pub fn stream_exists(&self, id: u64) -> bool

Source

pub fn ping(&mut self)

Sends a PING (keep-alive).

Source

pub fn close(&mut self, now: Instant, code: u64, reason: &[u8])

Closes the connection with an application error code.

Source

pub fn recv(&mut self, now: Instant, remote: SocketAddr, data: &mut [u8])

Processes one received datagram (may hold coalesced packets). data is decrypted in place.

Source

pub fn initiate_key_update(&mut self) -> bool

Starts a key update (RFC 9001 §6.1). Returns false when one is not allowed yet.

Source

pub fn rotate_remote_cid(&mut self) -> bool

Switches to an unused destination connection ID and retires the current one. Returns false if none is available.

Source

pub fn poll_timeout(&self) -> Option<Instant>

The next time Connection::handle_timeout must be called.

Source

pub fn handle_timeout(&mut self, now: Instant)

Source

pub fn poll_transmit( &mut self, now: Instant, buf: &mut Vec<u8>, max_datagrams: usize, ) -> Option<Transmit>

Writes up to max_datagrams datagrams into buf (cleared first).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.