♻️ refactor
parent
4820722ec2
commit
d5431b5c62
|
@ -10,7 +10,10 @@ pub use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
pub use anyhow::bail;
|
||||
pub use anyhow::{
|
||||
Context,
|
||||
bail,
|
||||
};
|
||||
pub use futures_util::StreamExt;
|
||||
pub use tokio::{
|
||||
io::{
|
||||
|
|
|
@ -24,28 +24,13 @@ pub async fn p2_connect_to_p3 (
|
|||
) -> anyhow::Result <quinn::NewConnection>
|
||||
{
|
||||
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
|
||||
|
||||
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
|
||||
let cmd_type = Command::CONNECT_P2_TO_P3.0;
|
||||
|
||||
let req_buf = [
|
||||
Command::CONNECT_P2_TO_P3.0,
|
||||
client_id,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
send.write_all (&req_buf).await?;
|
||||
send.write_all (&[cmd_type, client_id, 0, 0]).await?;
|
||||
|
||||
let mut resp_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut resp_buf).await?;
|
||||
let expected = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P2_TO_P3.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
if resp_buf != expected {
|
||||
bail! ("P2 didn't get OK response when connecting to P3");
|
||||
}
|
||||
expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
|
||||
.context ("P2 didn't get OK response when connecting to P3")?;
|
||||
|
||||
Ok (new_conn)
|
||||
}
|
||||
|
@ -111,28 +96,26 @@ pub async fn p4_connect_to_p3 (
|
|||
) -> anyhow::Result <quinn::NewConnection>
|
||||
{
|
||||
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
|
||||
|
||||
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
|
||||
let cmd_type = Command::CONNECT_P4_TO_P3.0;
|
||||
|
||||
let req_buf = [
|
||||
Command::CONNECT_P4_TO_P3.0,
|
||||
server_id,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
send.write_all (&req_buf).await?;
|
||||
send.write_all (&[cmd_type, server_id, 0, 0]).await?;
|
||||
|
||||
let mut resp_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut resp_buf).await?;
|
||||
let expected = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P4_TO_P3.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
if resp_buf != expected {
|
||||
bail! ("P4 didn't get OK response when connecting to P3");
|
||||
}
|
||||
expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
|
||||
.context ("P4 didn't get OK response when connecting to P3")?;
|
||||
|
||||
Ok (new_conn)
|
||||
}
|
||||
|
||||
async fn expect_exact_response (
|
||||
recv: &mut RecvStream, expected: [u8; 4]
|
||||
) -> anyhow::Result <()>
|
||||
{
|
||||
let mut resp_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut resp_buf).await?;
|
||||
if resp_buf != expected {
|
||||
bail! ("Didn't receive exact response");
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue