Commit 869dc152 authored by Rusty Russell's avatar Rusty Russell

ccan/io: simplify I/O callbacks.

Use a -1 for error codes.  This makes it easier to write your own io funcs.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 0f16a419
...@@ -151,14 +151,11 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts, ...@@ -151,14 +151,11 @@ bool io_timeout_(struct io_conn *conn, struct timespec ts,
} }
/* Returns true if we're finished. */ /* Returns true if we're finished. */
static bool do_write(int fd, struct io_plan *plan) static int do_write(int fd, struct io_plan *plan)
{ {
ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len); ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
if (ret < 0) { if (ret < 0)
/* Override next function to close us. */ return -1;
plan->next = io_close;
return true;
}
plan->u.write.buf += ret; plan->u.write.buf += ret;
plan->u.write.len -= ret; plan->u.write.len -= ret;
...@@ -184,14 +181,11 @@ struct io_plan io_write_(const void *data, size_t len, ...@@ -184,14 +181,11 @@ struct io_plan io_write_(const void *data, size_t len,
return plan; return plan;
} }
static bool do_read(int fd, struct io_plan *plan) static int do_read(int fd, struct io_plan *plan)
{ {
ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len); ssize_t ret = read(fd, plan->u.read.buf, plan->u.read.len);
if (ret <= 0) { if (ret <= 0)
/* Override next function to close us. */ return -1;
plan->next = io_close;
return true;
}
plan->u.read.buf += ret; plan->u.read.buf += ret;
plan->u.read.len -= ret; plan->u.read.len -= ret;
...@@ -217,17 +211,14 @@ struct io_plan io_read_(void *data, size_t len, ...@@ -217,17 +211,14 @@ struct io_plan io_read_(void *data, size_t len,
return plan; return plan;
} }
static bool do_read_partial(int fd, struct io_plan *plan) static int do_read_partial(int fd, struct io_plan *plan)
{ {
ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp); ssize_t ret = read(fd, plan->u.readpart.buf, *plan->u.readpart.lenp);
if (ret <= 0) { if (ret <= 0)
/* Override next function to close us. */ return -1;
plan->next = io_close;
return true;
}
*plan->u.readpart.lenp = ret; *plan->u.readpart.lenp = ret;
return true; return 1;
} }
/* Queue a partial request to read into a buffer. */ /* Queue a partial request to read into a buffer. */
...@@ -249,17 +240,14 @@ struct io_plan io_read_partial_(void *data, size_t *len, ...@@ -249,17 +240,14 @@ struct io_plan io_read_partial_(void *data, size_t *len,
return plan; return plan;
} }
static bool do_write_partial(int fd, struct io_plan *plan) static int do_write_partial(int fd, struct io_plan *plan)
{ {
ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp); ssize_t ret = write(fd, plan->u.writepart.buf, *plan->u.writepart.lenp);
if (ret < 0) { if (ret < 0)
/* Override next function to close us. */ return -1;
plan->next = io_close;
return true;
}
*plan->u.writepart.lenp = ret; *plan->u.writepart.lenp = ret;
return true; return 1;
} }
/* Queue a partial write request. */ /* Queue a partial write request. */
...@@ -310,7 +298,16 @@ void io_wake_(struct io_conn *conn, struct io_plan plan) ...@@ -310,7 +298,16 @@ void io_wake_(struct io_conn *conn, struct io_plan plan)
void io_ready(struct io_conn *conn) void io_ready(struct io_conn *conn)
{ {
if (conn->plan.io(conn->fd.fd, &conn->plan)) { switch (conn->plan.io(conn->fd.fd, &conn->plan)) {
case -1: /* Failure means a new plan: close up. */
set_current(conn);
conn->plan = io_close(NULL, NULL);
backend_plan_changed(conn);
set_current(NULL);
break;
case 0: /* Keep going with plan. */
break;
case 1: /* Done: get next plan. */
set_current(conn); set_current(conn);
if (timeout_active(conn)) if (timeout_active(conn))
backend_del_timeout(conn); backend_del_timeout(conn);
......
...@@ -16,7 +16,7 @@ struct io_conn; ...@@ -16,7 +16,7 @@ struct io_conn;
struct io_plan { struct io_plan {
int pollflag; int pollflag;
/* Only NULL if idle. */ /* Only NULL if idle. */
bool (*io)(int fd, struct io_plan *plan); int (*io)(int fd, struct io_plan *plan);
/* Only NULL if closing. */ /* Only NULL if closing. */
struct io_plan (*next)(struct io_conn *, void *arg); struct io_plan (*next)(struct io_conn *, void *arg);
void *next_arg; void *next_arg;
......
...@@ -23,7 +23,7 @@ static void finish_ok(struct io_conn *conn, struct packet *pkt) ...@@ -23,7 +23,7 @@ static void finish_ok(struct io_conn *conn, struct packet *pkt)
io_break(pkt, io_idle()); io_break(pkt, io_idle());
} }
static bool do_read_packet(int fd, struct io_plan *plan) static int do_read_packet(int fd, struct io_plan *plan)
{ {
struct packet *pkt = plan->u.ptr_len.p; struct packet *pkt = plan->u.ptr_len.p;
char *dest; char *dest;
...@@ -41,7 +41,7 @@ static bool do_read_packet(int fd, struct io_plan *plan) ...@@ -41,7 +41,7 @@ static bool do_read_packet(int fd, struct io_plan *plan)
ok1(pkt->state == 2); ok1(pkt->state == 2);
pkt->state++; pkt->state++;
if (pkt->len == 0) if (pkt->len == 0)
return true; return 1;
if (!pkt->contents && !(pkt->contents = malloc(pkt->len))) if (!pkt->contents && !(pkt->contents = malloc(pkt->len)))
goto fail; goto fail;
else { else {
...@@ -63,9 +63,7 @@ static bool do_read_packet(int fd, struct io_plan *plan) ...@@ -63,9 +63,7 @@ static bool do_read_packet(int fd, struct io_plan *plan)
fail: fail:
free(pkt->contents); free(pkt->contents);
/* Override next function to close us. */ return -1;
plan->next = io_close;
return true;
} }
static struct io_plan io_read_packet(struct packet *pkt, static struct io_plan io_read_packet(struct packet *pkt,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment