From 10260e2f2490df1d8d9494ba195345115b7ae282 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 8 Dec 2018 11:05:56 +1030 Subject: [PATCH] lightningd: expose lower-level APIs. We need these for literal copying of requests between plugin and client. Signed-off-by: Rusty Russell --- lightningd/json_stream.c | 9 ++++++--- lightningd/json_stream.h | 9 +++++++++ lightningd/jsonrpc.c | 28 +++++++++++++++------------- lightningd/jsonrpc.h | 4 ++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c index 11ab28150..3ddf00b67 100644 --- a/lightningd/json_stream.c +++ b/lightningd/json_stream.c @@ -95,15 +95,18 @@ static void js_written_some(struct json_stream *js) io_wake(js); } -void json_stream_append(struct json_stream *js, const char *str) +void json_stream_append_part(struct json_stream *js, const char *str, size_t len) { - size_t len = strlen(str); - mkroom(js, len); memcpy(membuf_add(&js->outbuf, len), str, len); js_written_some(js); } +void json_stream_append(struct json_stream *js, const char *str) +{ + json_stream_append_part(js, str, strlen(str)); +} + static void json_stream_append_vfmt(struct json_stream *js, const char *fmt, va_list ap) { diff --git a/lightningd/json_stream.h b/lightningd/json_stream.h index 29c05fce3..64632da39 100644 --- a/lightningd/json_stream.h +++ b/lightningd/json_stream.h @@ -55,6 +55,15 @@ void json_object_end(struct json_stream *js); */ void json_stream_append(struct json_stream *js, const char *str); +/** + * json_stream_append_part - literally insert part of string into json_stream. + * @js: the json_stream. + * @str: the string. + * @len: the length to append (<= strlen(str)). + */ +void json_stream_append_part(struct json_stream *js, const char *str, + size_t len); + /** * json_stream_append_fmt - insert formatted string into the json_stream. * @js: the json_stream. diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 857ac92ac..e969e64a9 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -386,14 +386,9 @@ static void destroy_command(struct command *cmd) list_del_from(&cmd->jcon->commands, &cmd->list); } -void command_success(struct command *cmd, struct json_stream *result) +void command_raw_complete(struct command *cmd, struct json_stream *result) { - assert(cmd); - assert(cmd->have_json_stream); - json_stream_append(result, " }\n\n"); json_stream_close(result, cmd); - if (cmd->ok) - *(cmd->ok) = true; /* If we have a jcon, it will free result for us. */ if (cmd->jcon) @@ -402,19 +397,26 @@ void command_success(struct command *cmd, struct json_stream *result) tal_free(cmd); } +void command_success(struct command *cmd, struct json_stream *result) +{ + assert(cmd); + assert(cmd->have_json_stream); + json_stream_append(result, " }\n\n"); + if (cmd->ok) + *(cmd->ok) = true; + + command_raw_complete(cmd, result); +} + void command_failed(struct command *cmd, struct json_stream *result) { assert(cmd->have_json_stream); /* Have to close error */ json_stream_append(result, " } }\n\n"); - json_stream_close(result, cmd); if (cmd->ok) *(cmd->ok) = false; - /* If we have a jcon, it will free result for us. */ - if (cmd->jcon) - tal_steal(cmd->jcon, result); - tal_free(cmd); + command_raw_complete(cmd, result); } void PRINTF_FMT(3, 4) command_fail(struct command *cmd, int code, @@ -456,7 +458,7 @@ static void json_command_malformed(struct json_connection *jcon, json_stream_close(js, NULL); } -static struct json_stream *attach_json_stream(struct command *cmd) +struct json_stream *json_stream_raw_for_cmd(struct command *cmd) { struct json_stream *js; @@ -473,7 +475,7 @@ static struct json_stream *attach_json_stream(struct command *cmd) static struct json_stream *json_start(struct command *cmd) { - struct json_stream *js = attach_json_stream(cmd); + struct json_stream *js = json_stream_raw_for_cmd(cmd); json_stream_append_fmt(js, "{ \"jsonrpc\": \"2.0\", \"id\" : %s, ", cmd->id); diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index bc3735887..4b6785d65 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -98,6 +98,10 @@ void PRINTF_FMT(3, 4) command_fail(struct command *cmd, int code, /* Mainly for documentation, that we plan to close this later. */ void command_still_pending(struct command *cmd); +/* For low-level JSON stream access: */ +struct json_stream *json_stream_raw_for_cmd(struct command *cmd); +void command_raw_complete(struct command *cmd, struct json_stream *result); + /** * Create a new jsonrpc to wrap all related information. *