Search in sources :

Example 11 with ServerChannel

use of io.netty.channel.ServerChannel in project activemq-artemis by apache.

the class NettyAcceptor method startServerChannels.

private void startServerChannels() {
    String[] hosts = TransportConfiguration.splitHosts(host);
    for (String h : hosts) {
        SocketAddress address;
        if (useInvm) {
            address = new LocalAddress(h);
        } else {
            address = new InetSocketAddress(h, port);
        }
        Channel serverChannel = bootstrap.bind(address).syncUninterruptibly().channel();
        serverChannelGroup.add(serverChannel);
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) InetSocketAddress(java.net.InetSocketAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) KQueueServerSocketChannel(io.netty.channel.kqueue.KQueueServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) Channel(io.netty.channel.Channel) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 12 with ServerChannel

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

the class DetectPeerCloseWithoutReadTest method serverCloseWithoutClientReadIsDetected0.

private void serverCloseWithoutClientReadIsDetected0(final boolean extraReadRequested) throws InterruptedException {
    EventLoopGroup serverGroup = null;
    EventLoopGroup clientGroup = null;
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicInteger bytesRead = new AtomicInteger();
        final int expectedBytes = 100;
        serverGroup = newGroup();
        clientGroup = newGroup();
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(serverGroup);
        sb.channel(serverChannel());
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ByteBuf buf = ctx.alloc().buffer(expectedBytes);
                        buf.writerIndex(buf.writerIndex() + expectedBytes);
                        ctx.writeAndFlush(buf).addListener(ChannelFutureListener.CLOSE);
                        ctx.fireChannelActive();
                    }
                });
            }
        });
        serverChannel = sb.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        Bootstrap cb = new Bootstrap();
        cb.group(serverGroup);
        cb.channel(clientChannel());
        // Ensure we read only one message per read() call and that we need multiple read()
        // calls to consume everything.
        cb.option(ChannelOption.AUTO_READ, false);
        cb.option(ChannelOption.MAX_MESSAGES_PER_READ, 1);
        cb.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(expectedBytes / 10));
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new TestHandler(bytesRead, extraReadRequested, latch));
            }
        });
        clientChannel = cb.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        latch.await();
        assertEquals(expectedBytes, bytesRead.get());
    } finally {
        if (serverChannel != null) {
            serverChannel.close().syncUninterruptibly();
        }
        if (clientChannel != null) {
            clientChannel.close().syncUninterruptibly();
        }
        if (serverGroup != null) {
            serverGroup.shutdownGracefully();
        }
        if (clientGroup != null) {
            clientGroup.shutdownGracefully();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 13 with ServerChannel

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

the class Http2ServerUpgradeCodecTest method testUpgrade.

private static void testUpgrade(Http2ConnectionHandler handler, ChannelHandler multiplexer) {
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "*");
    request.headers().set(HttpHeaderNames.HOST, "netty.io");
    request.headers().set(HttpHeaderNames.CONNECTION, "Upgrade, HTTP2-Settings");
    request.headers().set(HttpHeaderNames.UPGRADE, "h2c");
    request.headers().set("HTTP2-Settings", "AAMAAABkAAQAAP__");
    ServerChannel parent = Mockito.mock(ServerChannel.class);
    EmbeddedChannel channel = new EmbeddedChannel(parent, DefaultChannelId.newInstance(), true, false, new ChannelInboundHandlerAdapter());
    ChannelHandlerContext ctx = channel.pipeline().firstContext();
    Http2ServerUpgradeCodec codec;
    if (multiplexer == null) {
        codec = new Http2ServerUpgradeCodec(handler);
    } else {
        codec = new Http2ServerUpgradeCodec((Http2FrameCodec) handler, multiplexer);
    }
    assertTrue(codec.prepareUpgradeResponse(ctx, request, new DefaultHttpHeaders()));
    codec.upgradeTo(ctx, request);
    // Flush the channel to ensure we write out all buffered data
    channel.flush();
    channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf());
    Http2FrameInboundWriter writer = new Http2FrameInboundWriter(channel);
    writer.writeInboundSettings(new Http2Settings());
    writer.writeInboundRstStream(Http2CodecUtil.HTTP_UPGRADE_STREAM_ID, Http2Error.CANCEL.code());
    assertSame(handler, channel.pipeline().remove(handler.getClass()));
    assertNull(channel.pipeline().get(handler.getClass()));
    assertTrue(channel.finish());
    // Check that the preface was send (a.k.a the settings frame)
    ByteBuf settingsBuffer = channel.readOutbound();
    assertNotNull(settingsBuffer);
    settingsBuffer.release();
    ByteBuf buf = channel.readOutbound();
    assertNotNull(buf);
    buf.release();
    assertNull(channel.readOutbound());
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerChannel(io.netty.channel.ServerChannel) ByteBuf(io.netty.buffer.ByteBuf) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 14 with ServerChannel

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

the class ParameterizedSslHandlerTest method reentryOnHandshakeComplete.

private void reentryOnHandshakeComplete(SslProvider clientProvider, SslProvider serverProvider, EventLoopGroup group, SocketAddress bindAddress, Class<? extends ServerChannel> serverClass, Class<? extends Channel> clientClass, boolean serverAutoRead, boolean clientAutoRead) throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(clientProvider).build();
    Channel sc = null;
    Channel cc = null;
    try {
        final String expectedContent = "HelloWorld";
        final CountDownLatch serverLatch = new CountDownLatch(1);
        final CountDownLatch clientLatch = new CountDownLatch(1);
        final StringBuilder serverQueue = new StringBuilder(expectedContent.length());
        final StringBuilder clientQueue = new StringBuilder(expectedContent.length());
        sc = new ServerBootstrap().group(group).channel(serverClass).childOption(ChannelOption.AUTO_READ, serverAutoRead).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(disableHandshakeTimeout(sslServerCtx.newHandler(ch.alloc())));
                ch.pipeline().addLast(new ReentryWriteSslHandshakeHandler(expectedContent, serverQueue, serverLatch));
            }
        }).bind(bindAddress).syncUninterruptibly().channel();
        cc = new Bootstrap().group(group).channel(clientClass).option(ChannelOption.AUTO_READ, clientAutoRead).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(disableHandshakeTimeout(sslClientCtx.newHandler(ch.alloc())));
                ch.pipeline().addLast(new ReentryWriteSslHandshakeHandler(expectedContent, clientQueue, clientLatch));
            }
        }).connect(sc.localAddress()).syncUninterruptibly().channel();
        serverLatch.await();
        assertEquals(expectedContent, serverQueue.toString());
        clientLatch.await();
        assertEquals(expectedContent, clientQueue.toString());
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) 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)

Aggregations

ServerChannel (io.netty.channel.ServerChannel)14 Channel (io.netty.channel.Channel)10 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)6 ChannelFuture (io.netty.channel.ChannelFuture)5 EventLoopGroup (io.netty.channel.EventLoopGroup)5 InetSocketAddress (java.net.InetSocketAddress)5 Bootstrap (io.netty.bootstrap.Bootstrap)4 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 LocalServerChannel (io.netty.channel.local.LocalServerChannel)4 ChannelInitializer (io.netty.channel.ChannelInitializer)3 LocalAddress (io.netty.channel.local.LocalAddress)3 IOException (java.io.IOException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 MAX_PORT_NUMBER (io.scalecube.transport.Addressing.MAX_PORT_NUMBER)2 MIN_PORT_NUMBER (io.scalecube.transport.Addressing.MIN_PORT_NUMBER)2