X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fjagi%2Fscgi%2FEventServer.java;h=f617cf54f5c7970aaf6b9263ef504b4f23da415f;hb=72715a9fadefd64bf37dca1977ebc3dd6d1f8c43;hp=1a6bc2a42eb68599c93003639332691680b5c4b1;hpb=a1480d6f1457fab85b52f3caab83baa527ae0571;p=jagi.git diff --git a/src/jagi/scgi/EventServer.java b/src/jagi/scgi/EventServer.java index 1a6bc2a..f617cf5 100644 --- a/src/jagi/scgi/EventServer.java +++ b/src/jagi/scgi/EventServer.java @@ -21,11 +21,6 @@ public class EventServer implements Runnable { tgt -> new Thread(tgt, "Request handler thread")); public EventServer(ServerSocketChannel sk, Function handler) { - try { - sk.configureBlocking(false); - } catch(IOException e) { - throw(new RuntimeException(e)); - } this.sk = sk; this.handler = handler; } @@ -120,11 +115,14 @@ public class EventServer implements Runnable { public void handle(int events) throws IOException { if(!eof && (buf.remaining() == 0)) { - buf.rewind(); + buf.clear(); while(buf.remaining() > 0) { - if(in.read(buf) < 0) + if(in.read(buf) < 0) { + eof = true; break; + } } + buf.flip(); } double now = Driver.current().time(); if((events & SelectionKey.OP_WRITE) != 0) { @@ -173,7 +171,7 @@ public class EventServer implements Runnable { public void handle(int events) throws IOException { double now = Driver.current().time(); if((events & SelectionKey.OP_READ) != 0) { - buf.rewind(); + buf.clear(); if(buf.remaining() > max - cur) buf.limit(buf.position() + (int)Math.min(max - cur, Integer.MAX_VALUE)); int rv = sk.read(buf); @@ -181,7 +179,8 @@ public class EventServer implements Runnable { eof = true; } else if(rv > 0) { lastread = now; - cur += rv; + if((cur += rv) >= max) + eof = true; } buf.flip(); while(buf.remaining() > 0) @@ -339,6 +338,7 @@ public class EventServer implements Runnable { int headlen = 0; ByteBuffer head = null; Map env = null; + Request req = null; Client(SocketChannel sk) { this.sk = sk; @@ -406,8 +406,7 @@ public class EventServer implements Runnable { if((events & SelectionKey.OP_READ) != 0) { if((env == null) && !readhead()) return; - Request req = new Request(env, sk); - submit(() -> EventServer.this.handle(req, handler)); + req = new Request(env, sk); handoff = true; } if(Driver.current().time() > (lastread + timeout)) @@ -415,6 +414,8 @@ public class EventServer implements Runnable { } public void close() { + if(req != null) + submit(() -> EventServer.this.handle(req, handler)); if(!handoff) { try { sk.close(); @@ -431,11 +432,8 @@ public class EventServer implements Runnable { public int events() {return(SelectionKey.OP_ACCEPT);} public void handle(int events) throws IOException { - if((events & SelectionKey.OP_ACCEPT) != 0) { - SocketChannel cl = sk.accept(); - cl.configureBlocking(false); - Driver.current().add(new Client(cl)); - } + if((events & SelectionKey.OP_ACCEPT) != 0) + Driver.current().add(new Client(sk.accept())); } public void close() {