Fix a bug where channelActive is not fired for an accepted channel
This commit is contained in:
parent
3e2953cf92
commit
5caf78acc0
@ -18,10 +18,7 @@ package io.netty.channel.socket.aio;
|
|||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ChannelBufType;
|
import io.netty.buffer.ChannelBufType;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.ChannelPipeline;
|
import io.netty.channel.ChannelPipeline;
|
||||||
import io.netty.channel.ChannelStateHandler;
|
|
||||||
import io.netty.channel.ChannelStateHandlerAdapter;
|
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
|
||||||
import java.io.IOException;
|
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<Void, AioSocketChannel> CONNECT_HANDLER = new ConnectHandler();
|
||||||
private static final CompletionHandler<Integer, AioSocketChannel> READ_HANDLER = new ReadHandler();
|
private static final CompletionHandler<Integer, AioSocketChannel> READ_HANDLER = new ReadHandler();
|
||||||
private static final CompletionHandler<Integer, AioSocketChannel> WRITE_HANDLER = new WriteHandler();
|
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 final AtomicBoolean flushing = new AtomicBoolean(false);
|
||||||
private volatile AioSocketChannelConfig config;
|
private volatile AioSocketChannelConfig config;
|
||||||
|
|
||||||
@ -65,10 +45,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
|
|||||||
|
|
||||||
public AioSocketChannel(AioServerSocketChannel parent, Integer id, AsynchronousSocketChannel channel) {
|
public AioSocketChannel(AioServerSocketChannel parent, Integer id, AsynchronousSocketChannel channel) {
|
||||||
super(parent, id);
|
super(parent, id);
|
||||||
this.ch = channel;
|
ch = channel;
|
||||||
if (ch != null) {
|
if (ch != null) {
|
||||||
config = new AioSocketChannelConfig(javaChannel());
|
config = new AioSocketChannelConfig(javaChannel());
|
||||||
pipeline().addLast(READ_START_HANDLER);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,10 +108,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
|
|||||||
if (ch == null) {
|
if (ch == null) {
|
||||||
ch = AsynchronousSocketChannel.open(AsynchronousChannelGroup.withThreadPool(eventLoop()));
|
ch = AsynchronousSocketChannel.open(AsynchronousChannelGroup.withThreadPool(eventLoop()));
|
||||||
config = new AioSocketChannelConfig(javaChannel());
|
config = new AioSocketChannelConfig(javaChannel());
|
||||||
pipeline().addLast(READ_START_HANDLER);
|
} else if (remoteAddress() != null) {
|
||||||
|
read();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +168,8 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
|
|||||||
// a PendingWriteException will be thrown. So use CAS to not run
|
// a PendingWriteException will be thrown. So use CAS to not run
|
||||||
// into this
|
// into this
|
||||||
if (flushing.compareAndSet(false, true)) {
|
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);
|
javaChannel().write(buffer, this, WRITE_HANDLER);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -245,10 +224,9 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
|
|||||||
boolean closed = false;
|
boolean closed = false;
|
||||||
boolean read = false;
|
boolean read = false;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
int localReadAmount = result.intValue();
|
int localReadAmount = result.intValue();
|
||||||
if (localReadAmount > 0) {
|
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.
|
// current writerIndex + read amount of bytes.
|
||||||
//
|
//
|
||||||
// This is needed as the ByteBuffer and the ByteBuf does not share
|
// This is needed as the ByteBuffer and the ByteBuf does not share
|
||||||
@ -301,6 +279,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
|
|||||||
@Override
|
@Override
|
||||||
public void completed(Void result, AioSocketChannel channel) {
|
public void completed(Void result, AioSocketChannel channel) {
|
||||||
((AsyncUnsafe) channel.unsafe()).connectSuccess();
|
((AsyncUnsafe) channel.unsafe()).connectSuccess();
|
||||||
|
channel.pipeline().fireChannelActive();
|
||||||
|
|
||||||
// start reading from channel
|
// start reading from channel
|
||||||
channel.read();
|
channel.read();
|
||||||
|
Loading…
Reference in New Issue
Block a user