Skip to content

Commit

Permalink
Fix slow reads on Windows
Browse files Browse the repository at this point in the history
- Removes bad timeout overrides
- See:
  - berkowski#38
  - berkowski#47
  - serialport/serialport-rs#79
  - serialport/serialport-rs#73 (comment)
- Cleans up unused imports
  • Loading branch information
parasyte committed Jul 22, 2024
1 parent 49b1c56 commit d03fb20
Showing 1 changed file with 4 additions and 43 deletions.
47 changes: 4 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,15 @@ mod os_prelude {
pub use mio::windows::NamedPipe;
pub use serialport::COMPort as NativeBlockingSerialPort;
pub use std::ffi::OsStr;
pub use std::io::{self, Read, Write};
pub use std::mem;
pub use std::os::windows::ffi::OsStrExt;
pub use std::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle};
pub use std::os::windows::io::FromRawHandle;
pub use std::path::Path;
pub use std::ptr;
pub use std::time::Duration;
pub use winapi::shared::minwindef::TRUE;
pub use winapi::um::commapi::SetCommTimeouts;
pub use winapi::um::fileapi::*;
pub use winapi::um::handleapi::{DuplicateHandle, INVALID_HANDLE_VALUE};
pub use winapi::um::processthreadsapi::GetCurrentProcess;
pub use winapi::um::winbase::{COMMTIMEOUTS, FILE_FLAG_OVERLAPPED};
pub use winapi::um::winnt::{
DUPLICATE_SAME_ACCESS, FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE,
};
pub use winapi::um::handleapi::INVALID_HANDLE_VALUE;
pub use winapi::um::winbase::FILE_FLAG_OVERLAPPED;
pub use winapi::um::winnt::{FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE};
}
use os_prelude::*;

Expand Down Expand Up @@ -623,7 +616,6 @@ impl TryFrom<NativeBlockingSerialPort> for SerialStream {
com_port.set_data_bits(data_bits)?;
com_port.set_stop_bits(stop_bits)?;
com_port.set_flow_control(flow_control)?;
sys::override_comm_timeouts(handle)?;

Ok(Self {
inner: com_port,
Expand Down Expand Up @@ -724,7 +716,6 @@ mod io {
#[cfg(windows)]
mod io {
use super::{NativeBlockingSerialPort, SerialStream, StdIoResult};
use crate::sys;
use mio::windows::NamedPipe;
use std::io::{Read, Write};
use std::mem;
Expand Down Expand Up @@ -762,12 +753,9 @@ mod io {
}

impl FromRawHandle for SerialStream {
/// This method can potentially fail to override the communication timeout
/// value set in `sys::override_comm_timeouts` without any indication to the user.
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
let inner = mem::ManuallyDrop::new(NativeBlockingSerialPort::from_raw_handle(handle));
let pipe = NamedPipe::from_raw_handle(handle);
sys::override_comm_timeouts(handle).ok();

Self { inner, pipe }
}
Expand Down Expand Up @@ -799,33 +787,6 @@ mod sys {
}
}

#[cfg(windows)]
mod sys {

use super::os_prelude::*;
use super::StdIoResult;
/// Overrides timeout value set by serialport-rs so that the read end will
/// never wake up with 0-byte payload.
pub(crate) fn override_comm_timeouts(handle: RawHandle) -> StdIoResult<()> {
let mut timeouts = COMMTIMEOUTS {
// wait at most 1ms between two bytes (0 means no timeout)
ReadIntervalTimeout: 1,
// disable "total" timeout to wait at least 1 byte forever
ReadTotalTimeoutMultiplier: 0,
ReadTotalTimeoutConstant: 0,
// write timeouts are just copied from serialport-rs
WriteTotalTimeoutMultiplier: 0,
WriteTotalTimeoutConstant: 0,
};

let r = unsafe { SetCommTimeouts(handle, &mut timeouts) };
if r == 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
}

#[cfg(unix)]
impl Source for SerialStream {
#[inline(always)]
Expand Down

0 comments on commit d03fb20

Please sign in to comment.