add QUIC client

main
_ 2021-07-17 01:10:30 +00:00
parent 937cc65c70
commit fe2040706b
4 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,35 @@
use quic_demo::prelude::*;
#[tokio::main]
async fn main () -> anyhow::Result <()> {
tracing_subscriber::fmt::init ();
let server_cert = tokio::fs::read ("quic_server.crt").await?;
let server_addr = "127.0.0.1:30380".parse ()?;
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&server_cert])?;
debug! ("Connecting to relay server");
let quinn::NewConnection {
connection,
..
} = endpoint.connect (&server_addr, "localhost")?.await?;
debug! ("Connecting to end server");
let (mut send, mut recv) = connection.open_bi ().await?;
let mut buf = vec! [0u8; 65_536];
let mut send_interval = tokio::time::interval (Duration::from_millis (1000));
// send_interval.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip);
loop {
send_interval.tick ().await;
send.write_all (b"Hi!\n").await?;
let bytes_read = recv.read (&mut buf).await?.ok_or_else (|| anyhow::anyhow! ("Server or relay closed connection"))?;
let buf_slice = &buf [0..bytes_read];
let s = std::str::from_utf8 (buf_slice)?;
println! ("Received: `{}`", s);
}
}

View File

@ -21,7 +21,7 @@ async fn main () -> anyhow::Result <()> {
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = recv.read (&mut buf).await? {
let s = format! ("bytes_read: {}\n", bytes_read);
let s = format! ("bytes_read: {}", bytes_read);
send.write_all (s.as_bytes ()).await?;
}

View File

@ -1,3 +1,7 @@
pub use std::{
time::Duration,
};
pub use futures_util::StreamExt;
pub use tracing::{
debug,

View File

@ -5,7 +5,10 @@ use quinn::{
Certificate, CertificateChain, ClientConfig, ClientConfigBuilder, Endpoint, Incoming,
PrivateKey, ServerConfig, ServerConfigBuilder, TransportConfig,
};
use std::{error::Error, net::SocketAddr, sync::Arc};
use std::{
net::SocketAddr,
sync::Arc,
};
/// Constructs a QUIC endpoint configured for use a client only.
///