]> code.octet-stream.net Git - m17rt/blobdiff - m17app/src/tnc.rs
Error handler for soundmodem components
[m17rt] / m17app / src / tnc.rs
index 99bacc721aa9a5e99dd96fa31989472f32c0b6c8..ef30e38f0d325bd360cc2ddd301226e08f53b65e 100644 (file)
@@ -6,9 +6,21 @@ use std::io::{Read, Write};
 /// 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 {
+    /// Return a copy of this TNC.
+    ///
+    /// `M17App` will use this to create a second instance of the supplied TNC then use
+    /// one of them for reading and one of them for writing, concurrently across two threads.
+    ///
+    /// Implementations do not need to worry about trying to make two simultaneous reads or
+    /// two simultaneous writes do something sensible. `M17App` will not do this and it would
+    /// probably produce garbled KISS messages anyway.
     fn try_clone(&mut self) -> Result<Self, TncError>;
-    fn start(&mut self) -> Result<(), TncError>;
-    fn close(&mut self) -> Result<(), TncError>;
+
+    /// Start I/O.
+    fn start(&mut self);
+
+    /// Shut down I/O - it is assumed we cannot restart.
+    fn close(&mut self);
 }
 
 #[derive(Debug, PartialEq, Eq, Clone)]
@@ -19,16 +31,14 @@ pub enum TncError {
 
 impl Tnc for std::net::TcpStream {
     fn try_clone(&mut self) -> Result<Self, TncError> {
-        self.try_clone().map_err(|_| TncError::Unknown)
+        std::net::TcpStream::try_clone(self).map_err(|_| TncError::Unknown)
     }
 
-    fn start(&mut self) -> Result<(), TncError> {
+    fn start(&mut self) {
         // already started, hopefully we get onto reading the socket quickly
-        Ok(())
     }
 
-    fn close(&mut self) -> Result<(), TncError> {
-        self.shutdown(std::net::Shutdown::Both)
-            .map_err(|_| TncError::Unknown)
+    fn close(&mut self) {
+        let _ = self.shutdown(std::net::Shutdown::Both);
     }
 }