1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! [`Connection`]s hold a connection to a peer in an
//! [`Endpoint`](crate::Endpoint).
//!
//!
//! A single [`Connection`] can have multiple streams, streams consist of a
//! [`Sender`] and [`Receiver`], which can send and receive messages on that
//! stream.
//!
//! You can use [`open_stream`](Connection::open_stream) to open a stream.

mod connecting;
mod incoming;
mod receiver;
mod receiver_stream;
mod sender;

use std::{
	fmt::{self, Debug, Formatter},
	future::Future,
	marker::PhantomData,
	net::SocketAddr,
	pin::{pin, Pin},
	task::{Context, Poll},
};

pub use connecting::Connecting;
use futures_channel::oneshot;
use futures_util::{
	stream::{self, FusedStream},
	FutureExt, StreamExt,
};
pub use incoming::Incoming;
use pin_project::pin_project;
use quinn::{crypto::rustls::HandshakeData, AcceptBi, RecvStream, SendStream, VarInt};
pub use receiver::Receiver;
use receiver_stream::ReceiverStream;
pub use sender::Sender;
use serde::{de::DeserializeOwned, Serialize};
use stream::Stream;

use super::Task;
use crate::{error, CertificateChain};

/// Represents an open connection. Receives [`Incoming`] through [`Stream`].
#[pin_project]
#[derive(Clone)]
pub struct Connection<T: DeserializeOwned + Serialize + Send + 'static> {
	/// Initiate new connections or close socket.
	connection: quinn::Connection,
	/// Receive incoming streams.
	receiver:
		flume::r#async::RecvStream<'static, Result<(SendStream, RecvStream), error::Connection>>,
	/// [`Task`] handling new incoming streams.
	task: Task<()>,
	/// Type for type negotiation for new streams.
	types: PhantomData<T>,
}

impl<T: DeserializeOwned + Serialize + Send + 'static> Debug for Connection<T> {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
		formatter
			.debug_struct("Connection")
			.field("connection", &self.connection)
			.field("receiver", &"RecvStream")
			.field("task", &self.task)
			.field("types", &self.types)
			.finish()
	}
}

impl<T: DeserializeOwned + Serialize + Send + 'static> Connection<T> {
	/// Builds a new [`Connection`] from raw [`quinn`] types.
	pub(super) fn new(connection: quinn::Connection) -> Self {
		// channels for passing down new `Incoming` `Connection`s
		let (sender, receiver) = flume::unbounded();
		let receiver = receiver.into_stream();

		// `Task` handling incoming streams
		let task = Task::new(|shutdown| {
			let connection = connection.clone();
			async move {
				IncomingStreams {
					connection: &connection,
					accept: None,
					shutdown,
					sender,
					complete: false,
				}
				.await;
			}
		});

		Self {
			connection,
			receiver,
			task,
			types: PhantomData,
		}
	}

	/// Open a stream on this [`Connection`], allowing to send data back and
	/// forth.
	///
	/// Use `S` and `R` to define which type this stream is sending and
	/// receiving and `type` to send this information to the receiver.
	///
	/// # Errors
	/// - [`error::Stream::Open`] if opening a stream failed
	/// - [`error::Stream::Sender`] if sending the type information to the peer
	///   failed, see [`error::Sender`] for more details
	pub async fn open_stream<
		S: DeserializeOwned + Serialize + Send + 'static,
		R: DeserializeOwned + Serialize + Send + 'static,
	>(
		&self,
		r#type: &T,
	) -> Result<(Sender<S>, Receiver<R>), error::Stream> {
		let (sender, receiver) = self.connection.open_bi().await?;

		let sender = Sender::new(sender);
		let receiver = Receiver::new(ReceiverStream::new(receiver));

		sender.send_any(&r#type)?;

		Ok((sender, receiver))
	}

	/// The negotiated application protocol. See
	/// [`Builder::set_protocols`](crate::Builder::set_protocols).
	#[must_use]
	pub fn protocol(&self) -> Option<Vec<u8>> {
		self.connection.handshake_data().and_then(|data| {
			data.downcast_ref::<HandshakeData>()
				.and_then(|data| data.protocol.clone())
		})
	}

	/// Get the peer's [`CertificateChain`], if available.
	#[must_use]
	pub fn peer_identity(&self) -> Option<CertificateChain> {
		self.connection.peer_identity().and_then(|cert| {
			cert.downcast_ref::<Vec<rustls::Certificate>>()
				.map(|certs| CertificateChain::from_rustls(certs))
		})
	}

	/// The peer's address. Clients may change addresses at will, e.g. when
	/// switching to a cellular internet connection.
	#[must_use]
	pub fn remote_address(&self) -> SocketAddr {
		self.connection.remote_address()
	}

	/// Prevents any new incoming streams. Already incoming streams will
	/// finish first.
	///
	/// # Errors
	/// [`error::AlreadyClosed`] if it was already closed.
	pub async fn close_incoming(&self) -> Result<(), error::AlreadyClosed> {
		self.task.close(()).await
	}

	/// Close the [`Connection`] immediately.
	///
	/// To close a [`Connection`] gracefully use [`Sender::finish`], the
	/// [`Receiver`] can't be gracefully closed from the receiving end.
	///
	/// # Errors
	/// [`error::AlreadyClosed`] if it was already closed.
	pub async fn close(&self) -> Result<(), error::AlreadyClosed> {
		self.connection.close(VarInt::from_u32(0), &[]);
		(&self.task).await
	}
}

impl<T: DeserializeOwned + Serialize + Send + 'static> Stream for Connection<T> {
	type Item = Result<Incoming<T>, error::Connection>;

	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
		if self.receiver.is_terminated() {
			Poll::Ready(None)
		} else {
			self.receiver
				.poll_next_unpin(cx)
				.map_ok(|(sender, receiver)| Incoming::new(sender, receiver))
		}
	}
}

impl<T: DeserializeOwned + Serialize + Send + 'static> FusedStream for Connection<T> {
	fn is_terminated(&self) -> bool {
		self.receiver.is_terminated()
	}
}

struct IncomingStreams<'connection> {
	connection: &'connection quinn::Connection,
	accept: Option<Pin<Box<AcceptBi<'connection>>>>,
	shutdown: oneshot::Receiver<()>,
	sender: flume::Sender<Result<(SendStream, RecvStream), error::Connection>>,
	complete: bool,
}

impl Future for IncomingStreams<'_> {
	type Output = ();

	fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
		if self.complete || self.connection.close_reason().is_some() {
			return Poll::Ready(());
		}

		match self.shutdown.poll_unpin(cx) {
			Poll::Ready(_) => {
				self.complete = true;
				self.accept = None;
				Poll::Ready(())
			}
			Poll::Pending => {
				loop {
					let mut accept = self
						.accept
						.take()
						.unwrap_or_else(|| Box::pin(self.connection.accept_bi()));
					match accept.poll_unpin(cx) {
						Poll::Ready(incoming) => {
							// if there is no receiver, it means that we dropped the last
							// `Endpoint`
							if self
								.sender
								.send(incoming.map_err(error::Connection))
								.is_err()
							{
								self.complete = true;
								return Poll::Ready(());
							}
						}
						Poll::Pending => {
							// Restore the same accept future to our state.
							self.accept = Some(accept);
							return Poll::Pending;
						}
					}
				}
			}
		}
	}
}