daemon_conn: simplify msg_queue_cleared_cb.

Now it just returns true if it queued something.  This allows it
to queue multiple packets, and lets it share code paths with other code
in future patches.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-03-14 02:14:55 +10:30
parent 87effd90c2
commit 5e333b75b9
3 changed files with 16 additions and 18 deletions

View File

@ -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)

View File

@ -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 *);
};
/**

View File

@ -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;
}
}