+ no_flush--;
+}
+
+
+/**
+ * Write an message to a multiplexed stream. If this fails then rsync
+ * exits.
+ **/
+static void mplex_write(enum msgcode code, char *buf, size_t len)
+{
+ char buffer[4096];
+ size_t n = len;
+
+ SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
+
+ if (n > sizeof buffer - 4)
+ n = sizeof buffer - 4;
+
+ memcpy(&buffer[4], buf, n);
+ writefd_unbuffered(sock_f_out, buffer, n+4);
+
+ len -= n;
+ buf += n;
+
+ if (len)
+ writefd_unbuffered(sock_f_out, buf, len);
+}
+
+
+void io_flush(int flush_it_all)
+{
+ msg_list_push(flush_it_all);
+
+ if (!iobuf_out_cnt || no_flush)
+ return;
+
+ if (io_multiplexing_out)
+ mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
+ else
+ writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
+ iobuf_out_cnt = 0;
+}
+
+
+static void writefd(int fd,char *buf,size_t len)
+{
+ if (fd == msg_fd_out) {
+ rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
+ exit_cleanup(RERR_PROTOCOL);
+ }
+
+ if (fd == sock_f_out)
+ stats.total_written += len;
+
+ if (fd == write_batch_monitor_out) {
+ if ((size_t)write(batch_fd, buf, len) != len)
+ exit_cleanup(RERR_FILEIO);
+ }
+
+ if (!iobuf_out || fd != sock_f_out) {
+ writefd_unbuffered(fd, buf, len);
+ return;
+ }
+
+ while (len) {
+ int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
+ if (n > 0) {
+ memcpy(iobuf_out+iobuf_out_cnt, buf, n);
+ buf += n;
+ len -= n;
+ iobuf_out_cnt += n;
+ }
+
+ if (iobuf_out_cnt == IO_BUFFER_SIZE)
+ io_flush(NORMAL_FLUSH);
+ }
+}
+
+
+void write_shortint(int f, int x)
+{
+ uchar b[2];
+ b[0] = x;
+ b[1] = x >> 8;
+ writefd(f, (char *)b, 2);
+}
+
+
+void write_int(int f,int32 x)
+{
+ char b[4];
+ SIVAL(b,0,x);
+ writefd(f,b,4);
+}
+
+
+void write_int_named(int f, int32 x, const char *phase)
+{
+ io_write_phase = phase;
+ write_int(f, x);
+ io_write_phase = phase_unknown;
+}
+
+
+/*
+ * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
+ * 64-bit types on this platform.
+ */
+void write_longint(int f, int64 x)
+{
+ char b[8];
+
+ if (x <= 0x7FFFFFFF) {
+ write_int(f, (int)x);
+ return;
+ }
+
+#if SIZEOF_INT64 < 8
+ rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
+ exit_cleanup(RERR_UNSUPPORTED);
+#else
+ write_int(f, (int32)0xFFFFFFFF);
+ SIVAL(b,0,(x&0xFFFFFFFF));
+ SIVAL(b,4,((x>>32)&0xFFFFFFFF));
+
+ writefd(f,b,8);
+#endif
+}
+
+void write_buf(int f,char *buf,size_t len)
+{
+ writefd(f,buf,len);
+}
+
+
+/** Write a string to the connection */
+void write_sbuf(int f, char *buf)
+{
+ writefd(f, buf, strlen(buf));
+}
+
+
+void write_byte(int f, uchar c)
+{
+ writefd(f, (char *)&c, 1);
+}
+
+
+/**
+ * Read a line of up to @p maxlen characters into @p buf (not counting
+ * the trailing null). Strips the (required) trailing newline and all
+ * carriage returns.
+ *
+ * @return 1 for success; 0 for I/O error or truncation.
+ **/
+int read_line(int f, char *buf, size_t maxlen)
+{
+ while (maxlen) {
+ buf[0] = 0;
+ read_buf(f, buf, 1);
+ if (buf[0] == 0)
+ return 0;
+ if (buf[0] == '\n')
+ break;
+ if (buf[0] != '\r') {
+ buf++;
+ maxlen--;
+ }
+ }
+ *buf = '\0';
+ return maxlen > 0;
+}
+
+
+void io_printf(int fd, const char *format, ...)
+{
+ va_list ap;
+ char buf[1024];
+ int len;
+
+ va_start(ap, format);
+ len = vsnprintf(buf, sizeof buf, format, ap);
+ va_end(ap);
+
+ if (len < 0)
+ exit_cleanup(RERR_STREAMIO);
+
+ write_sbuf(fd, buf);
+}
+
+
+/** Setup for multiplexing a MSG_* stream with the data stream. */
+void io_start_multiplex_out(void)