libplugin: use jsonrpc_io logic for sync requests too.
It's a little overkill, but it's clear. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/jsonrpc_io.h>
|
||||
#include <common/utils.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define READ_CHUNKSIZE 64
|
||||
|
||||
@@ -126,3 +128,24 @@ struct io_plan *jsonrpc_io_read_(struct io_conn *conn,
|
||||
&json_in->bytes_read,
|
||||
next, arg);
|
||||
}
|
||||
|
||||
bool jsonrpc_sync_read(struct jsonrpc_io *json_in, int infd)
|
||||
{
|
||||
int r;
|
||||
|
||||
/* Make sure there's more room */
|
||||
membuf_prepare_space(&json_in->membuf, READ_CHUNKSIZE);
|
||||
|
||||
/* Try to read more. */
|
||||
r = read(infd,
|
||||
membuf_space(&json_in->membuf),
|
||||
membuf_num_space(&json_in->membuf));
|
||||
if (r < 0)
|
||||
return false;
|
||||
if (r == 0) {
|
||||
errno = 0;
|
||||
return false;
|
||||
}
|
||||
json_in->bytes_read = r;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,15 @@ struct io_plan *jsonrpc_io_read_(struct io_conn *conn,
|
||||
const char *jsonrpc_newly_read(struct jsonrpc_io *json_in,
|
||||
size_t *len);
|
||||
|
||||
/**
|
||||
* jsonrpc_sync_read: read from fd into buffer.
|
||||
* @json_in: buffer to read into.
|
||||
* @infd: file descriptort to read.
|
||||
*
|
||||
* Returns false on error or EOF; for EOF errno will be 0.
|
||||
*/
|
||||
bool jsonrpc_sync_read(struct jsonrpc_io *json_in, int infd);
|
||||
|
||||
/**
|
||||
* jsonrpc_io_parse: try to parse more of the buffer.
|
||||
* @ctx: context to allocate error message off.
|
||||
|
||||
Reference in New Issue
Block a user