♻️ refactor

main
_ 2021-10-10 16:48:25 +00:00
parent 88fab23871
commit 6fbe35379b
2 changed files with 78 additions and 55 deletions

View File

@ -25,14 +25,37 @@ 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])?;
let client = P2Client::connect (conf)?;
client.run ().await?;
debug! ("Connecting to relay server");
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 (&endpoint, &conf.relay_addr, &conf.client_id).await?;
} = 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?;
@ -79,6 +102,7 @@ async fn main () -> anyhow::Result <()> {
task_tcp_server.await??;
Ok (())
}
}
/// A filled-out config for constructing a P2 client

View File

@ -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])?;