Home / Class/ QuicServerExample Class — netty Architecture

QuicServerExample Class — netty Architecture

Architecture documentation for the QuicServerExample class in QuicServerExample.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  7be62b50_79de_d916_a0d7_e87717583f43["QuicServerExample"]
  c820ec6c_a9f3_29a8_b1c9_e0bfb667de58["QuicServerExample.java"]
  7be62b50_79de_d916_a0d7_e87717583f43 -->|defined in| c820ec6c_a9f3_29a8_b1c9_e0bfb667de58
  cbff61e0_fc4a_d13c_1468_157af3207d34["QuicServerExample()"]
  7be62b50_79de_d916_a0d7_e87717583f43 -->|method| cbff61e0_fc4a_d13c_1468_157af3207d34
  1abb7b1d_92a6_cce6_9ab0_fef90fc51d4c["main()"]
  7be62b50_79de_d916_a0d7_e87717583f43 -->|method| 1abb7b1d_92a6_cce6_9ab0_fef90fc51d4c

Relationship Graph

Source Code

codec-native-quic/src/test/java/io/netty/handler/codec/quic/example/QuicServerExample.java lines 44–126

public final class QuicServerExample {

    private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(QuicServerExample.class);

    private QuicServerExample() { }

    public static void main(String[] args) throws Exception {
        SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate();
        QuicSslContext context = QuicSslContextBuilder.forServer(
                selfSignedCertificate.privateKey(), null, selfSignedCertificate.certificate())
                .applicationProtocols("http/0.9").build();
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
        ChannelHandler codec = new QuicServerCodecBuilder().sslContext(context)
                .maxIdleTimeout(5000, TimeUnit.MILLISECONDS)
                // Configure some limits for the maximal number of streams (and the data) that we want to handle.
                .initialMaxData(10000000)
                .initialMaxStreamDataBidirectionalLocal(1000000)
                .initialMaxStreamDataBidirectionalRemote(1000000)
                .initialMaxStreamsBidirectional(100)
                .initialMaxStreamsUnidirectional(100)
                .activeMigration(true)

                // Setup a token handler. In a production system you would want to implement and provide your custom
                // one.
                .tokenHandler(InsecureQuicTokenHandler.INSTANCE)
                // ChannelHandler that is added into QuicChannel pipeline.
                .handler(new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        QuicChannel channel = (QuicChannel) ctx.channel();
                        // Create streams etc..
                    }

                    public void channelInactive(ChannelHandlerContext ctx) {
                        ((QuicChannel) ctx.channel()).collectStats().addListener(f -> {
                            if (f.isSuccess()) {
                                LOGGER.info("Connection closed: {}", f.getNow());
                            }
                            //only test for first connection
                            ctx.channel().parent().close();
                        });
                    }

                    @Override
                    public boolean isSharable() {
                        return true;
                    }
                })
                .streamHandler(new ChannelInitializer<QuicStreamChannel>() {
                    @Override
                    protected void initChannel(QuicStreamChannel ch)  {
                        // Add a LineBasedFrameDecoder here as we just want to do some simple HTTP 0.9 handling.
                        ch.pipeline().addLast(new LineBasedFrameDecoder(1024))
                                .addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                ByteBuf byteBuf = (ByteBuf) msg;
                                try {
                                    if (byteBuf.toString(CharsetUtil.US_ASCII).trim().equals("GET /")) {
                                        ByteBuf buffer = ctx.alloc().directBuffer();
                                        buffer.writeCharSequence("Hello World!\r\n", CharsetUtil.US_ASCII);
                                        // Write the buffer and shutdown the output by writing a FIN.
                                        ctx.writeAndFlush(buffer).addListener(QuicStreamChannel.SHUTDOWN_OUTPUT);
                                    }
                                } finally {
                                    byteBuf.release();
                                }
                            }
                        });
                    }
                }).build();
        try {
            Bootstrap bs = new Bootstrap();
            Channel channel = bs.group(group)
                    .channel(NioDatagramChannel.class)
                    .handler(codec)
                    .bind(new InetSocketAddress(9999)).sync().channel();
            channel.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }

Frequently Asked Questions

What is the QuicServerExample class?
QuicServerExample is a class in the netty codebase, defined in codec-native-quic/src/test/java/io/netty/handler/codec/quic/example/QuicServerExample.java.
Where is QuicServerExample defined?
QuicServerExample is defined in codec-native-quic/src/test/java/io/netty/handler/codec/quic/example/QuicServerExample.java at line 44.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free