pub struct Connection(/* private fields */);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§
Source§impl Connection
impl Connection
Sourcepub fn weak_handle(&self) -> WeakConnectionHandle
pub fn weak_handle(&self) -> WeakConnectionHandle
Returns a weak reference to the inner connection struct.
Sourcepub 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.
Sourcepub 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.
Sourcepub fn accept_uni(&self) -> AcceptUni<'_> ⓘ
pub fn accept_uni(&self) -> AcceptUni<'_> ⓘ
Accept the next incoming uni-directional stream
Sourcepub 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.
Sourcepub fn read_datagram(&self) -> ReadDatagram<'_> ⓘ
pub fn read_datagram(&self) -> ReadDatagram<'_> ⓘ
Receive an application datagram
Sourcepub fn open_path_ensure(
&self,
addr: SocketAddr,
initial_status: PathStatus,
) -> OpenPath ⓘ
pub fn open_path_ensure( &self, addr: SocketAddr, initial_status: PathStatus, ) -> OpenPath ⓘ
Opens a new path if no path exists yet for the remote address.
Otherwise behaves exactly as open_path.
Sourcepub fn open_path(
&self,
addr: SocketAddr,
initial_status: PathStatus,
) -> OpenPath ⓘ
pub fn open_path( &self, addr: SocketAddr, initial_status: PathStatus, ) -> OpenPath ⓘ
Opens an additional path if the multipath extension is negotiated.
The returned future completes once the path is either fully opened and ready to carry application data, or if there was an error.
Dropping the returned future does not cancel the opening of the path, the
PathEvent::Opened event will still be emitted from Self::path_events if the
path opens. The PathId for the events can be extracted from
OpenPath::path_id.
Failure to open a path can either occur immediately, before polling the returned
future, or at a later time. If the failure is immediate OpenPath::path_id will
return None and the future will be ready immediately. If the failure happens
later, a PathEvent will be emitted.
Sourcepub fn path_events(&self) -> Receiver<PathEvent>
pub fn path_events(&self) -> Receiver<PathEvent>
A broadcast receiver of PathEvents for all paths in this connection
Sourcepub fn nat_traversal_updates(&self) -> Receiver<Event>
pub fn nat_traversal_updates(&self) -> Receiver<Event>
A broadcast receiver of iroh_hp::Events for updates about server addresses
Sourcepub 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.
Sourcepub fn on_closed(&self) -> OnClosed ⓘ
pub fn on_closed(&self) -> OnClosed ⓘ
Wait for the connection to be closed without keeping a strong reference to the connection
Returns a future that resolves, once the connection is closed, to a tuple of
(ConnectionError, ConnectionStats).
Calling Self::closed keeps the connection alive until it is either closed locally via Connection::close
or closed by the remote peer. This function instead does not keep the connection itself alive,
so if all other clones of the connection are dropped, the connection will be closed implicitly even
if there are futures returned from this function still being awaited.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
Sourcepub 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.
If multipath is enabled this will return the address of any
path, and may not be consistent. Prefer Path::remote_address instead.
Sourcepub 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 quinn_udp::RecvMeta::dst_ip for a list of
supported platforms when using quinn_udp for I/O, which is the default.
Sourcepub fn rtt(&self, path_id: PathId) -> Option<Duration>
pub fn rtt(&self, path_id: PathId) -> Option<Duration>
Current best estimate of this connection’s latency (round-trip-time)
Sourcepub fn stats(&self) -> ConnectionStats
pub fn stats(&self) -> ConnectionStats
Returns connection statistics
Sourcepub fn path_stats(&self, path_id: PathId) -> Option<PathStats>
pub fn path_stats(&self, path_id: PathId) -> Option<PathStats>
Returns path statistics
Sourcepub fn congestion_state(&self, path_id: PathId) -> Option<Box<dyn Controller>>
pub fn congestion_state(&self, path_id: PathId) -> Option<Box<dyn Controller>>
Current state of the congestion control algorithm, for debugging purposes
Sourcepub fn handshake_data(&self) -> Option<Box<dyn Any>>
pub fn handshake_data(&self) -> Option<Box<dyn Any>>
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.
Sourcepub fn peer_identity(&self) -> Option<Box<dyn Any>>
pub fn peer_identity(&self) -> Option<Box<dyn Any>>
Sourcepub 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.
Sourcepub fn force_key_update(&self)
pub fn force_key_update(&self)
Update traffic keys spontaneously
This primarily exists for testing purposes.
Sourcepub 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.
When both peers call this method with the same label and context
arguments and output buffers of equal length, they will get the
same sequence of bytes in output. These bytes are cryptographically
strong and pseudorandom, and are suitable for use as keying material.
See RFC5705 for more information.
Sourcepub 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.
Sourcepub fn set_send_window(&self, send_window: u64)
pub fn set_send_window(&self, send_window: u64)
Sourcepub fn set_receive_window(&self, receive_window: VarInt)
pub fn set_receive_window(&self, receive_window: VarInt)
Sourcepub 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.
Sourcepub fn observed_external_addr(&self) -> Receiver<Option<SocketAddr>>
pub fn observed_external_addr(&self) -> Receiver<Option<SocketAddr>>
Track changed on our external address as reported by the peer.
Sourcepub fn is_multipath_enabled(&self) -> bool
pub fn is_multipath_enabled(&self) -> bool
Is multipath enabled?
Sourcepub fn add_nat_traversal_address(
&self,
address: SocketAddr,
) -> Result<(), Error>
pub fn add_nat_traversal_address( &self, address: SocketAddr, ) -> Result<(), Error>
Registers one address at which this endpoint might be reachable
When the NAT traversal extension is negotiated, servers send these addresses to clients in
ADD_ADDRESS frames. This allows clients to obtain server address candidates to initiate
NAT traversal attempts. Clients provide their own reachable addresses in REACH_OUT frames
when Self::initiate_nat_traversal_round is called.
Sourcepub fn remove_nat_traversal_address(
&self,
address: SocketAddr,
) -> Result<(), Error>
pub fn remove_nat_traversal_address( &self, address: SocketAddr, ) -> Result<(), Error>
Removes one or more addresses from the set of addresses at which this endpoint is reachable
When the NAT traversal extension is negotiated, servers send address removals to
clients in REMOVE_ADDRESS frames. This allows clients to stop using outdated
server address candidates that are no longer valid for NAT traversal.
For clients, removed addresses will no longer be advertised in REACH_OUT frames.
Addresses not present in the set will be silently ignored.
Sourcepub fn get_local_nat_traversal_addresses(
&self,
) -> Result<Vec<SocketAddr>, Error>
pub fn get_local_nat_traversal_addresses( &self, ) -> Result<Vec<SocketAddr>, Error>
Get the current local nat traversal addresses
Sourcepub fn get_remote_nat_traversal_addresses(
&self,
) -> Result<Vec<SocketAddr>, Error>
pub fn get_remote_nat_traversal_addresses( &self, ) -> Result<Vec<SocketAddr>, Error>
Get the currently advertised nat traversal addresses by the server
Sourcepub fn initiate_nat_traversal_round(&self) -> Result<Vec<SocketAddr>, Error>
pub fn initiate_nat_traversal_round(&self) -> Result<Vec<SocketAddr>, Error>
Initiates a new nat traversal round
A nat traversal round involves advertising the client’s local addresses in REACH_OUT
frames, and initiating probing of the known remote addresses. When a new round is
initiated, the previous one is cancelled, and paths that have not been opened are closed.
Returns the server addresses that are now being probed.
Trait Implementations§
Source§impl Clone for Connection
impl Clone for Connection
Source§fn clone(&self) -> Connection
fn clone(&self) -> Connection
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more