| 705 | } |
| 706 | |
| 707 | static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks) |
| 708 | { |
| 709 | const jsmntok_t *idtok, *contenttok; |
| 710 | struct out_req *out; |
| 711 | struct command_result *res; |
| 712 | const char *buf = plugin->rpc_buffer + plugin->rpc_read_offset; |
| 713 | |
| 714 | idtok = json_get_member(buf, toks, "id"); |
| 715 | if (!idtok) |
| 716 | /* FIXME: Don't simply ignore notifications! */ |
| 717 | return; |
| 718 | |
| 719 | out = strmap_getn(&plugin->out_reqs, |
| 720 | buf + idtok->start, |
| 721 | idtok->end - idtok->start); |
| 722 | if (!out) { |
| 723 | /* This can actually happen, if they free req! */ |
| 724 | plugin_log(plugin, LOG_DBG, "JSON reply with unknown id '%.*s'", |
| 725 | json_tok_full_len(toks), |
| 726 | json_tok_full(buf, toks)); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | /* Remove destructor if one existed */ |
| 731 | if (out->cmd) |
| 732 | tal_del_destructor2(out->cmd, disable_request_cb, out); |
| 733 | |
| 734 | /* We want to free this if callback doesn't. */ |
| 735 | tal_steal(tmpctx, out); |
| 736 | |
| 737 | contenttok = json_get_member(buf, toks, "error"); |
| 738 | if (contenttok) { |
| 739 | if (out->errcb) |
| 740 | res = out->errcb(out->cmd, buf, contenttok, out->arg); |
| 741 | else |
| 742 | res = out->cb(out->cmd, buf, toks, out->arg); |
| 743 | } else { |
| 744 | contenttok = json_get_member(buf, toks, "result"); |
| 745 | if (!contenttok) |
| 746 | plugin_err(plugin, "Bad JSONRPC, no 'error' nor 'result': '%.*s'", |
| 747 | json_tok_full_len(toks), |
| 748 | json_tok_full(buf, toks)); |
| 749 | /* errcb is NULL if it's a single whole-object callback */ |
| 750 | if (out->errcb) |
| 751 | res = out->cb(out->cmd, buf, contenttok, out->arg); |
| 752 | else |
| 753 | res = out->cb(out->cmd, buf, toks, out->arg); |
| 754 | } |
| 755 | |
| 756 | assert(res == &pending || res == &complete); |
| 757 | } |
| 758 | |
| 759 | struct command_result * |
| 760 | send_outreq(struct plugin *plugin, const struct out_req *req) |
no test coverage detected