lightningd: expose lower-level APIs.

We need these for literal copying of requests between plugin and client.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-12-08 11:05:56 +10:30
parent 8015e7dcfb
commit 10260e2f24
4 changed files with 34 additions and 16 deletions

View File

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

View File

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

View File

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

View File

@ -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.
*