CARMA C++
ReaderWithTimeout.h
1 #ifndef CARMA_UI_RTD_READERWITHTIMEOUT_H
2 #define CARMA_UI_RTD_READERWITHTIMEOUT_H
3 
4 /*
5  * @file
6  *
7  * Reads from stdin with a timeout
8  *
9  * @author Steve Scott
10  * $id: $
11  *
12  * $CarmaCopyright$
13  */
14 
15 #include <sys/time.h>
16 
17 #include <cstdio>
18 #include <string>
19 
20 namespace carma {
21 namespace ui {
22 namespace rtd {
23 
34 public:
38  ReaderWithTimeout(const struct ::timeval tv);
40  ReaderWithTimeout(const int fd, const struct ::timeval tv);
41 
49  bool getBytes(std::string &bytes, struct ::timeval tv);
50  bool getBytes(std::string &bytes);
51 
56  int getErrorCode() const;
57 
59  enum {
60  SUCCESS, // successful read
61  BROKEN_CNX, // broken connection
62  TIMEOUT, // timeout
63  IOCTL_ERROR, // error with ioctl(FIONREAD)
64  POLL_ERROR, // error with poll()
65  READ_ERROR, // error with read()
66  };
67 
68 private:
69  bool getNBytes(int nBytes, struct ::timeval *tv, std::string &bytes);
70 
71  const int inputFile; // file descriptor
72  struct ::timeval defaultTimeout;
73  int errorCode;
74 };
75 
76 } // namespace carma::ui::rtd
77 } // namespace carma::ui
78 } // namespace carma
79 
80 #endif // CARMA_UI_RTD_READERWITHTIMEOUT_H
int getErrorCode() const
Get error code of last IO.
Read a series of bytes with a timeout and return a raw byte string with the contents of the read...
bool getBytes(std::string &bytes, struct::timeval tv)
Read input from file and put into input string.