summaryrefslogtreecommitdiffstats
path: root/client.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-07-30 16:32:12 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-07-30 16:32:12 +0000
commit5f13bb0c3ade513ec3f88af8b6c0f575fa00cc4b (patch)
treec5951ede730fd867a6b658479574472d8eb1bcfa /client.c
parent479d614884f66e9fa11f1292a2ef36991da46c1d (diff)
There aren't many client message types or code to handle them so get rid of the
lookup table and use a switch, merge the tiny handler functions into it, and move the whole lot to client.c. Also change client_msg_dispatch to consume as many messages as possible and move the call to it to the right place so it checks for signals afterwards. Prompted by suggestions from eric@.
Diffstat (limited to 'client.c')
-rw-r--r--client.c81
1 files changed, 65 insertions, 16 deletions
diff --git a/client.c b/client.c
index e8785359..cb2207dd 100644
--- a/client.c
+++ b/client.c
@@ -137,7 +137,6 @@ int
client_main(struct client_ctx *cctx)
{
struct pollfd pfd;
- int xtimeout; /* Yay for ncurses namespace! */
siginit();
@@ -158,25 +157,12 @@ client_main(struct client_ctx *cctx)
sigcont = 0;
}
- switch (client_msg_dispatch(cctx)) {
- case -1:
- goto out;
- case 0:
- /* May be more in buffer, don't let poll block. */
- xtimeout = 0;
- break;
- default:
- /* Out of data, poll may block. */
- xtimeout = INFTIM;
- break;
- }
-
pfd.fd = cctx->srv_fd;
pfd.events = POLLIN;
if (BUFFER_USED(cctx->srv_out) > 0)
pfd.events |= POLLOUT;
- if (poll(&pfd, 1, xtimeout) == -1) {
+ if (poll(&pfd, 1, INFTIM) == -1) {
if (errno == EAGAIN || errno == EINTR)
continue;
fatal("poll failed");
@@ -186,9 +172,11 @@ client_main(struct client_ctx *cctx)
cctx->exittype = CCTX_DIED;
break;
}
+
+ if (client_msg_dispatch(cctx) != 0)
+ break;
}
-out:
if (sigterm) {
printf("[terminated]\n");
return (1);
@@ -227,3 +215,64 @@ client_handle_winch(struct client_ctx *cctx)
sigwinch = 0;
}
+
+int
+client_msg_dispatch(struct client_ctx *cctx)
+{
+ struct hdr hdr;
+ struct msg_print_data printdata;
+
+ for (;;) {
+ if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
+ return (0);
+ memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
+ if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
+ return (0);
+ buffer_remove(cctx->srv_in, sizeof hdr);
+
+ switch (hdr.type) {
+ case MSG_DETACH:
+ if (hdr.size != 0)
+ fatalx("bad MSG_DETACH size");
+
+ client_write_server(cctx, MSG_EXITING, NULL, 0);
+ cctx->exittype = CCTX_DETACH;
+ break;
+ case MSG_ERROR:
+ if (hdr.size != sizeof printdata)
+ fatalx("bad MSG_PRINT size");
+ buffer_read(cctx->srv_in, &printdata, sizeof printdata);
+ printdata.msg[(sizeof printdata.msg) - 1] = '\0';
+
+ cctx->errstr = xstrdup(printdata.msg);
+ return (-1);
+ case MSG_EXIT:
+ if (hdr.size != 0)
+ fatalx("bad MSG_EXIT size");
+
+ client_write_server(cctx, MSG_EXITING, NULL, 0);
+ cctx->exittype = CCTX_EXIT;
+ break;
+ case MSG_EXITED:
+ if (hdr.size != 0)
+ fatalx("bad MSG_EXITED size");
+
+ return (-1);
+ case MSG_SHUTDOWN:
+ if (hdr.size != 0)
+ fatalx("bad MSG_SHUTDOWN size");
+
+ client_write_server(cctx, MSG_EXITING, NULL, 0);
+ cctx->exittype = CCTX_SHUTDOWN;
+ break;
+ case MSG_SUSPEND:
+ if (hdr.size != 0)
+ fatalx("bad MSG_SUSPEND size");
+
+ client_suspend();
+ break;
+ default:
+ fatalx("unexpected message");
+ }
+ }
+}