Fix a bug where channelActive is not fired for an accepted channel

This commit is contained in:
Trustin Lee 2012-06-18 17:22:06 +09:00
parent 3e2953cf92
commit 5caf78acc0

View File

@ -18,10 +18,7 @@ package io.netty.channel.socket.aio;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ChannelBufType;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelStateHandler;
import io.netty.channel.ChannelStateHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import java.io.IOException;
@ -38,24 +35,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
private static final CompletionHandler<Void, AioSocketChannel> CONNECT_HANDLER = new ConnectHandler();
private static final CompletionHandler<Integer, AioSocketChannel> READ_HANDLER = new ReadHandler();
private static final CompletionHandler<Integer, AioSocketChannel> WRITE_HANDLER = new WriteHandler();
private static final ChannelStateHandler READ_START_HANDLER = new ChannelStateHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
try {
super.channelActive(ctx);
// once the channel is active, the first read is scheduled
((AioSocketChannel)ctx.channel()).read();
} finally {
ctx.pipeline().remove(this);
}
}
};
private final AtomicBoolean flushing = new AtomicBoolean(false);
private volatile AioSocketChannelConfig config;
@ -65,10 +45,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
public AioSocketChannel(AioServerSocketChannel parent, Integer id, AsynchronousSocketChannel channel) {
super(parent, id);
this.ch = channel;
ch = channel;
if (ch != null) {
config = new AioSocketChannelConfig(javaChannel());
pipeline().addLast(READ_START_HANDLER);
}
}
@ -129,10 +108,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
if (ch == null) {
ch = AsynchronousSocketChannel.open(AsynchronousChannelGroup.withThreadPool(eventLoop()));
config = new AioSocketChannelConfig(javaChannel());
pipeline().addLast(READ_START_HANDLER);
} else if (remoteAddress() != null) {
read();
}
return null;
}
@ -190,7 +168,8 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
// a PendingWriteException will be thrown. So use CAS to not run
// into this
if (flushing.compareAndSet(false, true)) {
ByteBuffer buffer = (ByteBuffer) buf.nioBuffer();
ByteBuffer buffer = buf.nioBuffer();
System.err.println("WRITE: " + buffer);
javaChannel().write(buffer, this, WRITE_HANDLER);
}
return false;
@ -245,10 +224,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
boolean closed = false;
boolean read = false;
try {
int localReadAmount = result.intValue();
if (localReadAmount > 0) {
//Set the writerIndex of the buffer correctly to the
// Set the writerIndex of the buffer correctly to the
// current writerIndex + read amount of bytes.
//
// This is needed as the ByteBuffer and the ByteBuf does not share
@ -301,6 +279,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
@Override
public void completed(Void result, AioSocketChannel channel) {
((AsyncUnsafe) channel.unsafe()).connectSuccess();
channel.pipeline().fireChannelActive();
// start reading from channel
channel.read();