Skip to content

Commit

Permalink
Added cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lolzballs committed May 30, 2015
1 parent 06e6261 commit ebf90ed
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Map;

public class Socks5ClientTunnelChannelWrapper extends TunnelChannelWrapper {
public final HashMap<Integer, Socks5ConnectionData> connections;
Expand Down Expand Up @@ -39,4 +40,14 @@ public void startReading(int connectionId) {
connection.client.addInterest(SelectionKey.OP_READ);
connection.client.stopReading = false;
}

@Override
public void cleanup() {
for (Map.Entry<Integer, Socks5ConnectionData> entry : connections.entrySet()) {
ChannelWrapper wrapper = entry.getValue().client;
wrapper.disconnectListener = null;
wrapper.close();
}
connections.clear();
}
}
3 changes: 3 additions & 0 deletions Common/src/tk/jackyliao123/proxy/TunnelChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ public ByteBuffer popWriteBuffer() throws IOException {
public abstract void stopReading(int connectionId);

public abstract void startReading(int connectionId);

public abstract void cleanup();

}
7 changes: 6 additions & 1 deletion Common/src/tk/jackyliao123/proxy/event/EventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ public void process(long timeout) throws IOException {
} catch (Throwable t) {
Logger.error("Critical Error, Throwable caught");
Logger.error(t);
kill((ChannelWrapper) key.attachment());
ChannelWrapper wrapper = (ChannelWrapper) key.attachment();
if (wrapper instanceof TunnelChannelWrapper) {
((TunnelChannelWrapper) wrapper).cleanup();
}
kill(wrapper);
System.gc();
}

keys.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Map;

public class ServerTunnelChannelWrapper extends TunnelChannelWrapper {
public HashMap<Integer, ChannelWrapper> connections;
Expand Down Expand Up @@ -36,4 +37,14 @@ public void startReading(int connectionId) {
connection.addInterest(SelectionKey.OP_READ);
connection.stopReading = false;
}

@Override
public void cleanup() {
for (Map.Entry<Integer, ChannelWrapper> entry : connections.entrySet()) {
ChannelWrapper wrapper = entry.getValue();
wrapper.disconnectListener = null;
wrapper.close();
}
connections.clear();
}
}

0 comments on commit ebf90ed

Please sign in to comment.