Search in sources :

Example 1 with UnixChannel

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;
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) UnixChannel(io.netty.channel.unix.UnixChannel) FileDescriptorSocketAddress(io.servicetalk.transport.api.FileDescriptorSocketAddress)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 KQueueEventLoopGroup (io.netty.channel.kqueue.KQueueEventLoopGroup)1 UnixChannel (io.netty.channel.unix.UnixChannel)1 Buffer (io.servicetalk.buffer.api.Buffer)1 FileDescriptorSocketAddress (io.servicetalk.transport.api.FileDescriptorSocketAddress)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1