public int write(ByteBuffer buf, NioChannel socket, long writeTimeout,MutableInteger lastWrite) throws IOException {
SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
if ( key == null ) throw new IOException("Key no longer registered");
+ KeyReference reference = new KeyReference();
KeyAttachment att = (KeyAttachment) key.attachment();
int written = 0;
boolean timedout = false;
}
try {
if ( att.getWriteLatch()==null || att.getWriteLatch().getCount()==0) att.startWriteLatch(1);
- poller.add(att,SelectionKey.OP_WRITE);
+ poller.add(att,SelectionKey.OP_WRITE,reference);
att.awaitWriteLatch(writeTimeout,TimeUnit.MILLISECONDS);
}catch (InterruptedException ignore) {
Thread.interrupted();
throw new SocketTimeoutException();
} finally {
poller.remove(att,SelectionKey.OP_WRITE);
- if (timedout && key != null) {
- poller.cancelKey(socket, key);
+ if (timedout && reference.key!=null) {
+ poller.cancelKey(reference.key);
}
+ reference.key = null;
}
return written;
}
public int read(ByteBuffer buf, NioChannel socket, long readTimeout) throws IOException {
SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
if ( key == null ) throw new IOException("Key no longer registered");
+ KeyReference reference = new KeyReference();
KeyAttachment att = (KeyAttachment) key.attachment();
int read = 0;
boolean timedout = false;
}
try {
if ( att.getReadLatch()==null || att.getReadLatch().getCount()==0) att.startReadLatch(1);
- poller.add(att,SelectionKey.OP_READ);
+ poller.add(att,SelectionKey.OP_READ, reference);
att.awaitReadLatch(readTimeout,TimeUnit.MILLISECONDS);
}catch (InterruptedException ignore) {
Thread.interrupted();
throw new SocketTimeoutException();
} finally {
poller.remove(att,SelectionKey.OP_READ);
- if (timedout && key != null) {
- poller.cancelKey(socket,key);
+ if (timedout && reference.key!=null) {
+ poller.cancelKey(reference.key);
}
+ reference.key = null;
}
return read;
}
protected ConcurrentLinkedQueue<Runnable> events = new ConcurrentLinkedQueue<Runnable>();
public void disable() { run = false; selector.wakeup();}
protected AtomicInteger wakeupCounter = new AtomicInteger(0);
- public void cancelKey(final NioChannel socket, final SelectionKey key) {
+ public void cancelKey(final SelectionKey key) {
Runnable r = new Runnable() {
public void run() {
key.cancel();
}
}
- public void add(final KeyAttachment key, final int ops) {
+ public void add(final KeyAttachment key, final int ops, final KeyReference ref) {
Runnable r = new Runnable() {
public void run() {
if ( key == null ) return;
try {
if (sk == null) {
sk = ch.register(selector, ops, key);
+ ref.key = sk;
} else if (!sk.isValid()) {
cancel(sk,key,ops);
} else {
}catch( Exception ignore ) {
if (log.isDebugEnabled())log.debug("",ignore);
}
+ try {
+ selector.close();//Close the connector
+ }catch( Exception ignore ) {
+ if (log.isDebugEnabled())log.debug("",ignore);
+ }
}
public void countDown(CountDownLatch latch) {
if ( latch == null ) return;
latch.countDown();
}
+ }
+
+ public class KeyReference {
+ SelectionKey key = null;
-
-
+ @Override
+ public void finalize() {
+ if (key!=null && key.isValid()) {
+ log.warn("Possible key leak, cancelling key in the finalizer.");
+ try {key.cancel();}catch (Exception ignore){}
+ }
+ key = null;
+ }
}
}
\ No newline at end of file