♻️ refactor: extracting protocol functions for things that handle requests
parent
69c4afe456
commit
2d57bb9618
|
@ -39,20 +39,32 @@ async fn main () -> anyhow::Result <()> {
|
|||
loop {
|
||||
let (relay_send, relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
|
||||
|
||||
tokio::spawn (handle_p2_connection (relay_send, relay_recv, local_tcp_port));
|
||||
tokio::spawn (handle_bi_stream (relay_send, relay_recv, local_tcp_port));
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_p2_connection (
|
||||
async fn handle_bi_stream (
|
||||
relay_send: quinn::SendStream,
|
||||
mut relay_recv: quinn::RecvStream,
|
||||
local_tcp_port: u16,
|
||||
) -> anyhow::Result <()>
|
||||
{
|
||||
match protocol::p4_accept_p3_stream (&mut relay_recv).await? {
|
||||
protocol::P3ToP4Stream::NewPtthConnection {
|
||||
client_id,
|
||||
..
|
||||
} => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port).await?,
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
async fn handle_new_ptth_connection (
|
||||
mut relay_send: quinn::SendStream,
|
||||
mut relay_recv: quinn::RecvStream,
|
||||
local_tcp_port: u16,
|
||||
) -> anyhow::Result <()>
|
||||
{
|
||||
let mut req_buf = [0, 0, 0, 0];
|
||||
relay_recv.read_exact (&mut req_buf).await?;
|
||||
assert_eq! (req_buf [0], Command::CONNECT_P2_TO_P4_STEP_2.0);
|
||||
|
||||
// TODO: Authorize P2 to connect to us
|
||||
|
||||
let resp_buf = [
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use anyhow::Result;
|
||||
use quinn::{
|
||||
SendStream,
|
||||
RecvStream,
|
||||
|
@ -17,11 +18,34 @@ impl Command {
|
|||
pub const OKAY: Command = Command (20);
|
||||
}
|
||||
|
||||
pub enum P3ToP4Stream {
|
||||
NewPtthConnection {
|
||||
client_id: u8,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn p4_accept_p3_stream (
|
||||
recv: &mut RecvStream,
|
||||
) -> Result <P3ToP4Stream>
|
||||
{
|
||||
let mut req_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut req_buf).await?;
|
||||
|
||||
let cmd_type = req_buf [0];
|
||||
|
||||
Ok (match Command (cmd_type) {
|
||||
Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection {
|
||||
client_id: req_buf [1],
|
||||
},
|
||||
_ => bail! ("Invalid command type while P2 was accepting a new bi stream from P3"),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn p2_connect_to_p3 (
|
||||
endpoint: &quinn::Endpoint,
|
||||
relay_addr: &std::net::SocketAddr,
|
||||
client_id: u8,
|
||||
) -> anyhow::Result <quinn::NewConnection>
|
||||
) -> Result <quinn::NewConnection>
|
||||
{
|
||||
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
|
||||
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
|
||||
|
@ -38,7 +62,7 @@ pub async fn p2_connect_to_p3 (
|
|||
pub async fn p2_connect_to_p5 (
|
||||
connection: &quinn::Connection,
|
||||
server_id: u8,
|
||||
) -> anyhow::Result <(SendStream, RecvStream)>
|
||||
) -> Result <(SendStream, RecvStream)>
|
||||
{
|
||||
let (mut send, mut recv) = connection.open_bi ().await?;
|
||||
|
||||
|
@ -67,7 +91,7 @@ pub async fn p4_connect_to_p3 (
|
|||
endpoint: &quinn::Endpoint,
|
||||
relay_addr: &std::net::SocketAddr,
|
||||
server_id: u8,
|
||||
) -> anyhow::Result <quinn::NewConnection>
|
||||
) -> Result <quinn::NewConnection>
|
||||
{
|
||||
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
|
||||
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
|
||||
|
@ -83,7 +107,7 @@ pub async fn p4_connect_to_p3 (
|
|||
|
||||
async fn expect_exact_response (
|
||||
recv: &mut RecvStream, expected: [u8; 4]
|
||||
) -> anyhow::Result <()>
|
||||
) -> Result <()>
|
||||
{
|
||||
let mut resp_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut resp_buf).await?;
|
||||
|
|
Loading…
Reference in New Issue