Skip to content

Commit

Permalink
Timeout During Disconnect Filter / Better NP Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Dec 15, 2023
1 parent 1fd98e8 commit 8ea8e3a
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/main/java/com/konloch/vortex/IO.java
Original file line number Diff line number Diff line change
@@ -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.*;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -148,7 +168,7 @@ public void run()

try
{
client.getSocket().close();
clientSC.close();
}
catch (Exception ex)
{
Expand Down

0 comments on commit 8ea8e3a

Please sign in to comment.