diff --git a/src/main/java/com/konloch/vortex/IO.java b/src/main/java/com/konloch/vortex/IO.java index bdcc927..208e9f2 100644 --- a/src/main/java/com/konloch/vortex/IO.java +++ b/src/main/java/com/konloch/vortex/IO.java @@ -1,7 +1,9 @@ package com.konloch.vortex; import java.io.IOException; +import java.net.Socket; import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; import java.util.*; /** @@ -56,7 +58,28 @@ public void run() if(client == null) return true; - boolean remove = !client.getSocket().isConnected(); + final SocketChannel clientSC = client.getSocket(); + + if(clientSC == null) + return true; + + boolean remove = !clientSC.isConnected(); + + //timeout if there is no network activity + if (Math.min(now - client.getLastNetworkActivityWrite(), + now - client.getLastNetworkActivityRead()) > server.getTimeout()) + { + remove = true; + + try + { + clientSC.close(); + } + catch (IOException e) + { + //ignore + } + } if (remove && server.getOnDisconnect() != null) server.getOnDisconnect().run(client); @@ -70,22 +93,19 @@ public void run() if(client == null) continue; + final SocketChannel clientSC = client.getSocket(); + + if(clientSC == null) + continue; + try { //if the client has been disconnected, do not try to process anything - if (!client.getSocket().isConnected()) + if (!clientSC.isConnected()) continue; - - //timeout if there is no network activity - if (Math.min(now - client.getLastNetworkActivityWrite(), - now - client.getLastNetworkActivityRead()) > server.getTimeout()) - { - client.getSocket().close(); - continue; - } //process reading (always in the reading state unless disconnected) - client.getSocket().read(buffer); + clientSC.read(buffer); if (buffer.position() > 0) { @@ -130,7 +150,7 @@ public void run() client.resetLastNetworkActivityWrite(); //sent the buffer - client.getSocket().write(buffer); + clientSC.write(buffer); //clear the buffer ((java.nio.Buffer) buffer).clear(); @@ -148,7 +168,7 @@ public void run() try { - client.getSocket().close(); + clientSC.close(); } catch (Exception ex) {