Skip to content

Commit

Permalink
Per-connection ServerConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek authored and Ralith committed Apr 13, 2024
1 parent ce62df4 commit 2714162
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ impl Endpoint {
mut incoming: Incoming,
now: Instant,
buf: &mut BytesMut,
server_config: Option<Arc<ServerConfig>>,
) -> Result<(ConnectionHandle, Connection), AcceptError> {
let packet_number = incoming.packet.header.number.expand(0);
let InitialHeader {
Expand All @@ -531,7 +532,8 @@ impl Endpoint {
});
}

let server_config = self.server_config.as_ref().unwrap().clone();
let server_config =
server_config.unwrap_or_else(|| self.server_config.as_ref().unwrap().clone());

if incoming
.crypto
Expand Down
2 changes: 1 addition & 1 deletion quinn-proto/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl TestEndpoint {
now: Instant,
) -> Result<ConnectionHandle, ConnectionError> {
let mut buf = BytesMut::new();
match self.endpoint.accept(incoming, now, &mut buf) {
match self.endpoint.accept(incoming, now, &mut buf, None) {
Ok((ch, conn)) => {
self.connections.insert(ch, conn);
self.accepted = Some(Ok(ch));
Expand Down
16 changes: 11 additions & 5 deletions quinn/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,19 @@ pub(crate) struct EndpointInner {
}

impl EndpointInner {
pub(crate) fn accept(&self, incoming: proto::Incoming) -> Result<Connecting, ConnectionError> {
pub(crate) fn accept(
&self,
incoming: proto::Incoming,
server_config: Option<Arc<ServerConfig>>,
) -> Result<Connecting, ConnectionError> {
let mut state = self.state.lock().unwrap();
let mut response_buffer = BytesMut::new();
match state
.inner
.accept(incoming, Instant::now(), &mut response_buffer)
{
match state.inner.accept(
incoming,
Instant::now(),
&mut response_buffer,
server_config,
) {
Ok((handle, conn)) => {
let socket = state.socket.clone();
let runtime = state.runtime.clone();
Expand Down
18 changes: 16 additions & 2 deletions quinn/src/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::{
future::{Future, IntoFuture},
net::{IpAddr, SocketAddr},
pin::Pin,
sync::Arc,
task::{Context, Poll},
};

use proto::ConnectionError;
use proto::{ConnectionError, ServerConfig};
use thiserror::Error;

use crate::{
Expand All @@ -25,7 +26,20 @@ impl Incoming {
/// Attempt to accept this incoming connection (an error may still occur)
pub fn accept(mut self) -> Result<Connecting, ConnectionError> {
let state = self.0.take().unwrap();
state.endpoint.accept(state.inner)
state.endpoint.accept(state.inner, None)
}

/// Accept this incoming connection using a custom configuration.
///
/// See [`accept()`] for more details.
///
/// [`accept()`]: Incoming::accept
pub fn accept_with(
mut self,
server_config: Arc<ServerConfig>,
) -> Result<Connecting, ConnectionError> {
let state = self.0.take().unwrap();
state.endpoint.accept(state.inner, Some(server_config))
}

/// Reject this incoming connection attempt
Expand Down

0 comments on commit 2714162

Please sign in to comment.