From af4f1d85ece3c95b0e93e581a702faf250c0ecfd Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sun, 1 Oct 2023 22:20:30 +0200 Subject: [PATCH] some fixes --- src/client.rs | 4 ++-- src/server_connection.rs | 14 ++++++++++++-- src/socket_adapter.rs | 3 +-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 3f1a54e..f8672e5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,7 +1,7 @@ use std::{ collections::HashMap, io::{Read, Write}, - net::{Shutdown, TcpStream}, + net::TcpStream, thread, time::{Duration, SystemTime}, vec, @@ -65,7 +65,7 @@ fn connect(params: &ClientParams) -> Connection { ); } } - serial.set_timeout(Duration::from_millis(10000)).unwrap(); + serial.set_timeout(Duration::from_millis(600000)).unwrap(); return Connection::new_serial(serial); } Connection::new_tcp(TcpStream::connect((params.server_ip, params.server_port)).unwrap()) diff --git a/src/server_connection.rs b/src/server_connection.rs index 51f2dfd..559cdd2 100644 --- a/src/server_connection.rs +++ b/src/server_connection.rs @@ -16,6 +16,7 @@ pub struct Connection { set_nonblocking_thunk: fn(NonNull<()>, bool) -> io::Result<()>, close_thunk: fn(NonNull<()>) -> io::Result<()>, is_nb: bool, + is_serial: bool, } impl Write for Connection { @@ -23,7 +24,7 @@ impl Write for Connection { self.as_write().write_vectored(bufs) } - fn write_all(&mut self, mut buf: &[u8]) -> io::Result<()> { + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { self.as_write().write_all(buf) } @@ -103,6 +104,7 @@ impl Connection { data.cast::().as_ref().shutdown(Shutdown::Both) }, is_nb: false, + is_serial: false, } } pub fn new_serial(serial: T) -> Self { @@ -113,7 +115,7 @@ impl Connection { set_nonblocking_thunk: |data, nb| unsafe { data.cast::() .as_mut() - .set_timeout(Duration::from_millis(if nb { 1 } else { 10000 })) + .set_timeout(Duration::from_millis(if nb { 0 } else { 600000 })) .map_err(|_| { io::Error::new(io::ErrorKind::ConnectionAborted, "serial port went down") }) @@ -121,6 +123,7 @@ impl Connection { // no need to close this. close_thunk: |_data| Ok(()), is_nb: false, + is_serial: true, } } fn as_read(&mut self) -> &mut (dyn Read) { @@ -129,6 +132,9 @@ impl Connection { fn as_write(&mut self) -> &mut (dyn Write) { &mut self.readwrite } + pub fn is_nonblocking(&self) -> bool { + self.is_nb + } pub fn set_nonblocking(&mut self, nonblocking: bool) -> io::Result<()> { self.is_nb = nonblocking; (self.set_nonblocking_thunk)(self.data, nonblocking) @@ -136,4 +142,8 @@ impl Connection { pub fn close(&self) -> io::Result<()> { (self.close_thunk)(self.data) } + + pub fn is_serial(&self) -> bool { + self.is_serial + } } diff --git a/src/socket_adapter.rs b/src/socket_adapter.rs index 665e446..5534760 100644 --- a/src/socket_adapter.rs +++ b/src/socket_adapter.rs @@ -1,7 +1,6 @@ use std::{ io::{Error, Read}, io::{ErrorKind, Write}, - net::TcpStream, time::SystemTime, }; @@ -101,7 +100,7 @@ impl SocketAdapter { return Ok(()); } match { - self.internal.set_nonblocking(true)?; + self.internal.set_nonblocking(!self.internal.is_serial())?; let r = self .internal .write(&self.write[self.written..self.written + self.to_write]);