| 78 | } |
| 79 | |
| 80 | public static void main(String[] args) { |
| 81 | EventLoopGroup loopGroup; |
| 82 | Class<? extends ServerChannel> serverChannelClass; |
| 83 | var b = new ServerBootstrap(); |
| 84 | if (Epoll.isAvailable()) { |
| 85 | b.option(EpollChannelOption.SO_REUSEPORT, true); |
| 86 | loopGroup = new EpollEventLoopGroup(); |
| 87 | serverChannelClass = EpollServerSocketChannel.class; |
| 88 | } else { |
| 89 | loopGroup = new NioEventLoopGroup(); |
| 90 | serverChannelClass = NioServerSocketChannel.class; |
| 91 | } |
| 92 | //HttpContent |
| 93 | //HttpObjectAggregator |
| 94 | var format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); |
| 95 | loopGroup.next().scheduleWithFixedDelay(() -> Handler.date = format.format(new Date()), 0, 1, TimeUnit.SECONDS); |
| 96 | var addr = new InetSocketAddress(Integer.getInteger("port", 80)); |
| 97 | System.out.println("use " + serverChannelClass.getName() + ", listen " + addr); |
| 98 | b.group(loopGroup) |
| 99 | .option(ChannelOption.SO_BACKLOG, 8192) |
| 100 | .option(ChannelOption.SO_REUSEADDR, true) |
| 101 | .childOption(ChannelOption.SO_REUSEADDR, true) |
| 102 | .channel(serverChannelClass).childHandler(new ChannelInitializer<SocketChannel>() { |
| 103 | private static final HttpDecoderConfig decCfg = new HttpDecoderConfig() |
| 104 | .setMaxInitialLineLength(4096) |
| 105 | .setMaxHeaderSize(8192) |
| 106 | .setMaxChunkSize(8192) |
| 107 | .setChunkedSupported(true) |
| 108 | .setValidateHeaders(false); |
| 109 | |
| 110 | @Override |
| 111 | protected void initChannel(SocketChannel ch) { |
| 112 | ch.pipeline() |
| 113 | .addLast("encoder", new HttpResponseEncoder()) |
| 114 | .addLast("decoder", new HttpRequestDecoder(decCfg)) |
| 115 | .addLast("handler", new Handler()); |
| 116 | } |
| 117 | }).bind(addr); //.sync().channel().closeFuture().sync(); |
| 118 | // loopGroup.shutdownGracefully().sync(); |
| 119 | } |
| 120 | } |