+ static size_t remaining;
+ int tag, ret = 0;
+ char line[1024];
+ static char *buffer;
+ static size_t bufferIdx = 0;
+ static size_t bufferSz;
+
+ if (fd != multiplex_in_fd)
+ return read_timeout(fd, buf, len);
+
+ if (!io_multiplexing_in && remaining == 0) {
+ if (!buffer) {
+ bufferSz = 2 * IO_BUFFER_SIZE;
+ buffer = new_array(char, bufferSz);
+ if (!buffer) out_of_memory("read_unbuffered");
+ }
+ remaining = read_timeout(fd, buffer, bufferSz);
+ bufferIdx = 0;
+ }
+
+ while (ret == 0) {
+ if (remaining) {
+ len = MIN(len, remaining);
+ memcpy(buf, buffer + bufferIdx, len);
+ bufferIdx += len;
+ remaining -= len;
+ ret = len;
+ break;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ remaining = tag & 0xFFFFFF;
+ tag = (tag >> 24) - MPLEX_BASE;
+
+ switch (tag) {
+ case MSG_DATA:
+ if (!buffer || remaining > bufferSz) {
+ buffer = realloc_array(buffer, char, remaining);
+ if (!buffer) out_of_memory("read_unbuffered");
+ bufferSz = remaining;
+ }
+ read_loop(fd, buffer, remaining);
+ bufferIdx = 0;
+ break;
+ case MSG_INFO:
+ case MSG_ERROR:
+ if (remaining >= sizeof line) {
+ rprintf(FERROR, "multiplexing overflow %d:%ld\n\n",
+ tag, (long)remaining);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, line, remaining);
+ rwrite((enum logcode)tag, line, remaining);
+ remaining = 0;
+ break;
+ default:
+ rprintf(FERROR, "unexpected tag %d\n", tag);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ }
+
+ if (remaining == 0)
+ io_flush(NORMAL_FLUSH);
+
+ 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) {
+ 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)
+{
+ 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
+ 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;
+
+ assert(bytes_written > 0);
+ assert(bwlimit > 0);
+
+ tv.tv_usec = bytes_written * 1000 / bwlimit;
+ tv.tv_sec = tv.tv_usec / 1000000;
+ tv.tv_usec = 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;
+
+ msg_list_push(NORMAL_FLUSH);
+
+ no_flush++;
+
+ while (total < len) {
+ FD_ZERO(&w_fds);
+ FD_SET(fd,&w_fds);
+ fd_count = fd;
+
+ if (msg_fd_in >= 0) {
+ FD_ZERO(&r_fds);
+ FD_SET(msg_fd_in,&r_fds);
+ if (msg_fd_in > fd_count)
+ fd_count = msg_fd_in;
+ }
+
+ tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ errno = 0;
+ count = select(fd_count+1, msg_fd_in >= 0 ? &r_fds : NULL,
+ &w_fds, NULL, &tv);
+
+ if (count == 0) {
+ msg_list_push(NORMAL_FLUSH);
+ check_timeout();
+ }
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ continue;
+ }
+
+ if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds))
+ read_msg_fd();
+
+ if (FD_ISSET(fd, &w_fds)) {
+ int ret;
+ size_t n = len-total;
+ ret = write(fd,buf+total,n);
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ if (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: phase \"%s\": %s\n",
+ (long) len, io_write_phase,
+ strerror(errno));
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ sleep_for_bwlimit(ret);