我应该如何在Netty中安排编解码器处理程序?

时间:2014-05-06 08:51:57

标签: java netty encoder decoder

我对Netty编解码器有点困惑,我知道我需要在管道中添加一些代码,但是我应该如何安排编码器和解码器呢?任何人都可以简单解释如何在管道中安排编码器和解码器的规则吗?

服务器端代码:

protected static void run() throws Exception {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup);
    b.channel(NioServerSocketChannel.class);
    b.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();               

            pipeline.addLast("decoder", new HttpRequestDecoder());
            pipeline.addLast(new ProtobufVarint32FrameDecoder());
            pipeline.addLast("bufd",new 

                            ProtobufDecoder(PbConditions.Conditions.getDefaultInstance()));
            pipeline.addLast("servercodec",new HttpServerCodec());
            pipeline.addLast("aggegator",new HttpObjectAggregator(Integer.MAX_VALUE));
            pipeline.addLast(new MemcachedServiceHandler());
            pipeline.addLast("",new ProtobufEncoder());
            pipeline.addLast("responseencoder",new HttpResponseEncoder());
            pipeline.addLast("clientcodec",new HttpClientCodec());

        }
    });
    b.bind(IP, PORT).sync();

}

0 个答案:

没有答案