diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c index 3ddf00b67..611537a6b 100644 --- a/lightningd/json_stream.c +++ b/lightningd/json_stream.c @@ -59,6 +59,17 @@ struct json_stream *new_json_stream(const tal_t *ctx, struct command *writer) return js; } +struct json_stream *json_stream_dup(const tal_t *ctx, struct json_stream *original) +{ + size_t num_elems = membuf_num_elems(&original->outbuf); + char *elems = membuf_elems(&original->outbuf); + struct json_stream *js = tal_dup(ctx, struct json_stream, original); + membuf_init(&js->outbuf, tal_dup_arr(js, char, elems, num_elems, 0), + num_elems, membuf_tal_realloc); + membuf_added(&js->outbuf, num_elems); + return js; +} + bool json_stream_still_writing(const struct json_stream *js) { return js->writer != NULL; diff --git a/lightningd/json_stream.h b/lightningd/json_stream.h index 64632da39..3e7e4f494 100644 --- a/lightningd/json_stream.h +++ b/lightningd/json_stream.h @@ -23,6 +23,19 @@ struct json_stream; */ struct json_stream *new_json_stream(const tal_t *ctx, struct command *writer); +/** + * Duplicate an existing stream. + * + * Mostly useful when we want to send copies of a given stream to + * multiple recipients, that might read at different speeds from the + * stream. For example this is used when construcing a single + * notification and then duplicating it for the fanout. + * + * @ctx: tal context for allocation. + * @original: the stream to duplicate. + */ +struct json_stream *json_stream_dup(const tal_t *ctx, struct json_stream *original); + /** * json_stream_close - finished writing to a JSON stream. * @js: the json_stream.