X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/6437aa4e3e028e05ea84af469b15ed91ba0d8cc2..2813fcb83452c7fd91d799a984613a616180a067:/m17app/src/tnc.rs diff --git a/m17app/src/tnc.rs b/m17app/src/tnc.rs index 56cb661..99bacc7 100644 --- a/m17app/src/tnc.rs +++ b/m17app/src/tnc.rs @@ -1,60 +1,34 @@ -use std::io::{self, ErrorKind, Read, Write}; - -use m17core::tnc::SoftTnc; +use std::io::{Read, Write}; +/// A TNC that supports reading and writing M17 KISS messages. /// -pub trait Tnc: Read + Write + Sized { +/// TNCs must be cloneable to support reading and writing from different threads, +/// via a working implementation of try_clone(). We do not require `Clone` directly +/// as this could not be fulfilled by `TcpStream`. +pub trait Tnc: Read + Write + Sized + Send + 'static { fn try_clone(&mut self) -> Result; fn start(&mut self) -> Result<(), TncError>; fn close(&mut self) -> Result<(), TncError>; } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Clone)] pub enum TncError { - General(String), -} - -// TODO: move the following to its own module - -pub struct Soundmodem { - tnc: SoftTnc, - config: SoundmodemConfig, -} - -pub struct SoundmodemConfig { - // sound cards, PTT, etc. -} - -impl Read for Soundmodem { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.tnc - .read_kiss(buf) - .map_err(|s| io::Error::new(ErrorKind::Other, format!("{:?}", s))) - } -} - -impl Write for Soundmodem { - fn write(&mut self, buf: &[u8]) -> std::io::Result { - self.tnc - .write_kiss(buf) - .map_err(|s| io::Error::new(ErrorKind::Other, format!("{:?}", s))) - } - - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } + // TODO: Good error cases + Unknown, } -impl Tnc for Soundmodem { +impl Tnc for std::net::TcpStream { fn try_clone(&mut self) -> Result { - unimplemented!(); + self.try_clone().map_err(|_| TncError::Unknown) } fn start(&mut self) -> Result<(), TncError> { - unimplemented!(); + // already started, hopefully we get onto reading the socket quickly + Ok(()) } fn close(&mut self) -> Result<(), TncError> { - unimplemented!(); + self.shutdown(std::net::Shutdown::Both) + .map_err(|_| TncError::Unknown) } }