diff --git a/common/daemon_conn.c b/common/daemon_conn.c index fe6abcd18..95882eb81 100644 --- a/common/daemon_conn.c +++ b/common/daemon_conn.c @@ -16,7 +16,10 @@ struct io_plan *daemon_conn_read_next(struct io_conn *conn, struct io_plan *daemon_conn_write_next(struct io_conn *conn, struct daemon_conn *dc) { - const u8 *msg = msg_dequeue(&dc->out); + const u8 *msg; + +again: + msg = msg_dequeue(&dc->out); if (msg) { int fd = msg_extract_fd(msg); if (fd >= 0) @@ -25,11 +28,10 @@ struct io_plan *daemon_conn_write_next(struct io_conn *conn, return io_write_wire(conn, take(msg), daemon_conn_write_next, dc); } else if (dc->msg_queue_cleared_cb) { - return dc->msg_queue_cleared_cb(conn, dc); - } else { - return msg_queue_wait(conn, &dc->out, - daemon_conn_write_next, dc); + if (dc->msg_queue_cleared_cb(conn, dc)) + goto again; } + return msg_queue_wait(conn, &dc->out, daemon_conn_write_next, dc); } bool daemon_conn_sync_flush(struct daemon_conn *dc) diff --git a/common/daemon_conn.h b/common/daemon_conn.h index fec23720c..76ea57df9 100644 --- a/common/daemon_conn.h +++ b/common/daemon_conn.h @@ -24,10 +24,9 @@ struct daemon_conn { struct io_plan *(*daemon_conn_recv)(struct io_conn *conn, struct daemon_conn *); - /* Called whenever we've cleared the msg_out queue. Used to - * inject things into the write loop */ - struct io_plan *(*msg_queue_cleared_cb)(struct io_conn *conn, - struct daemon_conn *); + /* Called whenever we've cleared the msg_out queue. If it returns + * true, it has added packets to msg_out queue. */ + bool (*msg_queue_cleared_cb)(struct io_conn *, struct daemon_conn *); }; /** diff --git a/gossipd/gossip.c b/gossipd/gossip.c index d27e9cdb4..652415fd1 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -423,8 +423,7 @@ static struct io_plan *init_new_peer(struct io_conn *conn, static struct io_plan *owner_msg_in(struct io_conn *conn, struct daemon_conn *dc); -static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn, - struct daemon_conn *dc); +static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc); /* Create a node_announcement with the given signature. It may be NULL * in the case we need to create a provisional announcement for the @@ -914,7 +913,7 @@ static bool send_peer_with_fds(struct peer *peer, const u8 *msg) * * Registered as `msg_queue_cleared_cb` by the `peer->remote`. */ -static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc) +static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc) { struct queued_message *next; struct peer *peer = dc->ctx; @@ -925,22 +924,20 @@ static struct io_plan *nonlocal_dump_gossip(struct io_conn *conn, struct daemon_ /* Nothing to do if we're not gossiping */ if (!peer->gossip_sync) - return msg_queue_wait(conn, &peer->remote->out, - daemon_conn_write_next, dc); + return false; next = next_broadcast_message(peer->daemon->rstate->broadcasts, &peer->broadcast_index); if (!next) { peer->gossip_sync = false; - return msg_queue_wait(conn, &peer->remote->out, - daemon_conn_write_next, dc); + return false; } else { u8 *msg = towire_gossip_send_gossip(conn, peer->broadcast_index, next->payload); - return io_write_wire(conn, take(msg), - nonlocal_dump_gossip, dc); + daemon_conn_send(peer->remote, take(msg)); + return true; } }