summaryrefslogtreecommitdiff
path: root/gpspipe.c
diff options
context:
space:
mode:
authorChris Kuethe <ckuethe@users.berlios.de>2010-12-27 17:38:50 -0600
committerChris Kuethe <ckuethe@users.berlios.de>2010-12-27 17:38:50 -0600
commitc2a0d4181d3ada9623a5c7ecbfc06d593f40906c (patch)
treeba57e5546706060c130e4668de85c71efa1ea745 /gpspipe.c
parente4a632563f75b013185b4e21311dd1b7c9de28fd (diff)
downloadgpsd-c2a0d4181d3ada9623a5c7ecbfc06d593f40906c.tar.gz
use select(2) rather than blindly reading fd
Another Commit from the Caribbean. Observed while reading a remote gpsd instance over slow, high latency network.
Diffstat (limited to 'gpspipe.c')
-rw-r--r--gpspipe.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/gpspipe.c b/gpspipe.c
index 3ffaef36..57aa67dc 100644
--- a/gpspipe.c
+++ b/gpspipe.c
@@ -168,6 +168,7 @@ int main(int argc, char **argv)
unsigned int vflag = 0, l = 0;
FILE *fp;
unsigned int flags;
+ fd_set fds;
struct fixsource_t source;
char *serialport = NULL;
@@ -309,16 +310,30 @@ int main(int argc, char **argv)
for (;;) {
int i = 0;
int j = 0;
- int readbytes = 0;
+ int r = 0;
+ struct timeval tv;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000;
+ FD_ZERO(&fds);
+ FD_SET(gpsdata.gps_fd, &fds);
+ errno = 0;
+ r = select(gpsdata.gps_fd+1, &fds, NULL, NULL, &tv);
+ if (r == -1 && errno != EINTR) {
+ (void)fprintf(stderr, "gpspipe: select error %s(%d)\n",
+ strerror(errno), errno);
+ exit(1);
+ } else if (r == 0)
+ continue;
if (vflag)
spinner(vflag, l++);
/* reading directly from the socket avoids decode overhead */
errno = 0;
- readbytes = (int)read(gpsdata.gps_fd, buf, sizeof(buf));
- if (readbytes > 0) {
- for (i = 0; i < readbytes; i++) {
+ r = (int)read(gpsdata.gps_fd, buf, sizeof(buf));
+ if (r > 0) {
+ for (i = 0; i < r; i++) {
char c = buf[i];
if (j < (int)(sizeof(serbuf) - 1)) {
serbuf[j++] = buf[i];
@@ -370,7 +385,7 @@ int main(int argc, char **argv)
}
}
} else {
- if (readbytes == -1) {
+ if (r == -1) {
if (errno == EAGAIN)
continue;
else