Refactor AIO Transport to allow to use Bootstrap without the ugly hack

This commit is contained in:
Norman Maurer 2012-12-14 09:51:36 +01:00
parent 02a6e85feb
commit 96dc3f983f
4 changed files with 19 additions and 29 deletions

View File

@ -30,8 +30,6 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.aio.AioEventLoopGroup;
import io.netty.channel.socket.aio.AioServerSocketChannel;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.AttributeKey;
@ -101,9 +99,6 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
throw new IllegalArgumentException(
"channelClass must be subtype of " + ServerChannel.class.getSimpleName() + '.');
}
if (channelClass == AioServerSocketChannel.class) {
return channelFactory(new AioServerSocketChannelFactory());
}
return super.channel(channelClass);
}
@ -300,17 +295,5 @@ public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap> {
return buf.toString();
}
private final class AioServerSocketChannelFactory implements ChannelFactory {
@Override
public Channel newChannel() {
return new AioServerSocketChannel((AioEventLoopGroup) group(), (AioEventLoopGroup) childGroup);
}
@Override
public String toString() {
return AioServerSocketChannel.class.getSimpleName() + ".class";
}
}
}

View File

@ -40,7 +40,6 @@ public class AioServerSocketChannel extends AbstractAioChannel implements Server
private static final InternalLogger logger =
InternalLoggerFactory.getInstance(AioServerSocketChannel.class);
private final AioEventLoopGroup childGroup;
private final AioServerSocketChannelConfig config;
private boolean closed;
private final AtomicBoolean readSuspended = new AtomicBoolean();
@ -61,15 +60,14 @@ public class AioServerSocketChannel extends AbstractAioChannel implements Server
}
}
public AioServerSocketChannel(AioEventLoopGroup group) {
this(group, group);
public AioServerSocketChannel() {
super(null, null, null);
config = new AioServerSocketChannelConfig();
}
public AioServerSocketChannel(AioEventLoopGroup parentGroup, AioEventLoopGroup childGroup) {
super(null, null, newSocket(parentGroup.group));
this.childGroup = childGroup;
config = new AioServerSocketChannelConfig(javaChannel());
public AioServerSocketChannel(AsynchronousServerSocketChannel channel) {
super(null, null, channel);
config = new AioServerSocketChannelConfig(channel);
}
@Override
@ -79,7 +77,7 @@ public class AioServerSocketChannel extends AbstractAioChannel implements Server
@Override
public boolean isActive() {
return javaChannel().isOpen() && localAddress0() != null;
return ch != null && javaChannel().isOpen() && localAddress0() != null;
}
@Override
@ -141,7 +139,13 @@ public class AioServerSocketChannel extends AbstractAioChannel implements Server
@Override
protected Runnable doRegister() throws Exception {
return super.doRegister();
Runnable task = super.doRegister();
if (ch == null) {
AsynchronousServerSocketChannel channel = newSocket(((AioEventLoopGroup) eventLoop().parent()).group);
ch = channel;
config.channel = channel;
}
return task;
}
private static final class AcceptHandler

View File

@ -33,13 +33,16 @@ import java.util.Map;
final class AioServerSocketChannelConfig extends DefaultChannelConfig
implements ServerSocketChannelConfig {
private final AsynchronousServerSocketChannel channel;
volatile AsynchronousServerSocketChannel channel;
private volatile int backlog = NetworkConstants.SOMAXCONN;
AioServerSocketChannelConfig(AsynchronousServerSocketChannel channel) {
this.channel = channel;
}
AioServerSocketChannelConfig() {
}
@Override
public Map<ChannelOption<?>, Object> getOptions() {
return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG);

View File

@ -85,7 +85,7 @@ public class AioSocketChannel extends AbstractAioChannel implements SocketChanne
AioSocketChannel(
AioServerSocketChannel parent, Integer id, AsynchronousSocketChannel ch) {
super(parent, id, ch);
config = new AioSocketChannelConfig(null);
config = new AioSocketChannelConfig(ch);
}
@Override