From 96dc3f983fd8be1fd4069fcf1f97d77023a1c516 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 14 Dec 2012 09:51:36 +0100 Subject: [PATCH] Refactor AIO Transport to allow to use Bootstrap without the ugly hack --- .../io/netty/bootstrap/ServerBootstrap.java | 17 ------------- .../socket/aio/AioServerSocketChannel.java | 24 +++++++++++-------- .../aio/AioServerSocketChannelConfig.java | 5 +++- .../channel/socket/aio/AioSocketChannel.java | 2 +- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java index fb49ce897f..f42073dbd9 100644 --- a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -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 { 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 { 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"; - } - } } diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java index d51c915d1d..b03ee46e5f 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannel.java @@ -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 diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannelConfig.java b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannelConfig.java index d23dae7241..092a8d8f48 100644 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannelConfig.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioServerSocketChannelConfig.java @@ -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, Object> getOptions() { return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG); diff --git a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java index 04676ff236..f231f2338d 100755 --- a/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java @@ -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