static struct io_plan *write_json(struct io_conn *conn, struct json_connection *jcon) { struct json_output *out; out = list_pop(&jcon->output, struct json_output, list); if (!out) { if (jcon->stop) { log_unusual(jcon->log, "JSON-RPC shutdown"); /* Return us to toplevel lightningd.c */ io_break(jcon->dstate); return io_close(conn); } /* Reader can go again now. */ io_wake(jcon); return io_out_wait(conn, jcon, write_json, jcon); } jcon->outbuf = tal_steal(jcon, out->json); tal_free(out); log_io(jcon->log, false, jcon->outbuf, strlen(jcon->outbuf)); return io_write(conn, jcon->outbuf, strlen(jcon->outbuf), write_json, jcon); }
static void complete(struct pfiled *pfiled, struct io *io) { struct xseg_request *req = io->req; req->state |= XS_SERVED; if (cmdline_verbose) log_io("complete", io); xport p = xseg_respond(pfiled->xseg, req, pfiled->portno, X_ALLOC); xseg_signal(pfiled->xseg, p); __sync_fetch_and_sub(&pfiled->fdcache[io->fdcacheidx].ref, 1); }
static struct io_plan *read_json(struct io_conn *conn, struct json_connection *jcon) { jsmntok_t *toks; bool valid; log_io(jcon->log, true, jcon->buffer + jcon->used, jcon->len_read); /* Resize larger if we're full. */ jcon->used += jcon->len_read; if (jcon->used == tal_count(jcon->buffer)) tal_resize(&jcon->buffer, jcon->used * 2); again: toks = json_parse_input(jcon->buffer, jcon->used, &valid); if (!toks) { if (!valid) { log_unusual(jcon->dstate->base_log, "Invalid token in json input: '%.*s'", (int)jcon->used, jcon->buffer); return io_close(conn); } /* We need more. */ goto read_more; } /* Empty buffer? (eg. just whitespace). */ if (tal_count(toks) == 1) { jcon->used = 0; goto read_more; } parse_request(jcon, toks); /* Remove first {}. */ memmove(jcon->buffer, jcon->buffer + toks[0].end, tal_count(jcon->buffer) - toks[0].end); jcon->used -= toks[0].end; tal_free(toks); /* Need to wait for command to finish? */ if (jcon->current) { jcon->len_read = 0; return io_wait(conn, jcon, read_json, jcon); } /* See if we can parse the rest. */ goto again; read_more: tal_free(toks); return io_read_partial(conn, jcon->buffer + jcon->used, tal_count(jcon->buffer) - jcon->used, &jcon->len_read, read_json, jcon); }
/* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */ static char fgets_and_trim(FILE *fp) { char c; char *buf_ptr; if (fgets(G.wget_buf, sizeof(G.wget_buf) - 1, fp) == NULL) bb_perror_msg_and_die("error getting response"); buf_ptr = strchrnul(G.wget_buf, '\n'); c = *buf_ptr; *buf_ptr = '\0'; buf_ptr = strchrnul(G.wget_buf, '\r'); *buf_ptr = '\0'; log_io("< %s", G.wget_buf); return c; }
static int ftpcmd(const char *s1, const char *s2, FILE *fp) { int result; if (s1) { if (!s2) s2 = ""; fprintf(fp, "%s%s\r\n", s1, s2); fflush(fp); log_io("> %s%s", s1, s2); } do { fgets_and_trim(fp); } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' '); G.wget_buf[3] = '\0'; result = xatoi_positive(G.wget_buf); G.wget_buf[3] = ' '; return result; }