]> code.octet-stream.net Git - m17rt/blob - m17app/src/tnc.rs
Implement Tnc for TcpStream
[m17rt] / m17app / src / tnc.rs
1 use std::io::{Read, Write};
2
3 /// A TNC that supports reading and writing M17 KISS messages.
4 ///
5 /// TNCs must be cloneable to support reading and writing from different threads,
6 /// via a working implementation of try_clone(). We do not require `Clone` directly
7 /// as this could not be fulfilled by `TcpStream`.
8 pub trait Tnc: Read + Write + Sized + Send + 'static {
9 fn try_clone(&mut self) -> Result<Self, TncError>;
10 fn start(&mut self) -> Result<(), TncError>;
11 fn close(&mut self) -> Result<(), TncError>;
12 }
13
14 #[derive(Debug, PartialEq, Eq, Clone)]
15 pub enum TncError {
16 // TODO: Good error cases
17 Unknown,
18 }
19
20 impl Tnc for std::net::TcpStream {
21 fn try_clone(&mut self) -> Result<Self, TncError> {
22 self.try_clone().map_err(|_| TncError::Unknown)
23 }
24
25 fn start(&mut self) -> Result<(), TncError> {
26 // already started, hopefully we get onto reading the socket quickly
27 Ok(())
28 }
29
30 fn close(&mut self) -> Result<(), TncError> {
31 self.shutdown(std::net::Shutdown::Both)
32 .map_err(|_| TncError::Unknown)
33 }
34 }