some fixes
This commit is contained in:
parent
6f7f24016e
commit
af4f1d85ec
3 changed files with 15 additions and 6 deletions
|
@ -1,7 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
net::{Shutdown, TcpStream},
|
net::TcpStream,
|
||||||
thread,
|
thread,
|
||||||
time::{Duration, SystemTime},
|
time::{Duration, SystemTime},
|
||||||
vec,
|
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);
|
return Connection::new_serial(serial);
|
||||||
}
|
}
|
||||||
Connection::new_tcp(TcpStream::connect((params.server_ip, params.server_port)).unwrap())
|
Connection::new_tcp(TcpStream::connect((params.server_ip, params.server_port)).unwrap())
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub struct Connection {
|
||||||
set_nonblocking_thunk: fn(NonNull<()>, bool) -> io::Result<()>,
|
set_nonblocking_thunk: fn(NonNull<()>, bool) -> io::Result<()>,
|
||||||
close_thunk: fn(NonNull<()>) -> io::Result<()>,
|
close_thunk: fn(NonNull<()>) -> io::Result<()>,
|
||||||
is_nb: bool,
|
is_nb: bool,
|
||||||
|
is_serial: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for Connection {
|
impl Write for Connection {
|
||||||
|
@ -23,7 +24,7 @@ impl Write for Connection {
|
||||||
self.as_write().write_vectored(bufs)
|
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)
|
self.as_write().write_all(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +104,7 @@ impl Connection {
|
||||||
data.cast::<TcpStream>().as_ref().shutdown(Shutdown::Both)
|
data.cast::<TcpStream>().as_ref().shutdown(Shutdown::Both)
|
||||||
},
|
},
|
||||||
is_nb: false,
|
is_nb: false,
|
||||||
|
is_serial: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn new_serial<T: SerialPort + 'static>(serial: T) -> Self {
|
pub fn new_serial<T: SerialPort + 'static>(serial: T) -> Self {
|
||||||
|
@ -113,7 +115,7 @@ impl Connection {
|
||||||
set_nonblocking_thunk: |data, nb| unsafe {
|
set_nonblocking_thunk: |data, nb| unsafe {
|
||||||
data.cast::<T>()
|
data.cast::<T>()
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.set_timeout(Duration::from_millis(if nb { 1 } else { 10000 }))
|
.set_timeout(Duration::from_millis(if nb { 0 } else { 600000 }))
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
io::Error::new(io::ErrorKind::ConnectionAborted, "serial port went down")
|
io::Error::new(io::ErrorKind::ConnectionAborted, "serial port went down")
|
||||||
})
|
})
|
||||||
|
@ -121,6 +123,7 @@ impl Connection {
|
||||||
// no need to close this.
|
// no need to close this.
|
||||||
close_thunk: |_data| Ok(()),
|
close_thunk: |_data| Ok(()),
|
||||||
is_nb: false,
|
is_nb: false,
|
||||||
|
is_serial: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn as_read(&mut self) -> &mut (dyn Read) {
|
fn as_read(&mut self) -> &mut (dyn Read) {
|
||||||
|
@ -129,6 +132,9 @@ impl Connection {
|
||||||
fn as_write(&mut self) -> &mut (dyn Write) {
|
fn as_write(&mut self) -> &mut (dyn Write) {
|
||||||
&mut self.readwrite
|
&mut self.readwrite
|
||||||
}
|
}
|
||||||
|
pub fn is_nonblocking(&self) -> bool {
|
||||||
|
self.is_nb
|
||||||
|
}
|
||||||
pub fn set_nonblocking(&mut self, nonblocking: bool) -> io::Result<()> {
|
pub fn set_nonblocking(&mut self, nonblocking: bool) -> io::Result<()> {
|
||||||
self.is_nb = nonblocking;
|
self.is_nb = nonblocking;
|
||||||
(self.set_nonblocking_thunk)(self.data, nonblocking)
|
(self.set_nonblocking_thunk)(self.data, nonblocking)
|
||||||
|
@ -136,4 +142,8 @@ impl Connection {
|
||||||
pub fn close(&self) -> io::Result<()> {
|
pub fn close(&self) -> io::Result<()> {
|
||||||
(self.close_thunk)(self.data)
|
(self.close_thunk)(self.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_serial(&self) -> bool {
|
||||||
|
self.is_serial
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
io::{Error, Read},
|
io::{Error, Read},
|
||||||
io::{ErrorKind, Write},
|
io::{ErrorKind, Write},
|
||||||
net::TcpStream,
|
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -101,7 +100,7 @@ impl SocketAdapter {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
match {
|
match {
|
||||||
self.internal.set_nonblocking(true)?;
|
self.internal.set_nonblocking(!self.internal.is_serial())?;
|
||||||
let r = self
|
let r = self
|
||||||
.internal
|
.internal
|
||||||
.write(&self.write[self.written..self.written + self.to_write]);
|
.write(&self.write[self.written..self.written + self.to_write]);
|
||||||
|
|
Loading…
Reference in a new issue