Struct Connection
pub struct Connection(/* private fields */);quic and std only.Expand description
A QUIC connection.
If all references to a connection (including every clone of the Connection handle, streams of
incoming streams, and the various stream types) have been dropped, then the connection will be
automatically closed with an error_code of 0 and an empty reason. You can also close the
connection explicitly by calling Connection::close().
Closing the connection immediately abandons efforts to deliver data to the peer. Upon
receiving CONNECTION_CLOSE the peer may drop any stream data not yet delivered to the
application. Connection::close() describes in more detail how to gracefully close a
connection without losing application data.
May be cloned to obtain another handle to the same connection.
Implementations§
§impl Connection
impl Connection
pub fn open_uni(&self) -> OpenUni<'_> ⓘ
pub fn open_uni(&self) -> OpenUni<'_> ⓘ
Initiate a new outgoing unidirectional stream.
Streams are cheap and instantaneous to open unless blocked by flow control. As a consequence, the peer won’t be notified that a stream has been opened until the stream is actually used.
pub fn open_bi(&self) -> OpenBi<'_> ⓘ
pub fn open_bi(&self) -> OpenBi<'_> ⓘ
Initiate a new outgoing bidirectional stream.
Streams are cheap and instantaneous to open unless blocked by flow control. As a
consequence, the peer won’t be notified that a stream has been opened until the stream is
actually used. Calling open_bi() then waiting on the RecvStream without writing
anything to SendStream will never succeed.
pub fn accept_uni(&self) -> AcceptUni<'_> ⓘ
pub fn accept_uni(&self) -> AcceptUni<'_> ⓘ
Accept the next incoming uni-directional stream
pub fn accept_bi(&self) -> AcceptBi<'_> ⓘ
pub fn accept_bi(&self) -> AcceptBi<'_> ⓘ
Accept the next incoming bidirectional stream
Important Note: The Connection that calls open_bi() must write to its SendStream
before the other Connection is able to accept_bi(). Calling open_bi() then
waiting on the RecvStream without writing anything to SendStream will never succeed.
pub fn read_datagram(&self) -> ReadDatagram<'_> ⓘ
pub fn read_datagram(&self) -> ReadDatagram<'_> ⓘ
Receive an application datagram
pub async fn closed(&self) -> ConnectionError
pub async fn closed(&self) -> ConnectionError
Wait for the connection to be closed for any reason
Despite the return type’s name, closed connections are often not an error condition at the
application layer. Cases that might be routine include ConnectionError::LocallyClosed
and ConnectionError::ApplicationClosed.
pub fn close_reason(&self) -> Option<ConnectionError>
pub fn close_reason(&self) -> Option<ConnectionError>
If the connection is closed, the reason why.
Returns None if the connection is still open.
pub fn close(&self, error_code: VarInt, reason: &[u8])
pub fn close(&self, error_code: VarInt, reason: &[u8])
Close the connection immediately.
Pending operations will fail immediately with ConnectionError::LocallyClosed. No
more data is sent to the peer and the peer may drop buffered data upon receiving
the CONNECTION_CLOSE frame.
error_code and reason are not interpreted, and are provided directly to the peer.
reason will be truncated to fit in a single packet with overhead; to improve odds that it
is preserved in full, it should be kept under 1KiB.
§Gracefully closing a connection
Only the peer last receiving application data can be certain that all data is
delivered. The only reliable action it can then take is to close the connection,
potentially with a custom error code. The delivery of the final CONNECTION_CLOSE
frame is very likely if both endpoints stay online long enough, and
Endpoint::wait_idle() can be used to provide sufficient time. Otherwise, the
remote peer will time out the connection, provided that the idle timeout is not
disabled.
The sending side can not guarantee all stream data is delivered to the remote
application. It only knows the data is delivered to the QUIC stack of the remote
endpoint. Once the local side sends a CONNECTION_CLOSE frame in response to calling
close() the remote endpoint may drop any data it received but is as yet
undelivered to the application, including data that was acknowledged as received to
the local endpoint.
pub async fn handshake_confirmed(&self) -> Result<(), ConnectionError>
pub async fn handshake_confirmed(&self) -> Result<(), ConnectionError>
Wait for the handshake to be confirmed.
As a server, who must be authenticated by clients, this happens when the handshake completes upon receiving a TLS Finished message from the client. In return, the server send a HANDSHAKE_DONE frame.
As a client, this happens when receiving a HANDSHAKE_DONE frame. At this point, the server has either accepted our authentication, or, if client authentication is not required, accepted our lack of authentication.
pub fn send_datagram(&self, data: Bytes) -> Result<(), SendDatagramError>
pub fn send_datagram(&self, data: Bytes) -> Result<(), SendDatagramError>
Transmit data as an unreliable, unordered application datagram
Application datagrams are a low-level primitive. They may be lost or delivered out of order,
and data must both fit inside a single QUIC packet and be smaller than the maximum
dictated by the peer.
Previously queued datagrams which are still unsent may be discarded to make space for this datagram, in order of oldest to newest.
pub fn send_datagram_wait(&self, data: Bytes) -> SendDatagram<'_> ⓘ
pub fn send_datagram_wait(&self, data: Bytes) -> SendDatagram<'_> ⓘ
Transmit data as an unreliable, unordered application datagram
Unlike send_datagram(), this method will wait for buffer space during congestion
conditions, which effectively prioritizes old datagrams over new datagrams.
See send_datagram() for details.
pub fn max_datagram_size(&self) -> Option<usize>
pub fn max_datagram_size(&self) -> Option<usize>
Compute the maximum size of datagrams that may be passed to send_datagram().
Returns None if datagrams are unsupported by the peer or disabled locally.
This may change over the lifetime of a connection according to variation in the path MTU estimate. The peer can also enforce an arbitrarily small fixed limit, but if the peer’s limit is large this is guaranteed to be a little over a kilobyte at minimum.
Not necessarily the maximum size of received datagrams.
pub fn datagram_send_buffer_space(&self) -> usize
pub fn datagram_send_buffer_space(&self) -> usize
Bytes available in the outgoing datagram buffer
When greater than zero, calling send_datagram() with a datagram of
at most this size is guaranteed not to cause older datagrams to be dropped.
pub fn side(&self) -> Side
pub fn side(&self) -> Side
Which side of the connection this is: the one that opened it, or the one that accepted it.
pub fn remote_address(&self) -> SocketAddr
pub fn remote_address(&self) -> SocketAddr
The peer’s UDP address
If ServerConfig::migration is true, clients may change addresses at will, e.g. when
switching to a cellular internet connection.
pub fn local_ip(&self) -> Option<IpAddr>
pub fn local_ip(&self) -> Option<IpAddr>
The local IP address which was used when the peer established the connection
This can be different from the address the endpoint is bound to, in case
the endpoint is bound to a wildcard address like 0.0.0.0 or ::.
This will return None for clients, or when the platform does not expose this
information. See rama_udp::DatagramCapabilities::receive_local_ip for platform
support.
pub fn stats(&self) -> ConnectionStats
pub fn stats(&self) -> ConnectionStats
Returns connection statistics
pub fn handshake_data(&self) -> Option<HandshakeSummary>
pub fn handshake_data(&self) -> Option<HandshakeSummary>
Parameters negotiated during the handshake
Guaranteed to return Some on fully established connections or after
Connecting::handshake_data() succeeds. See that method’s documentations for details on
the returned value.
pub fn peer_identity(&self) -> Option<Vec<CertificateDer<'static>>>
pub fn peer_identity(&self) -> Option<Vec<CertificateDer<'static>>>
The certificate chain the peer presented, leaf first, if it presented one.
pub fn stable_id(&self) -> usize
pub fn stable_id(&self) -> usize
A stable identifier for this connection
Peer addresses and connection IDs can change, but this value will remain fixed for the lifetime of the connection.
pub fn force_key_update(&self) -> bool
pub fn force_key_update(&self) -> bool
Update traffic keys now, without waiting for the usage limit that would force one.
Answers whether an update was started. Nothing changes and the answer is false when the
connection is not established, when the handshake is not confirmed yet (RFC 9001 §6.1
forbids initiating an update before then, which for a client means after HANDSHAKE_DONE),
or when an update is already in flight (§6 allows one at a time).
ConnectionStats::key_updates counts the updates
this connection has made, whichever side asked for them.
pub fn export_keying_material(
&self,
output: &mut [u8],
label: &[u8],
context: &[u8],
) -> Result<(), ExportKeyingMaterialError>
pub fn export_keying_material( &self, output: &mut [u8], label: &[u8], context: &[u8], ) -> Result<(), ExportKeyingMaterialError>
Derive keying material from this connection’s TLS session secrets.
Two peers calling this with the same label, the same context and output buffers of
equal length get the same bytes. The bytes are cryptographically strong and pseudorandom,
suitable as keying material. A different label or a different context gives different
bytes.
TLS 1.3 defines this exporter in RFC 8446 §7.5; RFC 5705 defined the earlier one it replaces.
pub fn driver_stats(&self) -> DriverStats
pub fn driver_stats(&self) -> DriverStats
Socket and receive-queue statistics counted by the driver for this connection.
What the protocol engine counts is Connection::stats.
pub fn set_max_concurrent_uni_streams(&self, count: VarInt)
pub fn set_max_concurrent_uni_streams(&self, count: VarInt)
Modify the number of remotely initiated unidirectional streams that may be concurrently open
No streams may be opened by the peer unless fewer than count are already open. Large
counts increase both minimum and worst-case memory consumption.
pub fn max_concurrent_streams(&self, dir: Dir) -> u64
pub fn max_concurrent_streams(&self, dir: Dir) -> u64
How many remotely initiated streams of dir may be open at once, as this side has
allowed them.
Lowering the target with
set_max_concurrent_streams does not take
effect at once: the number falls by one as each open stream of that direction closes.
pub fn remote_open_streams(&self, dir: Dir) -> u64
pub fn remote_open_streams(&self, dir: Dir) -> u64
How many remotely initiated streams of dir are open, including those this side has
not accepted yet. They count against
max_concurrent_streams.
pub fn initial_local_id(&self) -> ConnectionId
pub fn initial_local_id(&self) -> ConnectionId
The connection ID this endpoint’s generator made for the handshake, from the generator
EndpointConfig::set_cid_generator
installed.
A connection issues further identifiers as it runs and retires this one in time, so several may be usable at once and this is not “the one in use”. It stays the same for the life of the connection, which is what makes it worth reporting.
pub fn trace_id(&self) -> ConnectionId
pub fn trace_id(&self) -> ConnectionId
The connection ID that names this connection in a qlog trace: the destination the client chose for its first Initial, which both ends know and neither changes.
pub fn qlog_control(&self) -> Option<ConnectionQlogControl>
pub fn qlog_control(&self) -> Option<ConnectionQlogControl>
Control this connection’s configured qlog sink. The handle can outlive the
connection and can be inserted into Rama extensions for an application-level trigger.
Returns None when no qlog sink was configured before creating the connection.
pub fn path_changed(&self)
pub fn path_changed(&self)
Tell the connection its network path changed, so the congestion controller, the round-trip estimate and MTU discovery start again from the transport configuration.
Use it when something outside QUIC says the path is a different one, such as a change of interface. The connection detects a peer’s own move on its own.
pub fn set_send_window(&self, send_window: u64)
pub fn set_send_window(&self, send_window: u64)
Set the maximum data this connection keeps in flight, as
TransportConfig::set_send_window does
before it is established.
pub fn set_receive_window(&self, receive_window: VarInt)
pub fn set_receive_window(&self, receive_window: VarInt)
Set the flow control window this connection advertises, as
TransportConfig::set_receive_window does
before it is established.
pub fn set_max_concurrent_bi_streams(&self, count: VarInt)
pub fn set_max_concurrent_bi_streams(&self, count: VarInt)
Modify the number of remotely initiated bidirectional streams that may be concurrently open
No streams may be opened by the peer unless fewer than count are already open. Large
counts increase both minimum and worst-case memory consumption.
Trait Implementations§
§impl Clone for Connection
impl Clone for Connection
§fn clone(&self) -> Connection
fn clone(&self) -> Connection
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for Connection
impl !UnwindSafe for Connection
impl Freeze for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl UnsafeUnpin for Connection
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more