♻️ refactor: start moving protocol chunks out of exe modules

main
_ 2021-07-18 18:03:29 +00:00
parent b1af608ab0
commit d265aa3cd9
2 changed files with 37 additions and 17 deletions

View File

@ -39,22 +39,7 @@ async fn main () -> anyhow::Result <()> {
let client_id = opt.client_id.unwrap_or (42);
let server_id = opt.server_id.unwrap_or (43);
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?;
assert_eq! (resp_buf, [
Command::OKAY.0,
Command::CONNECT_P2_TO_P3.0,
0,
0,
]);
protocol::p2_connect_to_p3 (&mut send, &mut recv, client_id).await?;
let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?;

View File

@ -1,3 +1,10 @@
use quinn::{
SendStream,
RecvStream,
};
use crate::prelude::*;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Command (pub u8);
@ -10,4 +17,32 @@ impl Command {
pub const OKAY: Command = Command (20);
}
// pub fn p2_connect_to_p3 (
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 (())
}