2021-07-18 18:03:29 +00:00
|
|
|
use quinn::{
|
|
|
|
SendStream,
|
|
|
|
RecvStream,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2021-07-18 17:57:54 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct Command (pub u8);
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
pub const CONNECT_P2_TO_P3: Command = Command (2);
|
|
|
|
pub const CONNECT_P4_TO_P3: Command = Command (4);
|
|
|
|
pub const CONNECT_P2_TO_P4: Command = Command (10);
|
|
|
|
pub const CONNECT_P2_TO_P4_STEP_2: Command = Command (11);
|
|
|
|
pub const CONNECT_P2_TO_P5: Command = Command (12);
|
|
|
|
pub const OKAY: Command = Command (20);
|
|
|
|
}
|
|
|
|
|
2021-07-18 18:03:29 +00:00
|
|
|
pub async fn p2_connect_to_p3 (
|
|
|
|
send: &mut SendStream,
|
|
|
|
recv: &mut RecvStream,
|
|
|
|
client_id: u8,
|
|
|
|
)
|
|
|
|
-> anyhow::Result <()>
|
|
|
|
{
|
|
|
|
let req_buf = [
|
|
|
|
Command::CONNECT_P2_TO_P3.0,
|
|
|
|
client_id,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
];
|
|
|
|
send.write_all (&req_buf).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");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|