提交 05e24caa authored 作者: Serhij S's avatar Serhij S

timeout fixes

上级 2a76a473
...@@ -118,7 +118,9 @@ pub fn open(params: &Parameters, timeout: Duration) -> Result<SystemPort> { ...@@ -118,7 +118,9 @@ pub fn open(params: &Parameters, timeout: Duration) -> Result<SystemPort> {
Ok(()) Ok(())
}) })
.map_err(Error::io)?; .map_err(Error::io)?;
port.set_timeout(timeout).map_err(Error::io)?; if timeout > Duration::from_secs(0) {
port.set_timeout(timeout).map_err(Error::io)?;
}
Ok(port) Ok(port)
} }
......
...@@ -131,9 +131,18 @@ impl Tcp { ...@@ -131,9 +131,18 @@ impl Tcp {
fn get_stream(&self) -> Result<MutexGuard<Option<TcpStream>>> { fn get_stream(&self) -> Result<MutexGuard<Option<TcpStream>>> {
let mut lock = self.stream.lock(); let mut lock = self.stream.lock();
if lock.as_mut().is_none() { if lock.as_mut().is_none() {
let mut stream = TcpStream::connect_timeout(&self.addr, self.timeouts.connect)?; let zero_to = Duration::from_secs(0);
stream.set_read_timeout(Some(self.timeouts.read))?; let mut stream = if self.timeouts.connect > zero_to {
stream.set_write_timeout(Some(self.timeouts.write))?; TcpStream::connect_timeout(&self.addr, self.timeouts.connect)?
} else {
TcpStream::connect(self.addr)?
};
if self.timeouts.read > zero_to {
stream.set_read_timeout(Some(self.timeouts.read))?;
}
if self.timeouts.write > zero_to {
stream.set_write_timeout(Some(self.timeouts.write))?;
}
stream.set_nodelay(true)?; stream.set_nodelay(true)?;
if let Some(ref chat) = self.chat { if let Some(ref chat) = self.chat {
chat(&mut stream).map_err(Error::io)?; chat(&mut stream).map_err(Error::io)?;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论