Search in sources :

Example 16 with Channel

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

the class Http2MultiplexCodecTest method failedOutboundStreamCreationThrowsAndClosesChannel.

/**
     * Test failing the promise of the first headers frame of an outbound stream. In practice this error case would most
     * likely happen due to the max concurrent streams limit being hit or the channel running out of stream identifiers.
     */
@Test(expected = Http2NoMoreStreamIdsException.class)
public void failedOutboundStreamCreationThrowsAndClosesChannel() throws Exception {
    parentChannel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            promise.tryFailure(new Http2NoMoreStreamIdsException());
        }
    });
    LastInboundHandler inboundHandler = new LastInboundHandler();
    childChannelInitializer.handler = inboundHandler;
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    Channel childChannel = b.parentChannel(parentChannel).handler(childChannelInitializer).connect().channel();
    assertTrue(childChannel.isActive());
    childChannel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
    parentChannel.flush();
    assertFalse(childChannel.isActive());
    assertFalse(childChannel.isOpen());
    inboundHandler.checkException();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Example 17 with Channel

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

the class Http2CodecTest method createOutboundStream.

@Test
public void createOutboundStream() {
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    Channel childChannel = b.parentChannel(clientChannel).handler(new TestChannelInitializer()).connect().syncUninterruptibly().channel();
    assertTrue(childChannel.isRegistered());
    assertTrue(childChannel.isActive());
    Http2Headers headers = new DefaultHttp2Headers();
    childChannel.write(new DefaultHttp2HeadersFrame(headers));
    ByteBuf data = Unpooled.buffer(100).writeZero(100);
    childChannel.writeAndFlush(new DefaultHttp2DataFrame(data, true));
    Http2HeadersFrame headersFrame = serverLastInboundHandler.blockingReadInbound();
    assertNotNull(headersFrame);
    assertEquals(3, headersFrame.streamId());
    assertEquals(headers, headersFrame.headers());
    Http2DataFrame dataFrame = serverLastInboundHandler.blockingReadInbound();
    assertNotNull(dataFrame);
    assertEquals(3, dataFrame.streamId());
    assertEquals(data.resetReaderIndex(), dataFrame.content());
    assertTrue(dataFrame.isEndStream());
    dataFrame.release();
    childChannel.close();
    Http2ResetFrame rstFrame = serverLastInboundHandler.blockingReadInbound();
    assertNotNull(rstFrame);
    assertEquals(3, rstFrame.streamId());
}
Also used : LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 18 with Channel

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

the class Http2CodecTest method setUp.

@Before
public void setUp() throws InterruptedException {
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    LocalAddress serverAddress = new LocalAddress(getClass().getName());
    serverLastInboundHandler = new SharableLastInboundHandler();
    ServerBootstrap sb = new ServerBootstrap().channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverConnectedChannel = ch;
            ch.pipeline().addLast(new Http2Codec(true, serverLastInboundHandler));
            serverChannelLatch.countDown();
        }
    });
    serverChannel = sb.bind(serverAddress).sync().channel();
    Bootstrap cb = new Bootstrap().channel(LocalChannel.class).group(group).handler(new Http2Codec(false, new TestChannelInitializer()));
    clientChannel = cb.connect(serverAddress).sync().channel();
    assertTrue(serverChannelLatch.await(5, TimeUnit.SECONDS));
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Before(org.junit.Before)

Example 19 with Channel

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

the class Http2CodecTest method multipleOutboundStreams.

