Try to parse complete messages from the plugin's buffer. */
| 695 | |
| 696 | /* Try to parse complete messages from the plugin's buffer. */ |
| 697 | static struct io_plan *plugin_read_json(struct io_conn *conn, |
| 698 | struct plugin *plugin) |
| 699 | { |
| 700 | struct wallet *wallet = plugin->plugins->ld->wallet; |
| 701 | const char *new_bytes, *buffer; |
| 702 | const jsmntok_t *toks; |
| 703 | size_t new_bytes_len; |
| 704 | size_t num_responses = 0; |
| 705 | /* wallet is NULL in really early code */ |
| 706 | bool want_transaction = (plugin->plugins->want_db_transaction |
| 707 | && wallet != NULL); |
| 708 | |
| 709 | new_bytes = jsonrpc_newly_read(plugin->json_in, &new_bytes_len); |
| 710 | if (new_bytes_len) { |
| 711 | log_io(plugin->log, LOG_IO_IN, NULL, "", |
| 712 | new_bytes, new_bytes_len); |
| 713 | } |
| 714 | |
| 715 | /* Parse until we get incomplete JSON */ |
| 716 | for (;;) { |
| 717 | const char *error; |
| 718 | const jsmntok_t *idtok; |
| 719 | struct plugin_destroyed *pd; |
| 720 | |
| 721 | error = jsonrpc_io_parse(tmpctx, plugin->json_in, |
| 722 | &toks, &buffer); |
| 723 | if (error) { |
| 724 | plugin_kill(plugin, LOG_UNUSUAL, "%s", error); |
| 725 | /* plugin_kill frees plugin */ |
| 726 | return io_close(NULL); |
| 727 | } |
| 728 | /* Incomplete? */ |
| 729 | if (!toks) |
| 730 | break; |
| 731 | |
| 732 | idtok = json_get_member(buffer, toks, "id"); |
| 733 | |
| 734 | /* We can be called extremely early, or as db hook, or for |
| 735 | * fake "terminated" request. */ |
| 736 | if (want_transaction) |
| 737 | db_begin_transaction(wallet->db); |
| 738 | |
| 739 | pd = plugin_detect_destruction(plugin); |
| 740 | if (!idtok) { |
| 741 | /* A Notification is a Request object without an "id" |
| 742 | * member. A Request object that is a Notification |
| 743 | * signifies the Client's lack of interest in the |
| 744 | * corresponding Response object, and as such no |
| 745 | * Response object needs to be returned to the |
| 746 | * client. The Server MUST NOT reply to a |
| 747 | * Notification, including those that are within a |
| 748 | * batch request. |
| 749 | * |
| 750 | * https://www.jsonrpc.org/specification#notification |
| 751 | */ |
| 752 | error = plugin_notification_handle(plugin, buffer, toks); |
| 753 | } else { |
| 754 | /* When a rpc call is made, the Server MUST reply with |
no test coverage detected