Unify connection structures

This commit is contained in:
John Doty 2022-10-08 19:26:09 +00:00
parent 01ef65c787
commit 63a02a4211

View file

@ -73,13 +73,14 @@ async fn pump_write<T: AsyncWrite + Unpin>(
// ----------------------------------------------------------------------------
// Server
struct ServerConnection {
struct Connection {
connected: Option<oneshot::Sender<()>>,
data: mpsc::Sender<Bytes>,
}
#[derive(Clone)]
struct ServerConnectionTable {
connections: Arc<Mutex<HashMap<u64, ServerConnection>>>,
connections: Arc<Mutex<HashMap<u64, Connection>>>,
}
impl ServerConnectionTable {
@ -91,7 +92,13 @@ impl ServerConnectionTable {
fn add(self: &mut Self, id: u64, data: mpsc::Sender<Bytes>) {
let mut connections = self.connections.lock().unwrap();
connections.insert(id, ServerConnection { data });
connections.insert(
id,
Connection {
connected: None,
data,
},
);
}
async fn receive(self: &Self, id: u64, buf: Bytes) {
@ -230,18 +237,6 @@ async fn server_main<Reader: AsyncRead + Unpin, Writer: AsyncWrite + Unpin>(
}
}
async fn spawn_ssh(server: &str) -> Result<tokio::process::Child, Error> {
let mut cmd = process::Command::new("ssh");
cmd.arg("-T").arg(server).arg("fwd").arg("--server");
cmd.stdout(std::process::Stdio::piped());
cmd.stdin(std::process::Stdio::piped());
match cmd.spawn() {
Ok(t) => Ok(t),
Err(e) => Err(Error::IO(e)),
}
}
async fn client_sync<T: AsyncRead + Unpin>(reader: &mut T) -> Result<(), Error> {
eprintln!("> Waiting for synchronization marker...");
let mut seen = 0;
@ -255,14 +250,9 @@ async fn client_sync<T: AsyncRead + Unpin>(reader: &mut T) -> Result<(), Error>
Ok(())
}
struct ClientConnection {
connected: Option<oneshot::Sender<()>>,
data: mpsc::Sender<Bytes>,
}
struct ClientConnectionTableState {
next_id: u64,
connections: HashMap<u64, ClientConnection>,
connections: HashMap<u64, Connection>,
}
#[derive(Clone)]
@ -286,7 +276,7 @@ impl ClientConnectionTable {
tbl.next_id += 1;
tbl.connections.insert(
id,
ClientConnection {
Connection {
connected: Some(connected),
data,
},
@ -532,6 +522,18 @@ pub async fn run_server() {
}
}
async fn spawn_ssh(server: &str) -> Result<tokio::process::Child, Error> {
let mut cmd = process::Command::new("ssh");
cmd.arg("-T").arg(server).arg("fwd").arg("--server");
cmd.stdout(std::process::Stdio::piped());
cmd.stdin(std::process::Stdio::piped());
match cmd.spawn() {
Ok(t) => Ok(t),
Err(e) => Err(Error::IO(e)),
}
}
pub async fn run_client(remote: &str) {
// TODO: Drive a reconnect loop
let mut child = spawn_ssh(remote).await.expect("failed to spawn");