👕 refactor: extract configs for client and server
parent
edd7e8de54
commit
b5be7709a3
|
@ -17,12 +17,18 @@ use tokio::{
|
|||
|
||||
use crate::loops;
|
||||
|
||||
pub async fn main () -> anyhow::Result <()> {
|
||||
let udp_sock = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, crate::PORT_1)).await?;
|
||||
udp_sock.connect ((Ipv4Addr::LOCALHOST, crate::PORT_0)).await?;
|
||||
pub struct Config {
|
||||
pub udp_eph_port: u16,
|
||||
pub udp_local_server_port: u16,
|
||||
pub tcp_server_port: u16,
|
||||
}
|
||||
|
||||
pub async fn main (cfg: Config) -> anyhow::Result <()> {
|
||||
let udp_sock = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, cfg.udp_local_server_port)).await?;
|
||||
udp_sock.connect ((Ipv4Addr::LOCALHOST, cfg.udp_eph_port)).await?;
|
||||
|
||||
let tcp_sock = TcpSocket::new_v4 ()?;
|
||||
let tcp_conn = tcp_sock.connect (SocketAddr::V4 (SocketAddrV4::new (Ipv4Addr::LOCALHOST, crate::PORT_2))).await?;
|
||||
let tcp_conn = tcp_sock.connect (SocketAddr::V4 (SocketAddrV4::new (Ipv4Addr::LOCALHOST, cfg.tcp_server_port))).await?;
|
||||
let (tcp_read, tcp_write) = tcp_conn.into_split ();
|
||||
|
||||
let tx_task;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
To test manually, run this 3 commands:
|
||||
|
||||
- Terminal A: `nc -l -u -p 9503`
|
||||
- Terminal A: `nc -l -u -p 9502`
|
||||
- Terminal B: `cargo run -p udp_over_tcp`
|
||||
- Terminal C: `nc -p 9500 -u 127.0.0.1 9501`
|
||||
|
||||
|
@ -30,7 +30,7 @@ const PORT_1: u16 = 9501;
|
|||
const PORT_2: u16 = 9502;
|
||||
|
||||
// The well-known UDP port that the PTTH_QUIC relay will bind
|
||||
const PORT_3: u16 = 9503;
|
||||
const PORT_3: u16 = 9502;
|
||||
|
||||
fn main () -> anyhow::Result <()> {
|
||||
let rt = runtime::Runtime::new ()?;
|
||||
|
@ -41,11 +41,24 @@ fn main () -> anyhow::Result <()> {
|
|||
}
|
||||
|
||||
async fn async_main () -> anyhow::Result <()> {
|
||||
let server_cfg = server::Config {
|
||||
tcp_port: PORT_2,
|
||||
udp_port: PORT_3,
|
||||
};
|
||||
let server_task = spawn (server::main (server_cfg));
|
||||
|
||||
let client_cfg = client::Config {
|
||||
udp_eph_port: PORT_0,
|
||||
udp_local_server_port: PORT_1,
|
||||
tcp_server_port: PORT_2,
|
||||
};
|
||||
let client_task = spawn (client::main (client_cfg));
|
||||
|
||||
tokio::select! {
|
||||
_val = spawn (client::main ()) => {
|
||||
_val = client_task => {
|
||||
println! ("Client exited, exiting");
|
||||
},
|
||||
_val = spawn (server::main ()) => {
|
||||
_val = server_task => {
|
||||
println! ("Server exited, exiting");
|
||||
},
|
||||
}
|
||||
|
|
|
@ -17,19 +17,29 @@ use tokio::{
|
|||
|
||||
use crate::loops;
|
||||
|
||||
pub async fn main () -> anyhow::Result <()> {
|
||||
let tcp_listener = TcpListener::bind ((Ipv4Addr::UNSPECIFIED, crate::PORT_2)).await?;
|
||||
#[derive (Clone)]
|
||||
pub struct Config {
|
||||
/// The well-known TCP port that the UDP-over-TCP server will bind
|
||||
pub tcp_port: u16,
|
||||
|
||||
/// The well-known UDP port that the PTTH_QUIC relay will bind
|
||||
pub udp_port: u16,
|
||||
}
|
||||
|
||||
pub async fn main (cfg: Config) -> anyhow::Result <()> {
|
||||
let tcp_listener = TcpListener::bind ((Ipv4Addr::UNSPECIFIED, cfg.tcp_port)).await?;
|
||||
|
||||
loop {
|
||||
let (conn, _peer_addr) = tcp_listener.accept ().await?;
|
||||
|
||||
spawn (handle_connection (conn));
|
||||
let cfg = cfg.clone ();
|
||||
spawn (handle_connection (cfg, conn));
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_connection (conn: TcpStream) -> anyhow::Result <()> {
|
||||
async fn handle_connection (cfg: Config, conn: TcpStream) -> anyhow::Result <()> {
|
||||
let udp_sock = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, 0)).await?;
|
||||
udp_sock.connect ((Ipv4Addr::LOCALHOST, crate::PORT_3)).await?;
|
||||
udp_sock.connect ((Ipv4Addr::LOCALHOST, cfg.udp_port)).await?;
|
||||
|
||||
let (tcp_read, tcp_write) = conn.into_split ();
|
||||
|
||||
|
|
Loading…
Reference in New Issue