Search in sources :

Example 41 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project reactor-netty by reactor.

the class NettyContextTest method addSeveralByteEncodersWhenCodec.

@Test
public void addSeveralByteEncodersWhenCodec() throws Exception {
    ChannelHandler encoder1 = new LineBasedFrameDecoder(12);
    ChannelHandler encoder2 = new LineBasedFrameDecoder(13);
    channel.pipeline().addLast(NettyPipeline.HttpCodec, new HttpServerCodec()).addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()).addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
    });
    testContext.addHandlerFirst("encoder1", encoder1).addHandlerFirst("encoder2", encoder2);
    assertEquals(channel.pipeline().names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "encoder2", "encoder1", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0"));
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 42 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project reactor-netty by reactor.

the class NettyContextTest method addNonByteDecoderWhenFullReactorPipeline.

@Test
public void addNonByteDecoderWhenFullReactorPipeline() throws Exception {
    channel.pipeline().addLast(NettyPipeline.HttpCodec, new HttpServerCodec()).addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()).addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
    });
    ChannelHandler decoder = new ChannelHandlerAdapter() {
    };
    testContext.addHandlerLast("decoder", decoder);
    assertEquals(channel.pipeline().names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "decoder", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0"));
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 43 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project reactor-netty by reactor.

the class NettyContextTest method addSeveralByteDecodersWhenCodec.

@Test
public void addSeveralByteDecodersWhenCodec() throws Exception {
    ChannelHandler decoder1 = new LineBasedFrameDecoder(12);
    ChannelHandler decoder2 = new LineBasedFrameDecoder(13);
    channel.pipeline().addLast(NettyPipeline.HttpCodec, new HttpServerCodec()).addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()).addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
    });
    testContext.addHandlerLast("decoder1$extract", NettyPipeline.inboundHandler(ADD_EXTRACTOR)).addHandlerLast("decoder1", decoder1).addHandlerLast("decoder2$extract", NettyPipeline.inboundHandler(ADD_EXTRACTOR)).addHandlerLast("decoder2", decoder2);
    assertEquals(channel.pipeline().names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "decoder1$extract", "decoder1", "decoder2$extract", "decoder2", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0"));
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 44 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project summer by foxsugar.

the class WebSocketServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 处理日志
    // pipeline.addLast(new LoggingHandler(LogLevel.INFO));
    // 处理心跳
    // pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS));
    // pipeline.addLast(new ChatHeartbeatHandler());
    // // 获取职责链
    // pipeline.addLast("http-codec", new HttpServerCodec());
    // pipeline.addLast("aggregator", new HttpObjectAggregator(64*1024));
    // pipeline.addLast("http-chunked", new ChunkedWriteHandler());
    // //        pipeline.addLast(new WebSocketServerCompressionHandler());
    // 
    // //        pipeline.addLast("encoder", new WsEncoder());
    // //        pipeline.addLast("decoder", new WsDecoder());
    // pipeline.addLast("handshake",new WebSocketServerProtocolHandler("/ws"));
    // pipeline.addLast(new WsCodec());
    // //        pipeline.addLast(new WsGameMsgHandler());
    // pipeline.addLast(new WsHandler());
    // //        pipeline.addLast("codec", new WsEncoder());
    // //websocket定义了传递数据的6中frame类型
    // //        pipeline.addLast(new GameMsgHandler());
    // 
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64 * 1024));
    pipeline.addLast(new WebSocketServerProtocolHandler("/websocket", null, true));
    pipeline.addLast(new WsCodec());
    pipeline.addLast(new GameMsgHandler());
// ChannelPipeline pipeline = ch.pipeline();
// pipeline.addLast(new HttpServerCodec());
// pipeline.addLast(new HttpObjectAggregator(65536));
// //        pipeline.addLast(new WebSocketServerCompressionHandler());
// //        pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
// //        pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
// pipeline.addLast(new WebSocketServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) GameMsgHandler(com.code.server.gate.handle.GameMsgHandler) WsCodec(com.code.server.gate.encoding.WsCodec) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 45 with HttpServerCodec

use of io.netty.handler.codec.http.HttpServerCodec in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetIdleEventHandler());
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

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