]> git.pond.sub.org Git - empserver/blobdiff - src/lib/empthread/io.c
Fix server shutdown to let player output drain properly
[empserver] / src / lib / empthread / io.c
index ef285a74c4a67f1104b8339e9e70a83e43527a88..47a2b1093e4de2a9cdb188131270077a7e4e276a 100644 (file)
@@ -108,7 +108,16 @@ io_open(int fd, int flags, int bufsize, struct timeval timeout)
 void
 io_close(struct iop *iop)
 {
-
+    char buf[IO_BUFSIZE];
+    int ret;
+
+    while (io_output(iop, 1) > 0) ;
+    shutdown(iop->fd, SHUT_WR);
+    while (empth_select(iop->fd, EMPTH_FD_READ, &iop->input_timeout) > 0) {
+       ret = read(iop->fd, buf, sizeof(buf));
+       if (ret <= 0)
+           break;
+    }
     if (iop->input)
        ioq_destroy(iop->input);
     if (iop->output)
@@ -201,15 +210,15 @@ io_output(struct iop *iop, int wait)
     if (wait)
        ef_make_stale();
 
-    if (!ioq_qsize(iop->output))
-       return 0;
-
     if ((iop->flags & IO_WRITE) == 0)
        return -1;
 
     if (iop->flags & IO_ERROR)
        return -1;
 
+    if (!ioq_qsize(iop->output))
+       return 0;
+
     if (wait) {
        res = empth_select(iop->fd, EMPTH_FD_WRITE, NULL);
        if (res == 0)