+ switch (tag) {
+ case MSG_DONE:
+ if (len != 0 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ flist_ndx_push(&redo_list, -1);
+ break;
+ case MSG_REDO:
+ if (len != 4 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, 4);
+ if (remove_sent_files)
+ decrement_active_files(IVAL(buf,0));
+ flist_ndx_push(&redo_list, IVAL(buf,0));
+ break;
+ case MSG_DELETED:
+ if (len >= (int)sizeof buf || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, len);
+ io_multiplex_write(MSG_DELETED, buf, len);
+ break;
+ case MSG_SUCCESS:
+ if (len != 4 || !am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, buf, len);
+ if (remove_sent_files) {
+ decrement_active_files(IVAL(buf,0));
+ io_multiplex_write(MSG_SUCCESS, buf, len);
+ }
+ if (preserve_hard_links)
+ flist_ndx_push(&hlink_list, IVAL(buf,0));
+ break;
+ case MSG_SOCKERR:
+ if (!am_generator) {
+ rprintf(FERROR, "invalid message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+ close_multiplexing_out();
+ /* FALL THROUGH */
+ case MSG_INFO:
+ case MSG_ERROR:
+ case MSG_LOG:
+ while (len) {
+ n = len;
+ if (n >= sizeof buf)
+ n = sizeof buf - 1;
+ read_loop(fd, buf, n);
+ rwrite((enum logcode)tag, buf, n);
+ len -= n;
+ }
+ break;
+ default:
+ rprintf(FERROR, "unknown message %d:%d\n", tag, len);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ msg_fd_in = fd;
+}
+
+/* This is used by the generator to limit how many file transfers can
+ * be active at once when --remove-sent-files is specified. Without
+ * this, sender-side deletions were mostly happening at the end. */
+void increment_active_files(int ndx, int itemizing, enum logcode code)
+{
+ /* TODO: tune these limits? */
+ while (active_filecnt >= (active_bytecnt >= 128*1024 ? 10 : 50)) {
+ if (hlink_list.head)
+ check_for_finished_hlinks(itemizing, code);
+ read_msg_fd();
+ }
+
+ active_filecnt++;
+ active_bytecnt += the_file_list->files[ndx]->length;
+}
+
+void decrement_active_files(int ndx)
+{
+ active_filecnt--;
+ active_bytecnt -= the_file_list->files[ndx]->length;
+}
+
+/* Try to push messages off the list onto the wire. If we leave with more
+ * to do, return 0. On error, return -1. If everything flushed, return 1.
+ * This is only active in the receiver. */
+static int msg_list_flush(int flush_it_all)
+{
+ static int written = 0;
+ struct timeval tv;
+ fd_set fds;
+
+ if (msg_fd_out < 0)
+ return -1;
+
+ while (msg_list.head) {
+ struct msg_list_item *ml = msg_list.head;
+ int n = write(msg_fd_out, ml->buf + written, ml->len - written);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ if (errno != EWOULDBLOCK && errno != EAGAIN)
+ return -1;
+ if (!flush_it_all)
+ return 0;
+ FD_ZERO(&fds);
+ FD_SET(msg_fd_out, &fds);
+ tv.tv_sec = select_timeout;
+ tv.tv_usec = 0;
+ if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
+ check_timeout();
+ } else if ((written += n) == ml->len) {
+ free(ml->buf);
+ msg_list.head = ml->next;
+ if (!msg_list.head)
+ msg_list.tail = NULL;
+ free(ml);
+ written = 0;
+ }