if (!committed) {
//Socket.send(socket, Constants.ACK_BYTES, 0, Constants.ACK_BYTES.length) < 0
ByteBuffer buf = ByteBuffer.wrap(Constants.ACK_BYTES,0,Constants.ACK_BYTES.length);
- writeToSocket(buf,false,true);
+ writeToSocket(buf,true);
}
}
* @throws IOException
* @todo Fix non blocking write properly
*/
- private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean flip, boolean block) throws IOException {
- //int limit = bytebuffer.position();
- if ( flip ) bytebuffer.flip();
+ private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block) throws IOException {
+ if (socket.getBufHandler().getWriteBuffer() != bytebuffer) {
+ socket.getBufHandler().getWriteBuffer().put(bytebuffer);
+ bytebuffer = socket.getBufHandler().getWriteBuffer();
+ }
+
int written = 0;
NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
if ( att == null ) throw new IOException("Key must be cancelled");
}finally {
if ( selector != null ) getSelectorPool().put(selector);
}
- if ( block ) socket.getBufHandler().getWriteBuffer().clear(); //only clear
+ if ( block ) bytebuffer.clear(); //only clear
this.total = 0;
return written;
}
//write to the socket, if there is anything to write
if (socket.getBufHandler().getWriteBuffer().position() > 0) {
- writeToSocket(socket.getBufHandler().getWriteBuffer(),true,true);
+ socket.getBufHandler().getWriteBuffer().flip();
+ writeToSocket(socket.getBufHandler().getWriteBuffer(),true);
}
}
public void add(final KeyAttachment key, final int ops) {
Runnable r = new Runnable() {
public void run() {
- SocketChannel ch = key.getChannel().getIOChannel();
+ if ( key == null ) return;
+ NioChannel nch = key.getChannel();
+ if ( nch == null ) return;
+ SocketChannel ch = nch.getIOChannel();
+ if ( ch == null ) return;
SelectionKey sk = ch.keyFor(selector);
try {
if (sk == null) {
public void remove(final KeyAttachment key, final int ops) {
Runnable r = new Runnable() {
public void run() {
- SocketChannel ch = key.getChannel().getIOChannel();
+ if ( key == null ) return;
+ NioChannel nch = key.getChannel();
+ if ( nch == null ) return;
+ SocketChannel ch = nch.getIOChannel();
+ if ( ch == null ) return;
SelectionKey sk = ch.keyFor(selector);
try {
if (sk == null) {
public int write(ByteBuffer buf, NioChannel socket, Selector selector,
long writeTimeout, boolean block,MutableInteger lastWrite) throws IOException {
- if (socket.getBufHandler().getWriteBuffer() != buf) {
- socket.getBufHandler().getWriteBuffer().put(buf);
- buf = socket.getBufHandler().getWriteBuffer();
- }
if ( SHARED && block ) {
return blockingSelector.write(buf,socket,writeTimeout,lastWrite);
}