modify client to accept connections from a local TCP client

main
_ 2021-07-17 02:39:08 +00:00
parent fe2040706b
commit c4cd8cf1aa
2 changed files with 48 additions and 12 deletions

View File

@ -1,3 +1,7 @@
use tokio::{
net::TcpListener,
};
use quic_demo::prelude::*;
#[tokio::main]
@ -15,21 +19,47 @@ async fn main () -> anyhow::Result <()> {
..
} = endpoint.connect (&server_addr, "localhost")?.await?;
debug! ("Waiting for local TCP client to connect to us");
let listener = TcpListener::bind ("127.0.0.1:30381").await?;
let (tcp_socket, _) = listener.accept ().await?;
let (mut local_recv, mut local_send) = tcp_socket.into_split ();
debug! ("Connecting to end server");
let (mut send, mut recv) = connection.open_bi ().await?;
let (mut relay_send, mut relay_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;
let uplink_task = tokio::spawn (async move {
// Uplink - local client to relay server
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 mut buf = vec! [0u8; 65_536];
loop {
let bytes_read = local_recv.read (&mut buf).await?;
let buf_slice = &buf [0..bytes_read];
relay_send.write_all (buf_slice).await?;
}
let s = std::str::from_utf8 (buf_slice)?;
println! ("Received: `{}`", s);
}
debug! ("Uplink closed");
Ok::<_, anyhow::Error> (())
});
let downlink_task = tokio::spawn (async move {
// Downlink - Relay server to local client
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = relay_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
local_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
uplink_task.await??;
downlink_task.await??;
Ok (())
}

View File

@ -3,6 +3,12 @@ pub use std::{
};
pub use futures_util::StreamExt;
pub use tokio::{
io::{
AsyncReadExt,
AsyncWriteExt,
},
};
pub use tracing::{
debug,
error,