Search in sources :

Example 91 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SctpLimitStreamsTest method testSctpInitMaxstreams.

@Test(timeout = 5000)
public void testSctpInitMaxstreams() throws Exception {
    EventLoopGroup loop = newEventLoopGroup();
    try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(loop).channel(serverClass()).option(ChannelOption.SO_REUSEADDR, true).option(SctpChannelOption.SCTP_INIT_MAXSTREAMS, SctpStandardSocketOptions.InitMaxStreams.create(1, 1)).localAddress(new InetSocketAddress(0)).childHandler(new ChannelInboundHandlerAdapter());
        Bootstrap clientBootstrap = new Bootstrap().group(loop).channel(clientClass()).option(SctpChannelOption.SCTP_INIT_MAXSTREAMS, SctpStandardSocketOptions.InitMaxStreams.create(112, 112)).handler(new ChannelInboundHandlerAdapter());
        Channel serverChannel = serverBootstrap.bind().syncUninterruptibly().channel();
        SctpChannel clientChannel = (SctpChannel) clientBootstrap.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        assertEquals(1, clientChannel.association().maxOutboundStreams());
        assertEquals(1, clientChannel.association().maxInboundStreams());
        serverChannel.close().syncUninterruptibly();
        clientChannel.close().syncUninterruptibly();
    } finally {
        loop.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 92 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class EpollDomainSocketFdTest method testSendRecvFd.

public void testSendRecvFd(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(1);
    sb.childHandler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            // Create new channel and obtain a file descriptor from it.
            final EpollDomainSocketChannel ch = new EpollDomainSocketChannel();
            ctx.writeAndFlush(ch.fd()).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        Throwable cause = future.cause();
                        queue.offer(cause);
                    }
                }
            });
        }
    });
    cb.handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            FileDescriptor fd = (FileDescriptor) msg;
            queue.offer(fd);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            queue.add(cause);
            ctx.close();
        }
    });
    cb.option(EpollChannelOption.DOMAIN_SOCKET_READ_MODE, DomainSocketReadMode.FILE_DESCRIPTORS);
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    Object received = queue.take();
    cc.close().sync();
    sc.close().sync();
    if (received instanceof FileDescriptor) {
        FileDescriptor fd = (FileDescriptor) received;
        Assert.assertTrue(fd.isOpen());
        fd.close();
        Assert.assertFalse(fd.isOpen());
        Assert.assertNull(queue.poll());
    } else {
        throw (Throwable) received;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) FileDescriptor(io.netty.channel.unix.FileDescriptor) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 93 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class EpollServerSocketChannelConfigTest method before.

@BeforeClass
public static void before() {
    group = new EpollEventLoopGroup(1);
    ServerBootstrap bootstrap = new ServerBootstrap();
    ch = (EpollServerSocketChannel) bootstrap.group(group).channel(EpollServerSocketChannel.class).childHandler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) BeforeClass(org.junit.BeforeClass)

Example 94 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class EpollSocketChannelConfigTest method before.

@BeforeClass
public static void before() {
    rand = new Random();
    group = new EpollEventLoopGroup(1);
    Bootstrap bootstrap = new Bootstrap();
    ch = (EpollSocketChannel) bootstrap.group(group).channel(EpollSocketChannel.class).handler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
}
Also used : Random(java.util.Random) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) BeforeClass(org.junit.BeforeClass)

Example 95 with ChannelInboundHandlerAdapter

use of io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class EpollSocketChannelTest method testTcpInfo.

@Test
public void testTcpInfo() throws Exception {
    EventLoopGroup group = new EpollEventLoopGroup(1);
    try {
        Bootstrap bootstrap = new Bootstrap();
        EpollSocketChannel ch = (EpollSocketChannel) bootstrap.group(group).channel(EpollSocketChannel.class).handler(new ChannelInboundHandlerAdapter()).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        EpollTcpInfo info = ch.tcpInfo();
        assertTcpInfo0(info);
        ch.close().syncUninterruptibly();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)103 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)75 Channel (io.netty.channel.Channel)65 Test (org.junit.Test)62 Bootstrap (io.netty.bootstrap.Bootstrap)51 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)48 ChannelFuture (io.netty.channel.ChannelFuture)33 CountDownLatch (java.util.concurrent.CountDownLatch)32 EventLoopGroup (io.netty.channel.EventLoopGroup)28 InetSocketAddress (java.net.InetSocketAddress)26 ClosedChannelException (java.nio.channels.ClosedChannelException)26 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)23 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)23 ByteBuf (io.netty.buffer.ByteBuf)22 ChannelFutureListener (io.netty.channel.ChannelFutureListener)17 LocalServerChannel (io.netty.channel.local.LocalServerChannel)17 SocketChannel (io.netty.channel.socket.SocketChannel)17 LocalAddress (io.netty.channel.local.LocalAddress)16 LocalChannel (io.netty.channel.local.LocalChannel)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)15