Skip to content

Commit

Permalink
refactor: fix edition 2024, improve gm_quic::util
Browse files Browse the repository at this point in the history
  • Loading branch information
eareimu authored and huster-zhangpeng committed Jan 26, 2025
1 parent 6ef0f0c commit c6311a2
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 49 deletions.
11 changes: 6 additions & 5 deletions gm-quic/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ struct Arguments {
fn main() {
let args = Arguments::parse();
let code = {
if let Err(e) = run(args) {
eprintln!("ERROR: {e}");
1
} else {
0
match run(args) {
Err(e) => {
eprintln!("ERROR: {e}");
1
}
_ => 0,
}
};
::std::process::exit(code);
Expand Down
13 changes: 7 additions & 6 deletions gm-quic/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ struct Opt {
fn main() {
let opt = Opt::parse();
let code = {
if let Err(e) = run(opt) {
eprintln!("ERROR: {e}");
1
} else {
0
match run(opt) {
Err(e) => {
eprintln!("ERROR: {e}");
1
}
_ => 0,
}
};
::std::process::exit(code);
Expand All @@ -44,7 +45,7 @@ async fn run(options: Opt) -> Result<(), Box<dyn std::error::Error>> {
.with_supported_versions([0x00000001u32])
.without_cert_verifier()
// .keep_alive()
.with_single_cert(options.cert, options.key)
.with_single_cert(options.cert.as_path(), options.key.as_path())
.listen(options.bind)?;

while let Ok((_conn, pathway)) = server.accept().await {
Expand Down
50 changes: 30 additions & 20 deletions gm-quic/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,63 @@ use std::path::Path;

use rustls::pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer};

pub struct Certificate(Vec<CertificateDer<'static>>);

impl From<Vec<CertificateDer<'static>>> for Certificate {
fn from(cert: Vec<CertificateDer<'static>>) -> Self {
Self(cert)
}
}

pub trait ToCertificate {
fn to_certificate(self) -> Vec<CertificateDer<'static>>;
}

impl ToCertificate for Certificate {
impl ToCertificate for Vec<CertificateDer<'static>> {
fn to_certificate(self) -> Vec<CertificateDer<'static>> {
self.0
self
}
}

impl<P: AsRef<Path>> ToCertificate for P {
impl ToCertificate for &Path {
fn to_certificate(self) -> Vec<CertificateDer<'static>> {
CertificateDer::pem_file_iter(self.as_ref())
CertificateDer::pem_file_iter(self)
.expect("failed to open cert file")
.collect::<Result<Vec<_>, _>>()
.expect("failed to parse cert file")
}
}

pub struct PrivateKey(PrivateKeyDer<'static>);
impl ToCertificate for &[u8] {
fn to_certificate(self) -> Vec<CertificateDer<'static>> {
CertificateDer::pem_slice_iter(self)
.collect::<Result<Vec<_>, _>>()
.expect("failed to parse cert file")
}
}

impl From<PrivateKeyDer<'static>> for PrivateKey {
fn from(key: PrivateKeyDer<'static>) -> Self {
Self(key)
impl<const N: usize> ToCertificate for &[u8; N] {
fn to_certificate(self) -> Vec<CertificateDer<'static>> {
<&[u8]>::to_certificate(self)
}
}

pub trait ToPrivateKey {
fn to_private_key(self) -> PrivateKeyDer<'static>;
}

impl ToPrivateKey for PrivateKey {
impl ToPrivateKey for PrivateKeyDer<'static> {
fn to_private_key(self) -> PrivateKeyDer<'static> {
self
}
}

impl ToPrivateKey for &Path {
fn to_private_key(self) -> PrivateKeyDer<'static> {
PrivateKeyDer::from_pem_file(self).expect("failed to parse private key file")
}
}

impl ToPrivateKey for &[u8] {
fn to_private_key(self) -> PrivateKeyDer<'static> {
self.0
PrivateKeyDer::from_pem_slice(self).expect("failed to parse private key file")
}
}

impl<P: AsRef<Path>> ToPrivateKey for P {
impl<const N: usize> ToPrivateKey for &[u8; N] {
fn to_private_key(self) -> PrivateKeyDer<'static> {
PrivateKeyDer::from_pem_file(self.as_ref()).expect("failed to parse private key file")
<&[u8]>::to_private_key(self)
}
}
2 changes: 1 addition & 1 deletion h3-shim/examples/h3-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub async fn run(opt: Opt) -> Result<(), Box<dyn std::error::Error + Send + Sync
.without_cert_verifier()
.with_parameters(params)
.enable_sni()
.add_host("localhost", cert, key)
.add_host("localhost", cert.as_path(), key.as_path())
.with_alpns([ALPN.to_vec()])
.listen(&opt.listen[..])?;
info!("listening on {:?}", quic_server.addresses());
Expand Down
15 changes: 9 additions & 6 deletions qbase/src/util/bound_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ impl<T> futures::Stream for Receiver<T> {
if queue.capacity() == 0 {
return Poll::Ready(None);
}
if let Some(item) = queue.pop_front() {
self.inner.write_waker.wake();
Poll::Ready(Some(item))
} else {
self.inner.read_waker.register(cx.waker());
Poll::Pending
match queue.pop_front() {
Some(item) => {
self.inner.write_waker.wake();
Poll::Ready(Some(item))
}
_ => {
self.inner.read_waker.register(cx.waker());
Poll::Pending
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion qconnection/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl ComponentsReady {
}
}

fn accpet_transport_parameters(components: &Components) -> impl Future<Output = ()> + Send {
fn accpet_transport_parameters(components: &Components) -> impl Future<Output = ()> + Send + use<> {
let params = components.parameters.clone();
let streams = components.spaces.data().streams().clone();
let cid_registry = components.cid_registry.clone();
Expand Down
12 changes: 7 additions & 5 deletions qconnection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub struct Components {
impl Components {
pub fn open_bi_stream(
&self,
) -> impl Future<Output = io::Result<Option<(StreamId, (StreamReader, StreamWriter))>>> + Send
) -> impl Future<Output = io::Result<Option<(StreamId, (StreamReader, StreamWriter))>>> + Send + use<>
{
let params = self.parameters.clone();
let streams = self.spaces.data().streams().clone();
Expand All @@ -193,7 +193,7 @@ impl Components {

pub fn open_uni_stream(
&self,
) -> impl Future<Output = io::Result<Option<(StreamId, StreamWriter)>>> + Send {
) -> impl Future<Output = io::Result<Option<(StreamId, StreamWriter)>>> + Send + use<> {
let params = self.parameters.clone();
let streams = self.spaces.data().streams().clone();
let send_notify = self.send_notify.clone();
Expand All @@ -209,7 +209,7 @@ impl Components {

pub fn accept_bi_stream(
&self,
) -> impl Future<Output = io::Result<Option<(StreamId, (StreamReader, StreamWriter))>>> + Send
) -> impl Future<Output = io::Result<Option<(StreamId, (StreamReader, StreamWriter))>>> + Send + use<>
{
let params = self.parameters.clone();
let streams = self.spaces.data().streams().clone();
Expand All @@ -227,7 +227,7 @@ impl Components {

pub fn accept_uni_stream(
&self,
) -> impl Future<Output = io::Result<Option<(StreamId, StreamReader)>>> + Send {
) -> impl Future<Output = io::Result<Option<(StreamId, StreamReader)>>> + Send + use<> {
let streams = self.spaces.data().streams().clone();
async move { Ok(Some(streams.accept_uni().await?)) }
}
Expand All @@ -236,7 +236,9 @@ impl Components {
self.spaces.data().datagrams().reader()
}

pub fn unreliable_writer(&self) -> impl Future<Output = io::Result<UnreliableWriter>> + Send {
pub fn unreliable_writer(
&self,
) -> impl Future<Output = io::Result<UnreliableWriter>> + Send + use<> {
let params = self.parameters.clone();
let datagrams = self.spaces.data().datagrams().clone();
async move {
Expand Down
2 changes: 1 addition & 1 deletion qconnection/src/path/burst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Burst {
fn prepare_buffers<'b>(
&self,
buffers: &'b mut Vec<Vec<u8>>,
) -> io::Result<impl Iterator<Item = &'b mut [u8]>> {
) -> io::Result<impl Iterator<Item = &'b mut [u8]> + use<'b>> {
let max_segments = self.path.interface.max_segments()?;
let max_segment_size = self.path.interface.max_segment_size()?;

Expand Down
5 changes: 4 additions & 1 deletion qconnection/src/path/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ impl super::Path {
}
}

pub fn idle_timeout(self: &Arc<Self>, components: &Components) -> impl Future<Output = ()> {
pub fn idle_timeout(
self: &Arc<Self>,
components: &Components,
) -> impl Future<Output = ()> + use<> {
let parameters = components.parameters.clone();
let this = self.clone();
async move {
Expand Down
2 changes: 1 addition & 1 deletion qconnection/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl ArcTlsSession {

/// Start the TLS handshake, automatically upgrade the keys, and transmit tls data.
#[tracing::instrument(level = "trace", skip(components))]
pub fn keys_upgrade(components: &Components) -> impl Future<Output = ()> + Send {
pub fn keys_upgrade(components: &Components) -> impl Future<Output = ()> + Send + use<> {
let crypto_streams: [&CryptoStream; 3] = [
components.spaces.initial().crypto_stream(),
components.spaces.handshake().crypto_stream(),
Expand Down
4 changes: 2 additions & 2 deletions qrecovery/src/journal/sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ impl<T: Clone> RotateGuard<'_, T> {
}

/// Called when the packet sent is acked by peer, return the frames in that packet.
pub fn on_pkt_acked(&mut self, pn: u64) -> impl Iterator<Item = T> + '_ {
pub fn on_pkt_acked(&mut self, pn: u64) -> impl Iterator<Item = T> + '_ + use<'_, T> {
self.inner.on_pkt_acked(pn)
}

/// Called when the packet sent may lost, reutrn the frames in that packet.
pub fn may_loss_pkt(&mut self, pn: u64) -> impl Iterator<Item = T> + '_ {
pub fn may_loss_pkt(&mut self, pn: u64) -> impl Iterator<Item = T> + '_ + use<'_, T> {
self.inner.may_loss_pkt(pn)
}

Expand Down

0 comments on commit c6311a2

Please sign in to comment.