+ static size_t remaining;
+ int tag, ret = 0;
+ char line[1024];
+
+ if (!io_multiplexing_in || fd != multiplex_in_fd)
+ return read_timeout(fd, buf, len);
+
+ while (ret == 0) {
+ if (remaining) {
+ len = MIN(len, remaining);
+ read_loop(fd, buf, len);
+ remaining -= len;
+ ret = len;
+ continue;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ remaining = tag & 0xFFFFFF;
+ tag = tag >> 24;
+
+ if (tag == MPLEX_BASE)
+ continue;
+
+ tag -= MPLEX_BASE;
+
+ if (tag != FERROR && tag != FINFO) {
+ rprintf(FERROR, "unexpected tag %d\n", tag);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ if (remaining > sizeof(line) - 1) {
+ rprintf(FERROR, "multiplexing overflow %d\n\n",
+ remaining);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ read_loop(fd, line, remaining);
+ line[remaining] = 0;
+
+ rprintf((enum logcode) tag, "%s", line);
+ remaining = 0;
+ }
+
+ return ret;
+}
+
+
+
+/**
+ * Do a buffered read from @p fd. Don't return until all @p n bytes
+ * have been read. If all @p n can't be read then exit with an
+ * error.
+ **/
+static void readfd (int fd, char *buffer, size_t N)
+{
+ int ret;
+ size_t total=0;
+
+ while (total < N) {
+ io_flush();
+
+ ret = read_unbuffered (fd, buffer + total, N-total);
+ total += ret;
+ }
+
+ stats.total_read += total;
+}
+
+
+int32 read_int(int f)
+{
+ char b[4];
+ int32 ret;
+
+ readfd(f,b,4);
+ ret = IVAL(b,0);
+ if (ret == (int32)0xffffffff) return -1;
+ return ret;
+}
+
+int64 read_longint(int f)
+{
+ extern int remote_version;
+ int64 ret;
+ char b[8];
+ ret = read_int(f);
+
+ if ((int32)ret != (int32)0xffffffff) {
+ return ret;
+ }
+
+#ifdef NO_INT64
+ rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
+ exit_cleanup(RERR_UNSUPPORTED);
+#else
+ if (remote_version >= 16) {
+ readfd(f,b,8);
+ ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
+ }
+#endif
+
+ return ret;
+}
+
+void read_buf(int f,char *buf,size_t len)
+{
+ readfd(f,buf,len);
+}
+
+void read_sbuf(int f,char *buf,size_t len)
+{
+ read_buf (f,buf,len);
+ buf[len] = 0;
+}
+
+unsigned char read_byte(int f)
+{
+ unsigned char c;
+ read_buf (f, (char *)&c, 1);
+ return c;
+}
+
+
+/**
+ * Sleep after writing to limit I/O bandwidth usage.
+ *
+ * @todo Rather than sleeping after each write, it might be better to
+ * use some kind of averaging. The current algorithm seems to always
+ * use a bit less bandwidth than specified, because it doesn't make up
+ * for slow periods. But arguably this is a feature. In addition, we
+ * ought to take the time used to write the data into account.
+ **/
+static void sleep_for_bwlimit(int bytes_written)
+{
+ struct timeval tv;
+
+ if (!bwlimit)
+ return;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = bytes_written * 1000 / bwlimit;
+
+ while (tv.tv_usec > 1000000) {
+ tv.tv_sec++;
+ tv.tv_usec -= 1000000;
+ }
+ select(0, NULL, NULL, NULL, tv);
+}
+
+
+/**
+ * Write len bytes to the file descriptor @p fd.
+ *
+ * This function underlies the multiplexing system. The body of the
+ * application never calls this function directly.
+ **/
+static void writefd_unbuffered(int fd,char *buf,size_t len)
+{
+ size_t total = 0;
+ fd_set w_fds, r_fds;
+ int fd_count, count;
+ struct timeval tv;
+
+ err_list_push();
+
+ no_flush++;
+
+ while (total < len) {
+ FD_ZERO(&w_fds);
+ FD_ZERO(&r_fds);
+ FD_SET(fd,&w_fds);
+ fd_count = fd;
+
+ if (io_error_fd != -1) {
+ FD_SET(io_error_fd,&r_fds);
+ if (io_error_fd > fd_count)
+ fd_count = io_error_fd;
+ }
+
+ tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ errno = 0;
+
+ count = select(fd_count+1,
+ io_error_fd != -1?&r_fds:NULL,
+ &w_fds,NULL,
+ &tv);
+
+ if (count == 0) {
+ check_timeout();
+ }
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ continue;
+ }
+
+ if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
+ read_error_fd();
+ }
+
+ if (FD_ISSET(fd, &w_fds)) {
+ int ret;
+ size_t n = len-total;
+ ret = write(fd,buf+total,n);
+
+ if (ret == -1 && errno == EINTR) {
+ continue;
+ }
+
+ if (ret == -1 &&
+ (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ msleep(1);
+ continue;
+ }
+
+ if (ret <= 0) {
+ /* Don't try to write errors back
+ * across the stream */
+ io_multiplexing_close();
+ rprintf(FERROR, RSYNC_NAME
+ ": writefd_unbuffered failed to write %ld bytes: %s\n",
+ (long) len,
+ strerror(errno));
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ sleep_for_bwlimit(ret);