Unify connection structures
This commit is contained in:
parent
01ef65c787
commit
63a02a4211
1 changed files with 24 additions and 22 deletions
46
src/lib.rs
46
src/lib.rs
|
|
@ -73,13 +73,14 @@ async fn pump_write<T: AsyncWrite + Unpin>(
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Server
|
// Server
|
||||||
|
|
||||||
struct ServerConnection {
|
struct Connection {
|
||||||
|
connected: Option<oneshot::Sender<()>>,
|
||||||
data: mpsc::Sender<Bytes>,
|
data: mpsc::Sender<Bytes>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct ServerConnectionTable {
|
struct ServerConnectionTable {
|
||||||
connections: Arc<Mutex<HashMap<u64, ServerConnection>>>,
|
connections: Arc<Mutex<HashMap<u64, Connection>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerConnectionTable {
|
impl ServerConnectionTable {
|
||||||
|
|
@ -91,7 +92,13 @@ impl ServerConnectionTable {
|
||||||
|
|
||||||
fn add(self: &mut Self, id: u64, data: mpsc::Sender<Bytes>) {
|
fn add(self: &mut Self, id: u64, data: mpsc::Sender<Bytes>) {
|
||||||
let mut connections = self.connections.lock().unwrap();
|
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) {
|
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> {
|
async fn client_sync<T: AsyncRead + Unpin>(reader: &mut T) -> Result<(), Error> {
|
||||||
eprintln!("> Waiting for synchronization marker...");
|
eprintln!("> Waiting for synchronization marker...");
|
||||||
let mut seen = 0;
|
let mut seen = 0;
|
||||||
|
|
@ -255,14 +250,9 @@ async fn client_sync<T: AsyncRead + Unpin>(reader: &mut T) -> Result<(), Error>
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClientConnection {
|
|
||||||
connected: Option<oneshot::Sender<()>>,
|
|
||||||
data: mpsc::Sender<Bytes>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ClientConnectionTableState {
|
struct ClientConnectionTableState {
|
||||||
next_id: u64,
|
next_id: u64,
|
||||||
connections: HashMap<u64, ClientConnection>,
|
connections: HashMap<u64, Connection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
@ -286,7 +276,7 @@ impl ClientConnectionTable {
|
||||||
tbl.next_id += 1;
|
tbl.next_id += 1;
|
||||||
tbl.connections.insert(
|
tbl.connections.insert(
|
||||||
id,
|
id,
|
||||||
ClientConnection {
|
Connection {
|
||||||
connected: Some(connected),
|
connected: Some(connected),
|
||||||
data,
|
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) {
|
pub async fn run_client(remote: &str) {
|
||||||
// TODO: Drive a reconnect loop
|
// TODO: Drive a reconnect loop
|
||||||
let mut child = spawn_ssh(remote).await.expect("failed to spawn");
|
let mut child = spawn_ssh(remote).await.expect("failed to spawn");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue