From 7e4f410287ac2f9315c875df5f70e443b26695f6 Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Tue, 8 Mar 2016 11:17:21 -0600 Subject: [PATCH] Quickly closes client sockets which do not communicate in the format expected by the server. --- .../jdk8/MuxingServerSocketChannel.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java b/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java index b800976..39ae79b 100644 --- a/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java +++ b/src/main/java/org/ice4j/socket/jdk8/MuxingServerSocketChannel.java @@ -1037,7 +1037,6 @@ class MuxingServerSocketChannel } int read = maybeRead(ch, db.getByteBuffer()); - boolean remove = false; // Try to filter ch (into a MuxServerSocketChannel). if (ch.isOpen()) @@ -1050,20 +1049,37 @@ class MuxingServerSocketChannel db.timestamp = now; DatagramPacket p = db.getDatagramPacket(); + int len = p.getLength(); - if (p.getLength() > 0 && filterAccept(p, ch)) + if (len > 0) { - remove = true; + if (filterAccept(p, ch)) + { + // A MuxServerSocketChannel has accepted ch so this + // MuxingServerSocketChannel is no longer responsible for + // ch. + return true; + } + else if (len >= SOCKET_CHANNEL_READ_CAPACITY) + { + // This MuxingServerSocketChannel has read from ch as much + // as it will ever read and no existing + // MuxServerSocketChannel has accepted ch. There is no point + // in waiting anymore. + closeNoExceptions(ch); + // Allow this MuxingServerSocketChannel to clean ch up. + return false; + } } - else if (read <= 0 - && now - db.timestamp >= SOCKET_CHANNEL_READ_TIMEOUT) + + if (read <= 0 && now - db.timestamp >= SOCKET_CHANNEL_READ_TIMEOUT) { - // The SocketChannel appears to have been abandoned by the - // client. + // It appears ch has been abandoned by the client. closeNoExceptions(ch); + return false; } } - return remove; + return false; } }