Browse Source

json: Add function to duplicate a json_stream

Will be used in the next commit to fan out notifications to multiple
subscribing plugins. We can't just use `tal_dup` from outside since
the definition is hidden outside the compilation unit.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
pr-2218
Christian Decker 6 years ago
parent
commit
dd2696a88b
  1. 11
      lightningd/json_stream.c
  2. 13
      lightningd/json_stream.h

11
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;

13
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.

Loading…
Cancel
Save