Search in sources :

Example 6 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project transporter by wang4ever.

the class WSChildHandlerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    WSConfig conf = this.config.getWsConfig();
    if (logger.isDebugEnabled())
        logger.debug("Initital channel handler...\twebsocketPath={}", conf.getWebsocketPath());
    ChannelPipeline p = ch.pipeline();
    // Configure SSL.
    if (conf.getSslEnable()) {
        /*
			 * SelfSignedCertificate ssc = new SelfSignedCertificate();
			 * SslContext sslCtx =
			 * SslContextBuilder.forServer(ssc.certificate(),
			 * ssc.privateKey()).build();
			 */
        File keyCertChainFile = new File(Resources.getResource(conf.getKeyCertChainFile()).getFile());
        File keyFile = new File(Resources.getResource(conf.getKeyFile()).getFile());
        SslContext sslCtx = SslContextBuilder.forServer(keyCertChainFile, keyFile).build();
        p.addLast(sslCtx.newHandler(ch.alloc()));
        if (logger.isDebugEnabled())
            logger.debug("The ssl(wss) handler has been enabled. sslCtx={}", sslCtx.toString());
    }
    // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
    if (conf.getLoggingEnable()) {
        p.addLast(new LoggingHandler(LogLevel.valueOf(conf.getLoggingLevel())));
        if (logger.isInfoEnabled())
            logger.info("Netty internal log has been used. (WS)level={}", conf.getLoggingLevel());
    }
    IdleStateHandler idleHandler = new IdleStateHandler(conf.getReadIdleSeconds(), conf.getWriteIdleSeconds(), conf.getAllIdleSeconds());
    p.addLast(idleHandler);
    // 将请求和应答消息解码为HTTP消息
    p.addLast("http-codec", new HttpServerCodec());
    // 将HTTP消息的多个部分合成一条完整的HTTP消息
    p.addLast("aggregator", new HttpObjectAggregator(65536));
    // 向客户端发送HTML5文件
    p.addLast("http-chunked", new ChunkedWriteHandler());
    p.addLast("ws-protocol", new WebSocketServerProtocolHandler(conf.getWebsocketPath(), null, true));
    p.addLast("text-handler", SpringContextHolder.getBean("textWSFrameHandler"));
}
Also used : WSConfig(io.transport.core.config.Configuration.WSConfig) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) File(java.io.File) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslContext(io.netty.handler.ssl.SslContext)

Example 7 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project brave by openzipkin.

the class ITNettyHttpTracing method init.

@Override
protected void init() throws Exception {
    stop();
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(NettyHttpTracing.create(httpTracing).serverHandler());
            p.addLast(new TestHandler(httpTracing));
        }
    });
    Channel ch = b.bind(0).sync().channel();
    port = ((InetSocketAddress) ch.localAddress()).getPort();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 8 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project brave by openzipkin.

the class NettyHttpServerBenchmarks method initServer.

@Override
protected int initServer() throws InterruptedException {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(new TracingDispatchHandler());
            p.addLast(new HelloWorldHandler());
        }
    });
    Channel ch = b.bind(0).sync().channel();
    return ((InetSocketAddress) ch.localAddress()).getPort();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RunnerException(org.openjdk.jmh.runner.RunnerException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 9 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project BRFS by zhangnianli.

the class NettyMessageServer method bind.

public void bind(int port) throws Exception {
    // 配置服务端的NIO线程组
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) {
                ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                ch.pipeline().addLast("http-servercodec", new HttpServerCodec());
                // 定义缓冲数据量
                ch.pipeline().addLast("http-aggegator", new HttpObjectAggregator(1024 * 1024 * 64));
                // ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                // ch.pipeline().addLast(
                // new ProtobufDecoder(NettyMessageProto.NettyMessageReqRes.getDefaultInstance()));
                // ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                // ch.pipeline().addLast(new ProtobufEncoder());
                // ch.pipeline().addLast("http-chunked",new ChunkedWriteHandler());
                ch.pipeline().addLast(new NettyMessageServerHandler());
                ch.pipeline().addLast("http-responseencoder", new HttpResponseEncoder());
            }
        });
        // 绑定端口,同步等待成功
        ChannelFuture f = b.bind(port).sync();
        System.out.println("init start");
        // 等待服务端监听端口关闭
        f.channel().closeFuture().sync();
    } finally {
        // 优雅退出,释放线程池资源
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 10 with HttpServerCodec

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec in project ballerina by ballerina-lang.

the class WebSocketRemoteServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    WebSocketRemoteServerFrameHandler frameHandler = new WebSocketRemoteServerFrameHandler();
    FRAME_HANDLERS.add(frameHandler);
    pipeline.addLast(frameHandler);
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) WebSocketServerCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)54 ChannelPipeline (io.netty.channel.ChannelPipeline)35 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)10 Test (org.junit.Test)9 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)8 Channel (io.netty.channel.Channel)7 ChannelHandler (io.netty.channel.ChannelHandler)7 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)6 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)6 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 SocketChannel (io.netty.channel.socket.SocketChannel)4 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)4