From 9190a5a51b3beefb90b5c3fc9dab05aac1936271 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Tue, 12 Dec 2023 23:11:02 +0100 Subject: [PATCH] safely close remote link in deployer --- sw/deployer/src/sc64/link.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/sw/deployer/src/sc64/link.rs b/sw/deployer/src/sc64/link.rs index 0f489be..bccccfb 100644 --- a/sw/deployer/src/sc64/link.rs +++ b/sw/deployer/src/sc64/link.rs @@ -193,6 +193,7 @@ trait Backend { data_type: DataType, packets: &mut VecDeque, ) -> Result, Error>; + fn close(&self) {} } struct SerialBackend { @@ -221,6 +222,7 @@ fn new_serial_backend(port: &str) -> Result { } struct TcpBackend { + stream: TcpStream, reader: BufReader, writer: BufWriter, } @@ -332,6 +334,10 @@ impl Backend for TcpBackend { Ok(None) } + + fn close(&self) { + self.stream.shutdown(std::net::Shutdown::Both).ok(); + } } fn new_tcp_backend(address: &str) -> Result { @@ -349,7 +355,11 @@ fn new_tcp_backend(address: &str) -> Result { }; let reader = BufReader::new(stream.try_clone()?); let writer = BufWriter::new(stream.try_clone()?); - Ok(TcpBackend { reader, writer }) + Ok(TcpBackend { + stream, + reader, + writer, + }) } pub struct Link { @@ -410,6 +420,12 @@ impl Link { } } +impl Drop for Link { + fn drop(&mut self) { + self.backend.close(); + } +} + pub fn new_local(port: &str) -> Result { Ok(Link { backend: Box::new(new_serial_backend(port)?),