connection::thread_loop(): Detect read errors and end of file reliably

This commit is contained in:
Petr Baudis 2011-11-30 01:52:04 +01:00
parent f161c02210
commit 56979634bf

View file

@ -186,16 +186,20 @@ connection::thread_loop(void)
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; tv.tv_usec = 0; tv.tv_sec = 0; tv.tv_usec = 0;
fd_set rfds; fd_set rfds; FD_ZERO(&rfds); FD_SET(fd, &rfds);
FD_ZERO(&rfds); fd_set efds; FD_ZERO(&efds); FD_SET(fd, &efds);
FD_SET(fd, &rfds); while (select(fd + 1, &rfds, NULL, &efds, &tv)) {
while (select(fd + 1, &rfds, NULL, &rfds, &tv)) { if (FD_ISSET(fd, &efds)) {
error = true;
break;
}
char cbuf[1024]; char cbuf[1024];
len = read(fd, cbuf, sizeof(cbuf)); len = read(fd, cbuf, sizeof(cbuf));
if (len < 0) { if (len <= 0) {
error = true; error = true;
} else if (len == 0) {
break; break;
} else { } else {
bool want_moar = false; bool want_moar = false;
pthread_mutex_lock(&buf_lock); pthread_mutex_lock(&buf_lock);