@Test
public void multipleOutboundStreams() {
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    b.parentChannel(clientChannel).handler(new TestChannelInitializer());
    Channel childChannel1 = b.connect().syncUninterruptibly().channel();
    assertTrue(childChannel1.isActive());
    assertFalse(isStreamIdValid(((AbstractHttp2StreamChannel) childChannel1).streamId()));
    Channel childChannel2 = b.connect().channel();
    assertTrue(childChannel2.isActive());
    assertFalse(isStreamIdValid(((AbstractHttp2StreamChannel) childChannel2).streamId()));
    Http2Headers headers1 = new DefaultHttp2Headers();
    Http2Headers headers2 = new DefaultHttp2Headers();
    // Test that streams can be made active (headers sent) in different order than the corresponding channels
    // have been created.
    childChannel2.writeAndFlush(new DefaultHttp2HeadersFrame(headers2));
    childChannel1.writeAndFlush(new DefaultHttp2HeadersFrame(headers1));
    Http2HeadersFrame headersFrame2 = serverLastInboundHandler.blockingReadInbound();
    assertNotNull(headersFrame2);
    assertEquals(3, headersFrame2.streamId());
    Http2HeadersFrame headersFrame1 = serverLastInboundHandler.blockingReadInbound();
    assertNotNull(headersFrame1);
    assertEquals(5, headersFrame1.streamId());
    assertEquals(3, ((AbstractHttp2StreamChannel) childChannel2).streamId());
    assertEquals(5, ((AbstractHttp2StreamChannel) childChannel1).streamId());
    childChannel1.close();
    childChannel2.close();
}
Also used : LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) Test(org.junit.Test)

Example 20 with Channel

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

the class Http2FrameWriterBenchmark method boostrapEnvWithTransport.

private static Environment boostrapEnvWithTransport(final EnvironmentType environmentType) {
    final EnvironmentParameters params = environmentType.params();
    ServerBootstrap sb = new ServerBootstrap();
    Bootstrap cb = new Bootstrap();
    final TransportEnvironment environment = new TransportEnvironment(cb, sb);
    EventLoopGroup serverEventLoopGroup = params.newEventLoopGroup();
    sb.group(serverEventLoopGroup, serverEventLoopGroup);
    sb.channel(params.serverChannelClass());
    sb.option(ChannelOption.ALLOCATOR, params.serverAllocator());
    sb.childOption(ChannelOption.ALLOCATOR, params.serverAllocator());
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
        }
    });
    cb.group(params.newEventLoopGroup());
    cb.channel(params.clientChannelClass());
    cb.option(ChannelOption.ALLOCATOR, params.clientAllocator());
    final CountDownLatch latch = new CountDownLatch(1);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            Http2Connection connection = new DefaultHttp2Connection(false);
            Http2RemoteFlowController remoteFlowController = params.remoteFlowController();
            if (remoteFlowController != null) {
                connection.remote().flowController(params.remoteFlowController());
            }
            Http2LocalFlowController localFlowController = params.localFlowController();
            if (localFlowController != null) {
                connection.local().flowController(localFlowController);
            }
            environment.writer(new DefaultHttp2FrameWriter());
            Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, environment.writer());
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
            Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).frameListener(new Http2FrameAdapter()).codec(decoder, encoder).build();
            p.addLast(connectionHandler);
            environment.context(p.lastContext());
            // Must wait for context to be set.
            latch.countDown();
        }
    });
    environment.serverChannel(sb.bind(params.address()));
    params.address(environment.serverChannel().localAddress());
    environment.clientChannel(cb.connect(params.address()));
    try {
        if (!latch.await(5, SECONDS)) {
            throw new RuntimeException("Channel did not initialize in time");
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
    return environment;
}
Also used : Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2Connection(io.netty.handler.codec.http2.Http2Connection) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) Http2RemoteFlowController(io.netty.handler.codec.http2.Http2RemoteFlowController) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) Http2ConnectionHandlerBuilder(io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DefaultHttp2FrameReader(io.netty.handler.codec.http2.DefaultHttp2FrameReader) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) Channel(io.netty.channel.Channel) DefaultHttp2ConnectionDecoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionDecoder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultHttp2FrameWriter(io.netty.handler.codec.http2.DefaultHttp2FrameWriter) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder)

Aggregations

Channel (io.netty.channel.Channel)886 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)226 ChannelFuture (io.netty.channel.ChannelFuture)204 Test (org.junit.Test)203 Bootstrap (io.netty.bootstrap.Bootstrap)199 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 InetSocketAddress (java.net.InetSocketAddress)165 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)132 IOException (java.io.IOException)126 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)106 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)96 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89