// A persistent read function. static ssize_t read_r(int fd, void *buf, size_t count, size_t ms) { double tMax = ms / 1000.0; // convert to seconds PosixTimer t; int r; unsigned int total = 0; while (total < count) { r = read(fd, ((unsigned char *)buf) + total, count - total); // did something seriously go wrong? if (r < 0 && errno != EINTR) { __dbg(string("PosixSerial: failed to read: ") + string(strerror(errno))); throw PosixSerial::ReadFailure(); } else if (r > 0) { // progress! total += r; t.start(); // restart the timer } // are we out of time? if (ms > 0 && t.elapsed() > tMax) { __dbg(string("PosixSerial: read timed out")); throw PosixSerial::ReadTimeout(); } } return total; }
// A persistent write function. static ssize_t write_r(int fd, const void *buf, size_t count, size_t ms) { double tMax = ms / 1000.0; // convert to seconds PosixTimer t; int r; unsigned int written = 0; while (written < count) { r = write(fd, ((const unsigned char *)buf) + written, count - written); // did something seriously go wrong? if (r < 0 && errno != EINTR) { __dbg(string("PosixSerial: failed to write: ") + string(strerror(errno))); throw PosixSerial::WriteFailure(); } else if (r > 0) { // progress! written += r; t.start(); // restart the timer } // are we out of time? if (ms > 0 && t.elapsed() > tMax) { __dbg(string("PosixSerial: write timed out")); throw PosixSerial::WriteTimeout(); } } return written; }