mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Add early-return possibility for the base read method.
This commit is contained in:
parent
88b628ba98
commit
b992bce88f
@ -412,7 +412,20 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
|||||||
struct timespec timeout_endtime(timespec_now() +
|
struct timespec timeout_endtime(timespec_now() +
|
||||||
read_timeout_constant_ + (read_timeout_multiplier_ * size));
|
read_timeout_constant_ + (read_timeout_multiplier_ * size));
|
||||||
|
|
||||||
|
// If there are already some bytes waiting to read, put those in the return
|
||||||
|
// buffer before setting up the first select call. This is important for
|
||||||
|
// performance reasons, as select/pselect can relinquish the thread even
|
||||||
|
// with data waiting.
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
|
if (available() > 0) {
|
||||||
|
ssize_t bytes_read_now = ::read (fd_, buf, size);
|
||||||
|
if (bytes_read_now < 1) {
|
||||||
|
throw SerialException ("device reports readiness to read but "
|
||||||
|
"returned no data (device disconnected?)");
|
||||||
|
}
|
||||||
|
bytes_read += static_cast<size_t> (bytes_read_now);
|
||||||
|
}
|
||||||
|
|
||||||
while (bytes_read < size) {
|
while (bytes_read < size) {
|
||||||
// Must determine whether the time remaining before endtime (total read
|
// Must determine whether the time remaining before endtime (total read
|
||||||
// timeout) or the inter-byte timeout is sooner, and use that one as the
|
// timeout) or the inter-byte timeout is sooner, and use that one as the
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user