use std::collections::{HashMap, VecDeque};
use std::time::Instant;
use bytes::Bytes;
use quietquic_proto::conn::{ConnState as CoreConn, SendFin};
use quietquic_proto::outcome::{ConnectionHandle, ReadOutcome, WriteOutcome};
use quinn_proto::{StreamId, VarInt};
use tokio::sync::{mpsc, oneshot, watch};
pub use quietquic_proto::conn::ConnError;
pub use quietquic_proto::outcome::ConnectionError;
pub(crate) struct Tagged {
pub(crate) handle: ConnectionHandle,
pub(crate) cmd: Cmd,
}
#[derive(Clone)]
pub(crate) struct CmdSender {
handle: ConnectionHandle,
tx: mpsc::Sender<Tagged>,
}
impl CmdSender {
pub(crate) fn new(handle: ConnectionHandle, tx: mpsc::Sender<Tagged>) -> Self {
Self { handle, tx }
}
async fn send(&self, cmd: Cmd) -> Result<(), ConnError> {
self.tx
.send(Tagged {
handle: self.handle,
cmd,
})
.await
.map_err(|_| ConnError::Closed)
}
fn try_send(&self, cmd: Cmd) -> Result<(), mpsc::error::TrySendError<Tagged>> {
self.tx.try_send(Tagged {
handle: self.handle,
cmd,
})
}
}
pub(crate) enum Cmd {
OpenBi(oneshot::Sender<Result<StreamId, ConnError>>),
AcceptBi(oneshot::Sender<Result<StreamId, ConnError>>),
Write {
id: StreamId,
data: Vec<u8>,
reply: oneshot::Sender<Result<(), ConnError>>,
},
Finish {
id: StreamId,
reply: oneshot::Sender<Result<(), ConnError>>,
},
WaitFinished {
id: StreamId,
reply: oneshot::Sender<Result<(), ConnError>>,
},
Reset {
id: StreamId,
code: VarInt,
reply: oneshot::Sender<Result<(), ConnError>>,
},
Stop {
id: StreamId,
code: VarInt,
reply: oneshot::Sender<Result<(), ConnError>>,
},
ReleaseSend { id: StreamId },
ReadToEnd {
id: StreamId,
limit: usize,
reply: oneshot::Sender<Result<Vec<u8>, ConnError>>,
},
Read {
id: StreamId,
max: usize,
reply: oneshot::Sender<Result<Vec<u8>, ConnError>>,
},
Close { code: VarInt, reason: Vec<u8> },
}
#[derive(Clone)]
pub struct Connection {
remote: std::net::SocketAddr,
handle: ConnectionHandle,
client_id: Option<String>,
cmds: CmdSender,
closed: watch::Receiver<Option<ConnectionError>>,
}
impl Connection {
pub(crate) fn new(
handle: ConnectionHandle,
remote: std::net::SocketAddr,
client_id: Option<String>,
cmds: CmdSender,
closed: watch::Receiver<Option<ConnectionError>>,
) -> Self {
Self {
remote,
handle,
client_id,
cmds,
closed,
}
}
pub fn remote_address(&self) -> std::net::SocketAddr {
self.remote
}
pub fn handle(&self) -> ConnectionHandle {
self.handle
}
pub fn client_id(&self) -> Option<&str> {
self.client_id.as_deref()
}
pub async fn open_bi(&self) -> Result<(SendStream, RecvStream), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds.send(Cmd::OpenBi(tx)).await?;
let id = rx.await.map_err(|_| ConnError::Closed)??;
Ok(bi_stream(id, self.cmds.clone()))
}
pub async fn accept_bi(&self) -> Result<(SendStream, RecvStream), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds.send(Cmd::AcceptBi(tx)).await?;
let id = rx.await.map_err(|_| ConnError::Closed)??;
Ok(bi_stream(id, self.cmds.clone()))
}
pub async fn close(&self, code: u64, reason: &[u8]) -> Result<(), ConnError> {
let code = varint(code)?;
let _ = self
.cmds
.send(Cmd::Close {
code,
reason: reason.to_vec(),
})
.await;
Ok(())
}
pub async fn closed(&self) -> ConnectionError {
let mut closed = self.closed.clone();
loop {
if let Some(reason) = closed.borrow().clone() {
return reason;
}
if closed.changed().await.is_err() {
return ConnectionError::LocallyClosed;
}
}
}
pub fn quinn_connection(&self) -> QuinnHandle {
QuinnHandle {
handle: self.handle,
cmds: self.cmds.clone(),
}
}
}
impl std::fmt::Debug for Connection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Connection")
.field("handle", &self.handle)
.field("remote", &self.remote)
.field("client_id", &self.client_id)
.finish()
}
}
#[derive(Clone)]
pub struct QuinnHandle {
handle: ConnectionHandle,
cmds: CmdSender,
}
impl QuinnHandle {
pub fn handle(&self) -> ConnectionHandle {
self.handle
}
pub async fn open_bi(&self) -> Result<(SendStream, RecvStream), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds.send(Cmd::OpenBi(tx)).await?;
let id = rx.await.map_err(|_| ConnError::Closed)??;
Ok(bi_stream(id, self.cmds.clone()))
}
pub async fn accept_bi(&self) -> Result<(SendStream, RecvStream), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds.send(Cmd::AcceptBi(tx)).await?;
let id = rx.await.map_err(|_| ConnError::Closed)??;
Ok(bi_stream(id, self.cmds.clone()))
}
pub async fn finish(&self, id: StreamId) -> Result<(), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds.send(Cmd::Finish { id, reply: tx }).await?;
rx.await.map_err(|_| ConnError::Closed)?
}
pub async fn wait_finished(&self, id: StreamId) -> Result<(), ConnError> {
wait_finished(&self.cmds, id).await
}
pub async fn finish_and_wait(&self, id: StreamId) -> Result<(), ConnError> {
self.finish(id).await?;
self.wait_finished(id).await
}
pub async fn forget_send(&self, id: StreamId) {
let _ = self.cmds.send(Cmd::ReleaseSend { id }).await;
}
}
pub struct RecvStream {
id: StreamId,
cmds: CmdSender,
}
impl RecvStream {
pub fn id(&self) -> StreamId {
self.id
}
pub async fn read(&mut self, max: usize) -> Result<Vec<u8>, ConnError> {
read_chunk(&self.cmds, self.id, max).await
}
pub async fn read_to_end(&mut self, limit: usize) -> Result<Vec<u8>, ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds
.send(Cmd::ReadToEnd {
id: self.id,
limit,
reply: tx,
})
.await?;
rx.await.map_err(|_| ConnError::Closed)?
}
pub async fn stop(&mut self, code: u64) -> Result<(), ConnError> {
let code = varint(code)?;
let (tx, rx) = oneshot::channel();
self.cmds
.send(Cmd::Stop {
id: self.id,
code,
reply: tx,
})
.await?;
rx.await.map_err(|_| ConnError::Closed)?
}
}
pub struct SendStream {
id: StreamId,
cmds: CmdSender,
}
impl SendStream {
pub fn id(&self) -> StreamId {
self.id
}
pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds
.send(Cmd::Write {
id: self.id,
data: buf.to_vec(),
reply: tx,
})
.await?;
rx.await.map_err(|_| ConnError::Closed)?
}
pub async fn finish(&mut self) -> Result<(), ConnError> {
let (tx, rx) = oneshot::channel();
self.cmds
.send(Cmd::Finish {
id: self.id,
reply: tx,
})
.await?;
rx.await.map_err(|_| ConnError::Closed)?
}
pub async fn wait_finished(&mut self) -> Result<(), ConnError> {
wait_finished(&self.cmds, self.id).await
}
pub async fn finish_and_wait(&mut self) -> Result<(), ConnError> {
self.finish().await?;
self.wait_finished().await
}
pub async fn reset(&mut self, code: u64) -> Result<(), ConnError> {
let code = varint(code)?;
let (tx, rx) = oneshot::channel();
self.cmds
.send(Cmd::Reset {
id: self.id,
code,
reply: tx,
})
.await?;
rx.await.map_err(|_| ConnError::Closed)?
}
}
impl Drop for SendStream {
fn drop(&mut self) {
let _ = self.cmds.try_send(Cmd::ReleaseSend { id: self.id });
}
}
async fn read_chunk(cmds: &CmdSender, id: StreamId, max: usize) -> Result<Vec<u8>, ConnError> {
if max == 0 {
return Ok(Vec::new());
}
let (tx, rx) = oneshot::channel();
cmds.send(Cmd::Read { id, max, reply: tx }).await?;
rx.await.map_err(|_| ConnError::Closed)?
}
async fn wait_finished(cmds: &CmdSender, id: StreamId) -> Result<(), ConnError> {
let (tx, rx) = oneshot::channel();
cmds.send(Cmd::WaitFinished { id, reply: tx }).await?;
rx.await.map_err(|_| ConnError::Closed)?
}
fn bi_stream(id: StreamId, cmds: CmdSender) -> (SendStream, RecvStream) {
(
SendStream {
id,
cmds: cmds.clone(),
},
RecvStream { id, cmds },
)
}
fn varint(code: u64) -> Result<VarInt, ConnError> {
VarInt::from_u64(code).map_err(|_| ConnError::InvalidErrorCode { code })
}
const READ_CHUNK: usize = 16 * 1024;
struct PendingWrite {
data: Vec<u8>,
offset: usize,
reply: oneshot::Sender<Result<(), ConnError>>,
}
struct PendingRead {
buf: Vec<u8>,
end_limit: Option<usize>,
max: Option<usize>,
reply: oneshot::Sender<Result<Vec<u8>, ConnError>>,
}
pub(crate) struct Parked {
pending_accepts: VecDeque<oneshot::Sender<Result<StreamId, ConnError>>>,
blocked_writes: HashMap<StreamId, PendingWrite>,
pending_reads: HashMap<StreamId, PendingRead>,
fin_waiters: HashMap<StreamId, Vec<oneshot::Sender<Result<(), ConnError>>>>,
closed: watch::Sender<Option<ConnectionError>>,
}
impl Parked {
pub(crate) fn new(closed: watch::Sender<Option<ConnectionError>>) -> Self {
Self {
pending_accepts: VecDeque::new(),
blocked_writes: HashMap::new(),
pending_reads: HashMap::new(),
fin_waiters: HashMap::new(),
closed,
}
}
pub(crate) fn subscribe_closed(&self) -> watch::Receiver<Option<ConnectionError>> {
self.closed.subscribe()
}
pub(crate) fn mark_closed(&self, reason: ConnectionError) {
let _ = self.closed.send(Some(reason));
}
pub(crate) fn apply_cmd(&mut self, core: &mut CoreConn, cmd: Cmd, now: Instant) {
match cmd {
Cmd::OpenBi(reply) => {
let _ = reply.send(core.open_bi());
}
Cmd::AcceptBi(reply) => match core.accept_bi() {
Ok(Some(id)) => {
let _ = reply.send(Ok(id));
}
Ok(None) => self.pending_accepts.push_back(reply),
Err(e) => {
let _ = reply.send(Err(e));
}
},
Cmd::Write { id, data, reply } => {
let mut pending = PendingWrite {
data,
offset: 0,
reply,
};
if !Self::pump_write(core, id, &mut pending) {
self.blocked_writes.insert(id, pending);
}
}
Cmd::Finish { id, reply } => {
let _ = reply.send(core.stream_finish(id));
}
Cmd::WaitFinished { id, reply } => match core.send_fin(id) {
Some(SendFin::Acked) => {
let _ = reply.send(Ok(()));
}
Some(SendFin::Stopped(code)) => {
let _ = reply.send(Err(ConnError::Stopped { code }));
}
Some(SendFin::Queued) => self.fin_waiters.entry(id).or_default().push(reply),
Some(_) => {
let _ = reply.send(Err(ConnError::ClosedStream));
}
None => {
let _ = reply.send(Err(ConnError::ClosedStream));
}
},
Cmd::Reset { id, code, reply } => {
let result = core.stream_reset(id, code.into_inner());
if result.is_ok() {
self.fail_fin_waiters(id, ConnError::ClosedStream);
self.fail_blocked_write(id, ConnError::ClosedStream);
}
let _ = reply.send(result);
}
Cmd::Stop { id, code, reply } => {
let result = core.stream_stop(id, code.into_inner());
if result.is_ok() {
self.fail_pending_read(id, ConnError::ClosedStream);
}
let _ = reply.send(result);
}
Cmd::ReleaseSend { id } => {
core.forget_send(id);
}
Cmd::ReadToEnd { id, limit, reply } => {
let mut pending = PendingRead {
buf: Vec::new(),
end_limit: Some(limit),
max: None,
reply,
};
if !Self::pump_read(core, id, &mut pending) {
self.pending_reads.insert(id, pending);
}
}
Cmd::Read { id, max, reply } => {
let mut pending = PendingRead {
buf: Vec::new(),
end_limit: None,
max: Some(max.max(1)),
reply,
};
if !Self::pump_read(core, id, &mut pending) {
self.pending_reads.insert(id, pending);
}
}
Cmd::Close { code, reason } => {
core.conn_mut().close(now, code, Bytes::from(reason));
}
}
}
pub(crate) fn on_stream_opened(&mut self, core: &mut CoreConn) {
while !self.pending_accepts.is_empty() {
match core.accept_bi() {
Ok(Some(id)) => {
if let Some(reply) = self.pending_accepts.pop_front() {
let _ = reply.send(Ok(id));
}
}
Ok(None) => return,
Err(e) => {
if let Some(reply) = self.pending_accepts.pop_front() {
let _ = reply.send(Err(e));
}
return;
}
}
}
}
pub(crate) fn on_readable(&mut self, core: &mut CoreConn, id: StreamId) {
let Some(mut pending) = self.pending_reads.remove(&id) else {
return;
};
if !Self::pump_read(core, id, &mut pending) {
self.pending_reads.insert(id, pending);
}
}
pub(crate) fn on_writable(&mut self, core: &mut CoreConn, id: StreamId) {
let Some(mut pending) = self.blocked_writes.remove(&id) else {
return;
};
if !Self::pump_write(core, id, &mut pending) {
self.blocked_writes.insert(id, pending);
}
}
pub(crate) fn on_fin_acked(&mut self, id: StreamId) {
if let Some(waiters) = self.fin_waiters.remove(&id) {
for reply in waiters {
let _ = reply.send(Ok(()));
}
}
}
pub(crate) fn on_stopped(&mut self, core: &mut CoreConn, id: StreamId, code: u64) {
self.fail_fin_waiters(id, ConnError::Stopped { code });
self.on_writable(core, id);
}
pub(crate) fn fail_all(&mut self) {
for reply in self.pending_accepts.drain(..) {
let _ = reply.send(Err(ConnError::Closed));
}
for (_, pending) in self.blocked_writes.drain() {
let _ = pending.reply.send(Err(ConnError::Closed));
}
for (_, pending) in self.pending_reads.drain() {
let _ = pending.reply.send(Err(ConnError::Closed));
}
for (_, waiters) in self.fin_waiters.drain() {
for reply in waiters {
let _ = reply.send(Err(ConnError::Closed));
}
}
}
fn fail_fin_waiters(&mut self, id: StreamId, err: ConnError) {
if let Some(waiters) = self.fin_waiters.remove(&id) {
for reply in waiters {
let _ = reply.send(Err(err.clone()));
}
}
}
fn fail_blocked_write(&mut self, id: StreamId, err: ConnError) {
if let Some(pending) = self.blocked_writes.remove(&id) {
let _ = pending.reply.send(Err(err));
}
}
fn fail_pending_read(&mut self, id: StreamId, err: ConnError) {
if let Some(pending) = self.pending_reads.remove(&id) {
let _ = pending.reply.send(Err(err));
}
}
fn pump_write(core: &mut CoreConn, id: StreamId, pending: &mut PendingWrite) -> bool {
loop {
if pending.offset >= pending.data.len() {
let reply = replace_reply_ok(&mut pending.reply);
let _ = reply.send(Ok(()));
return true;
}
match core.stream_write(id, &pending.data[pending.offset..]) {
Ok(WriteOutcome::Wrote(n)) => pending.offset += n,
Ok(WriteOutcome::Blocked) => return false,
Err(e) => {
let reply = replace_reply_ok(&mut pending.reply);
let _ = reply.send(Err(e));
return true;
}
}
}
}
fn pump_read(core: &mut CoreConn, id: StreamId, pending: &mut PendingRead) -> bool {
loop {
let filled = pending.buf.len();
let request = pending
.max
.map(|max| max.saturating_sub(filled).min(READ_CHUNK))
.unwrap_or_else(|| {
pending
.end_limit
.map(|limit| {
limit
.saturating_add(1)
.saturating_sub(filled)
.min(READ_CHUNK)
})
.unwrap_or(READ_CHUNK)
})
.max(1);
pending.buf.resize(filled + request, 0);
let outcome = core.stream_read(id, &mut pending.buf[filled..]);
match outcome {
Ok(ReadOutcome::Read(n)) => {
pending.buf.truncate(filled + n);
if pending
.end_limit
.is_some_and(|limit| pending.buf.len() > limit)
{
let limit = pending.end_limit.expect("checked above");
let reply = replace_reply_read(&mut pending.reply);
let _ = reply.send(Err(ConnError::ReadLimitExceeded { limit }));
return true;
}
if pending.max.is_some() {
let buf = std::mem::take(&mut pending.buf);
let reply = replace_reply_read(&mut pending.reply);
let _ = reply.send(Ok(buf));
return true;
}
}
Ok(ReadOutcome::Blocked) => {
pending.buf.truncate(filled);
return false;
}
Ok(ReadOutcome::Finished) => {
pending.buf.truncate(filled);
if pending.end_limit.is_some() {
core.forget_recv(id);
}
let buf = std::mem::take(&mut pending.buf);
let reply = replace_reply_read(&mut pending.reply);
let _ = reply.send(Ok(buf));
return true;
}
Err(e) => {
pending.buf.truncate(filled);
let reply = replace_reply_read(&mut pending.reply);
let _ = reply.send(Err(e));
return true;
}
}
}
}
}
fn replace_reply_ok(
slot: &mut oneshot::Sender<Result<(), ConnError>>,
) -> oneshot::Sender<Result<(), ConnError>> {
let (dead, _) = oneshot::channel();
std::mem::replace(slot, dead)
}
fn replace_reply_read(
slot: &mut oneshot::Sender<Result<Vec<u8>, ConnError>>,
) -> oneshot::Sender<Result<Vec<u8>, ConnError>> {
let (dead, _) = oneshot::channel();
std::mem::replace(slot, dead)
}