From 1ee6412bc01b15aa8fad07a8bbce694ca099a8cb Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Fri, 18 Feb 2022 04:56:57 +0100 Subject: [PATCH] Configure channel blocking as part of adding to the event-loop. --- src/jagi/event/Driver.java | 5 +++++ src/jagi/scgi/EventServer.java | 12 ++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/jagi/event/Driver.java b/src/jagi/event/Driver.java index 6ee920f..8fe83a7 100644 --- a/src/jagi/event/Driver.java +++ b/src/jagi/event/Driver.java @@ -184,6 +184,11 @@ public class Driver { void add(Watcher w, SelectableChannel ch) { if(watching.containsKey(w) || paused.containsKey(w) || timeheap.contains(w)) throw(new IllegalStateException(w + ": already registered")); + try { + ch.configureBlocking(false); + } catch(IOException e) { + throw(new RuntimeException(ch + ": could not make non-blocking", e)); + } int evs = w.events(); double timeout = w.timeout(); boolean hastime = timeout < Double.POSITIVE_INFINITY; diff --git a/src/jagi/scgi/EventServer.java b/src/jagi/scgi/EventServer.java index 1a6bc2a..2de2e86 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; } @@ -431,11 +426,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() { -- 2.11.0