提交 e5e71a1f authored 作者: Serhij S's avatar Serhij S

keep standard I/O errors

上级 d413f7ef
use crate::locking::MutexGuard; use crate::{locking::MutexGuard, Error};
use rtsc::data_policy::DataDeliveryPolicy; use rtsc::data_policy::DataDeliveryPolicy;
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
...@@ -60,6 +60,29 @@ impl Client { ...@@ -60,6 +60,29 @@ impl Client {
} }
} }
impl Read for Client {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self.0.read_exact(buf) {
Ok(()) => Ok(buf.len()),
Err(Error::IO(e)) => Err(e),
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
}
}
impl Write for Client {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self.0.write(buf) {
Ok(()) => Ok(buf.len()),
Err(Error::IO(e)) => Err(e),
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
pub struct SessionGuard { pub struct SessionGuard {
client: Client, client: Client,
session_id: usize, session_id: usize,
......
...@@ -44,7 +44,7 @@ pub mod thread_rt; ...@@ -44,7 +44,7 @@ pub mod thread_rt;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// The crate error type /// The crate error type
#[derive(thiserror::Error, Debug, PartialEq, Eq)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
/// the channel is full and the value can not be sent /// the channel is full and the value can not be sent
#[error("channel full")] #[error("channel full")]
...@@ -68,9 +68,12 @@ pub enum Error { ...@@ -68,9 +68,12 @@ pub enum Error {
/// Timeouts /// Timeouts
#[error("timed out")] #[error("timed out")]
Timeout, Timeout,
/// I/O and threading errors /// Standard I/O errors
#[error("I/O error: {0}")] #[error("I/O error: {0}")]
IO(String), IO(#[from] std::io::Error),
// Non-standard I/O errors
#[error("Communication error: {0}")]
Comm(String),
/// 3rd party API errors /// 3rd party API errors
#[error("API error {0}: {1}")] #[error("API error {0}: {1}")]
API(String, i64), API(String, i64),
...@@ -149,10 +152,9 @@ macro_rules! impl_error { ...@@ -149,10 +152,9 @@ macro_rules! impl_error {
}; };
} }
impl_error!(std::io::Error, IO);
#[cfg(feature = "modbus")] #[cfg(feature = "modbus")]
impl_error!(rmodbus::ErrorKind, IO); impl_error!(rmodbus::ErrorKind, Comm);
impl_error!(oneshot::RecvError, IO); impl_error!(oneshot::RecvError, Comm);
impl_error!(num::ParseIntError, InvalidData); impl_error!(num::ParseIntError, InvalidData);
impl_error!(num::ParseFloatError, InvalidData); impl_error!(num::ParseFloatError, InvalidData);
impl_error!(binrw::Error, BinRw); impl_error!(binrw::Error, BinRw);
...@@ -165,7 +167,7 @@ impl Error { ...@@ -165,7 +167,7 @@ impl Error {
Error::InvalidData(msg.to_string()) Error::InvalidData(msg.to_string())
} }
pub fn io<S: fmt::Display>(msg: S) -> Self { pub fn io<S: fmt::Display>(msg: S) -> Self {
Error::IO(msg.to_string()) Error::Comm(msg.to_string())
} }
pub fn failed<S: fmt::Display>(msg: S) -> Self { pub fn failed<S: fmt::Display>(msg: S) -> Self {
Error::Failed(msg.to_string()) Error::Failed(msg.to_string())
......
...@@ -496,14 +496,14 @@ fn thread_init_external( ...@@ -496,14 +496,14 @@ fn thread_init_external(
) -> Result<libc::c_int> { ) -> Result<libc::c_int> {
let (tid, tx_ok) = rx_tid.recv()?; let (tid, tx_ok) = rx_tid.recv()?;
if tid < 0 { if tid < 0 {
tx_ok.send(false).map_err(|e| Error::IO(e.to_string()))?; tx_ok.send(false).map_err(|e| Error::Comm(e.to_string()))?;
return Err(Error::RTGetTId(tid)); return Err(Error::RTGetTId(tid));
} }
if let Err(e) = apply_thread_params(tid, params, quiet) { if let Err(e) = apply_thread_params(tid, params, quiet) {
tx_ok.send(false).map_err(|e| Error::IO(e.to_string()))?; tx_ok.send(false).map_err(|e| Error::Comm(e.to_string()))?;
return Err(e); return Err(e);
} }
tx_ok.send(true).map_err(|e| Error::IO(e.to_string()))?; tx_ok.send(true).map_err(|e| Error::Comm(e.to_string()))?;
Ok(tid) Ok(tid)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论