@@ -88,10 +88,7 @@ struct plugin {
8888 const char * * beglist ;
8989
9090 /* To read from lightningd */
91- char * buffer ;
92- size_t used , len_read ;
93- jsmn_parser parser ;
94- jsmntok_t * toks ;
91+ struct jsonrpc_io * lightningd_in ;
9592
9693 /* To write to lightningd */
9794 struct list_head js_list ;
@@ -2222,65 +2219,31 @@ static void ld_command_handle(struct plugin *plugin,
22222219 plugin_err (plugin , "Unknown command '%s'" , cmd -> methodname );
22232220}
22242221
2225- /**
2226- * Try to parse a complete message from lightningd's buffer, and return true
2227- * if we could handle it.
2228- */
2229- static bool ld_read_json_one (struct plugin * plugin )
2222+ static struct io_plan * ld_read_json (struct io_conn * conn ,
2223+ struct plugin * plugin )
22302224{
2231- bool complete ;
2225+ /* Gather an parse any new bytes */
2226+ for (;;) {
2227+ const jsmntok_t * toks ;
2228+ const char * buf ;
2229+ const char * err ;
22322230
2233- if (!json_parse_input (& plugin -> parser , & plugin -> toks ,
2234- plugin -> buffer , plugin -> used ,
2235- & complete )) {
2236- plugin_err (plugin , "Failed to parse JSON response '%.*s'" ,
2237- (int )plugin -> used , plugin -> buffer );
2238- return false;
2239- }
2231+ err = jsonrpc_io_parse (tmpctx ,
2232+ plugin -> lightningd_in ,
2233+ & toks , & buf );
2234+ if (err )
2235+ plugin_err (plugin , "%s" , err );
22402236
2241- if (!complete ) {
2242- /* We need more. */
2243- return false;
2244- }
2237+ if (!toks )
2238+ break ;
22452239
2246- /* Empty buffer? (eg. just whitespace). */
2247- if (tal_count (plugin -> toks ) == 1 ) {
2248- toks_reset (plugin -> toks );
2249- jsmn_init (& plugin -> parser );
2250- plugin -> used = 0 ;
2251- return false;
2240+ ld_command_handle (plugin , buf , toks );
2241+ jsonrpc_io_parse_done (plugin -> lightningd_in );
22522242 }
22532243
2254- /* FIXME: Spark doesn't create proper jsonrpc 2.0! So we don't
2255- * check for "jsonrpc" here. */
2256- ld_command_handle (plugin , plugin -> buffer , plugin -> toks );
2257-
2258- /* Move this object out of the buffer */
2259- memmove (plugin -> buffer , plugin -> buffer + plugin -> toks [0 ].end ,
2260- tal_count (plugin -> buffer ) - plugin -> toks [0 ].end );
2261- plugin -> used -= plugin -> toks [0 ].end ;
2262- toks_reset (plugin -> toks );
2263- jsmn_init (& plugin -> parser );
2264-
2265- return true;
2266- }
2267-
2268- static struct io_plan * ld_read_json (struct io_conn * conn ,
2269- struct plugin * plugin )
2270- {
2271- plugin -> used += plugin -> len_read ;
2272- if (plugin -> used && plugin -> used == tal_count (plugin -> buffer ))
2273- tal_resize (& plugin -> buffer , plugin -> used * 2 );
2274-
2275- /* Read and process all messages from the connection */
2276- while (ld_read_json_one (plugin ))
2277- ;
2278-
2279- /* Now read more from the connection */
2280- return io_read_partial (plugin -> stdin_conn ,
2281- plugin -> buffer + plugin -> used ,
2282- tal_count (plugin -> buffer ) - plugin -> used ,
2283- & plugin -> len_read , ld_read_json , plugin );
2244+ /* Read more */
2245+ return jsonrpc_io_read (conn , plugin -> lightningd_in ,
2246+ ld_read_json , plugin );
22842247}
22852248
22862249static struct io_plan * ld_write_json (struct io_conn * conn ,
@@ -2326,9 +2289,7 @@ static struct io_plan *stdin_conn_init(struct io_conn *conn,
23262289{
23272290 plugin -> stdin_conn = conn ;
23282291 io_set_finish (conn , ld_conn_finish , plugin );
2329- return io_read_partial (plugin -> stdin_conn , plugin -> buffer ,
2330- tal_bytelen (plugin -> buffer ), & plugin -> len_read ,
2331- ld_read_json , plugin );
2292+ return ld_read_json (conn , plugin );
23322293}
23332294
23342295/* lightningd reads from our stdout */
@@ -2369,12 +2330,8 @@ static struct plugin *new_plugin(const tal_t *ctx,
23692330 p -> id = name ;
23702331 p -> developer = developer ;
23712332 p -> deprecated_ok_override = NULL ;
2372- p -> buffer = tal_arr ( p , char , 64 );
2333+ p -> lightningd_in = jsonrpc_io_new ( p );
23732334 list_head_init (& p -> js_list );
2374- p -> used = 0 ;
2375- p -> len_read = 0 ;
2376- jsmn_init (& p -> parser );
2377- p -> toks = toks_alloc (p );
23782335 /* Async RPC */
23792336 p -> jsonrpc_in = jsonrpc_io_new (p );
23802337 list_head_init (& p -> rpc_js_list );
0 commit comments