♻️ refactor
parent
88fab23871
commit
6fbe35379b
|
@ -25,62 +25,86 @@ async fn main () -> anyhow::Result <()> {
|
|||
let opt = Opt::from_args ();
|
||||
let conf = opt.into_config ().await?;
|
||||
|
||||
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;
|
||||
|
||||
debug! ("Connecting to relay server");
|
||||
|
||||
let quinn::NewConnection {
|
||||
connection,
|
||||
..
|
||||
} = protocol::p2_connect_to_p3 (&endpoint, &conf.relay_addr, &conf.client_id).await?;
|
||||
|
||||
let listener = TcpListener::bind (("127.0.0.1", conf.client_tcp_port)).await?;
|
||||
|
||||
debug! ("Accepting local TCP connections from P1 at {}", conf.client_tcp_port);
|
||||
|
||||
// End of per-port stuff
|
||||
// Beginning of per-connection stuff
|
||||
|
||||
let task_tcp_server = tokio::spawn (async move {
|
||||
let running = true;
|
||||
while running {
|
||||
let (tcp_socket, _) = listener.accept ().await?;
|
||||
let connection = connection.clone ();
|
||||
let server_id = conf.server_id.clone ();
|
||||
let server_tcp_port = conf.server_tcp_port;
|
||||
|
||||
tokio::spawn (async move {
|
||||
let (local_recv, local_send) = tcp_socket.into_split ();
|
||||
|
||||
debug! ("Starting PTTH connection");
|
||||
|
||||
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id, server_tcp_port).await?;
|
||||
|
||||
trace! ("Relaying bytes...");
|
||||
|
||||
let ptth_conn = quic_demo::connection::NewConnection {
|
||||
local_send,
|
||||
local_recv,
|
||||
relay_send,
|
||||
relay_recv,
|
||||
}.build ();
|
||||
|
||||
ptth_conn.wait_for_close ().await?;
|
||||
|
||||
debug! ("Ended PTTH connection");
|
||||
|
||||
Ok::<_, anyhow::Error> (())
|
||||
});
|
||||
}
|
||||
|
||||
Ok::<_, anyhow::Error> (())
|
||||
});
|
||||
|
||||
task_tcp_server.await??;
|
||||
let client = P2Client::connect (conf)?;
|
||||
client.run ().await?;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
pub struct P2Client {
|
||||
endpoint: quinn::Endpoint,
|
||||
conf: Arc <Config>,
|
||||
}
|
||||
|
||||
impl P2Client {
|
||||
pub fn connect (conf: Config) -> anyhow::Result <Self> {
|
||||
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;
|
||||
let conf = Arc::new (conf);
|
||||
|
||||
Ok (Self {
|
||||
endpoint,
|
||||
conf,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run (&self) -> anyhow::Result <()> {
|
||||
debug! ("P2 client connecting to P3 relay server");
|
||||
|
||||
let conf = Arc::clone (&self.conf);
|
||||
|
||||
let quinn::NewConnection {
|
||||
connection,
|
||||
..
|
||||
} = protocol::p2_connect_to_p3 (&self.endpoint, &conf.relay_addr, &conf.client_id).await?;
|
||||
|
||||
let listener = TcpListener::bind (("127.0.0.1", conf.client_tcp_port)).await?;
|
||||
|
||||
debug! ("Accepting local TCP connections from P1 at {}", conf.client_tcp_port);
|
||||
|
||||
// End of per-port stuff
|
||||
// Beginning of per-connection stuff
|
||||
|
||||
let task_tcp_server = tokio::spawn (async move {
|
||||
let running = true;
|
||||
while running {
|
||||
let (tcp_socket, _) = listener.accept ().await?;
|
||||
let connection = connection.clone ();
|
||||
let server_id = conf.server_id.clone ();
|
||||
let server_tcp_port = conf.server_tcp_port;
|
||||
|
||||
tokio::spawn (async move {
|
||||
let (local_recv, local_send) = tcp_socket.into_split ();
|
||||
|
||||
debug! ("Starting PTTH connection");
|
||||
|
||||
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id, server_tcp_port).await?;
|
||||
|
||||
trace! ("Relaying bytes...");
|
||||
|
||||
let ptth_conn = quic_demo::connection::NewConnection {
|
||||
local_send,
|
||||
local_recv,
|
||||
relay_send,
|
||||
relay_recv,
|
||||
}.build ();
|
||||
|
||||
ptth_conn.wait_for_close ().await?;
|
||||
|
||||
debug! ("Ended PTTH connection");
|
||||
|
||||
Ok::<_, anyhow::Error> (())
|
||||
});
|
||||
}
|
||||
|
||||
Ok::<_, anyhow::Error> (())
|
||||
});
|
||||
|
||||
task_tcp_server.await??;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
}
|
||||
|
||||
/// A filled-out config for constructing a P2 client
|
||||
#[derive (Clone)]
|
||||
pub struct Config {
|
||||
|
|
|
@ -25,7 +25,7 @@ pub async fn main (args: &[OsString], shutdown_rx: Option <watch::Receiver <bool
|
|||
let opt = Opt::from_iter (args);
|
||||
let conf = opt.into_config ().await?;
|
||||
|
||||
let end_server = Arc::new (P4EndServer::connect (conf).await?);
|
||||
let end_server = Arc::new (P4EndServer::connect (conf)?);
|
||||
|
||||
let run_task = {
|
||||
let end_server = Arc::clone (&end_server);
|
||||
|
@ -86,7 +86,6 @@ impl Opt {
|
|||
}
|
||||
}
|
||||
|
||||
/// An end server that is connected to its relay server
|
||||
pub struct P4EndServer {
|
||||
endpoint: quinn::Endpoint,
|
||||
conf: Arc <Config>,
|
||||
|
@ -95,7 +94,7 @@ pub struct P4EndServer {
|
|||
}
|
||||
|
||||
impl P4EndServer {
|
||||
pub async fn connect (conf: Config) -> anyhow::Result <Self> {
|
||||
pub fn connect (conf: Config) -> anyhow::Result <Self> {
|
||||
trace! ("P4 end server making its QUIC endpoint");
|
||||
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue