use of io.netty.channel.unix.UnixChannel in project servicetalk by apple.
the class TcpClient method connectWithFdBlocking.
/**
* Connect using a {@link FileDescriptorSocketAddress} and await for the connection.
*
* @param executionContext {@link ExecutionContext} to use for the connections.
* @param address to connect.
* @return New {@link NettyConnection}.
* @throws ExecutionException If connect failed.
* @throws InterruptedException If interrupted while waiting for connect to complete.
*/
public NettyConnection<Buffer, Buffer> connectWithFdBlocking(ExecutionContext<?> executionContext, SocketAddress address) throws Exception {
assumeTrue(executionContext.ioExecutor().isFileDescriptorSocketAddressSupported());
assumeTrue(Epoll.isAvailable() || KQueue.isAvailable());
final Class<? extends Channel> channelClass;
final EventLoopGroup eventLoopGroup;
if (Epoll.isAvailable()) {
eventLoopGroup = new EpollEventLoopGroup(1);
channelClass = EpollSocketChannel.class;
} else {
eventLoopGroup = new KQueueEventLoopGroup(1);
channelClass = KQueueSocketChannel.class;
}
AtomicBoolean dataReadDirectlyFromNetty = new AtomicBoolean();
// Bootstrap a netty channel to the server so we can access its FD and wrap it later in ST.
Bootstrap bs = new Bootstrap();
UnixChannel channel = (UnixChannel) bs.channel(channelClass).group(eventLoopGroup).handler(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
dataReadDirectlyFromNetty.set(true);
}
}).connect(address).sync().channel();
// Unregister it from the netty EventLoop as we want to to handle it via ST.
channel.deregister().sync();
FileDescriptorSocketAddress fd = new FileDescriptorSocketAddress(channel.fd().intValue());
NettyConnection<Buffer, Buffer> connection = connectBlocking(executionContext, fd);
assertThat("Data read on the FileDescriptor from netty pipeline.", dataReadDirectlyFromNetty.get(), is(false));
return connection;
}
Aggregations