wire_sync_write: support take()

We often want it to free the message after writing.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-06-23 16:03:20 +09:30
parent 8c6fab0ec4
commit 40ce29beac
5 changed files with 11 additions and 7 deletions

View File

@ -46,7 +46,7 @@ bool daemon_conn_sync_flush(struct daemon_conn *dc)
if (fd >= 0) {
if (!fdpass_send(io_conn_fd(dc->conn), fd))
return false;
} else if (!wire_sync_write(io_conn_fd(dc->conn), msg))
} else if (!wire_sync_write(io_conn_fd(dc->conn), take(msg)))
return false;
}
return true;

View File

@ -187,7 +187,7 @@ static u8 *read_next_peer_msg(struct state *state, const tal_t *ctx)
tal_free(pong);
} else if (gossip_msg(msg)) {
/* We relay gossip to gossipd, but don't relay from */
if (!wire_sync_write(GOSSIP_FD, msg))
if (!wire_sync_write(GOSSIP_FD, take(msg)))
peer_failed(PEER_FD, &state->cs, NULL,
WIRE_OPENING_PEER_WRITE_FAILED,
"Relaying gossip message");

View File

@ -65,9 +65,8 @@ static void status_send_with_hdr(u16 type, const void *p, size_t len)
tal_resize(&msg, 65535);
if (status_fd >= 0) {
if (!wire_sync_write(status_fd, msg))
if (!wire_sync_write(status_fd, take(msg)))
err(1, "Writing out status %u len %zu", type, len);
tal_free(msg);
} else {
daemon_conn_send(status_conn, take(msg));
}

View File

@ -3,13 +3,18 @@
#include <ccan/endian/endian.h>
#include <ccan/read_write_all/read_write_all.h>
bool wire_sync_write(int fd, const void *msg)
bool wire_sync_write(int fd, const void *msg TAKES)
{
be16 be_len = cpu_to_be16(tal_count(msg));
bool ret;
assert(be16_to_cpu(be_len) == tal_count(msg));
return write_all(fd, &be_len, sizeof(be_len))
ret = write_all(fd, &be_len, sizeof(be_len))
&& write_all(fd, msg, tal_count(msg));
if (taken(msg))
tal_free(msg);
return ret;
}
u8 *wire_sync_read(const tal_t *ctx, int fd)

View File

@ -4,7 +4,7 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
bool wire_sync_write(int fd, const void *msg);
bool wire_sync_write(int fd, const void *msg TAKES);
u8 *wire_sync_read(const tal_t *ctx, int fd);
#endif /* LIGHTNING_WIRE_WIRE_SYNC_H */