Example usage for io.netty.handler.codec LengthFieldBasedFrameDecoder LengthFieldBasedFrameDecoder

List of usage examples for io.netty.handler.codec LengthFieldBasedFrameDecoder LengthFieldBasedFrameDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec LengthFieldBasedFrameDecoder LengthFieldBasedFrameDecoder.

Prototype

public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) 

Source Link

Document

Creates a new instance.

Usage

From source file:james.learn.netty.echo2.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO//from w  ww  .  j  a v a2  s  . com
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        /* ByteBuf delimiter = Unpooled.copiedBuffer("$_"
                            .getBytes());
                         ch.pipeline().addLast(
                            new DelimiterBasedFrameDecoder(1024,
                               delimiter));*/
                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 4));
                        ch.pipeline().addLast(new LengthFieldPrepender(4, false));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:james.learn.netty.echo2.EchoServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO/*from w  w w. ja  v a 2  s . co  m*/
    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) throws Exception {
                        /*ByteBuf delimiter = Unpooled.copiedBuffer("$_"
                           .getBytes());
                        ch.pipeline().addLast(
                           new DelimiterBasedFrameDecoder(1024,
                              delimiter));*/
                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024 * 1024, 0, 4));
                        ch.pipeline().addLast(new LengthFieldPrepender(4, false));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:org.aotorrent.client.InboundChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();/*from   w w  w.ja va 2s  .c  o m*/
    //p.addLast(new LoggingHandler(LogLevel.INFO));
    p.addLast("timeoutHandler", new ReadTimeoutHandler(60, TimeUnit.SECONDS));
    p.addLast("bytesEncoder", new ByteArrayEncoder());
    p.addLast("handshakeHandler", new InboundHandshakeHandler(client));
    p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(65535, 0, 4));
    p.addLast("peerRequestDecoder", new PeerRequestDecoder());
}

From source file:org.aotorrent.client.OutboundChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();/*from  www  . j a  va 2 s .co  m*/
    //p.addLast(new LoggingHandler(LogLevel.INFO));
    p.addLast("trafficCounter", torrentEngine.getCounter());
    p.addLast("timeoutHandler", new ReadTimeoutHandler(60, TimeUnit.SECONDS));
    p.addLast("bytesEncoder", new ByteArrayEncoder());
    p.addLast("handshakeHandler", new OutboundHandshakeHandler(torrentEngine));
    p.addLast("lengthFrameDecoder", new LengthFieldBasedFrameDecoder(65535, 0, 4));
    p.addLast("peerRequestDecoder", new PeerRequestDecoder());
}

From source file:org.apache.hadoop.hbase.ipc.NettyRpcConnection.java

License:Apache License

private void established(Channel ch) throws IOException {
    ChannelPipeline p = ch.pipeline();//w w  w  .j av  a2 s  .c o  m
    String addBeforeHandler = p.context(BufferCallBeforeInitHandler.class).name();
    p.addBefore(addBeforeHandler, null,
            new IdleStateHandler(0, rpcClient.minIdleTimeBeforeClose, 0, TimeUnit.MILLISECONDS));
    p.addBefore(addBeforeHandler, null, new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    p.addBefore(addBeforeHandler, null,
            new NettyRpcDuplexHandler(this, rpcClient.cellBlockBuilder, codec, compressor));
    p.fireUserEventTriggered(BufferCallEvent.success());
}

From source file:org.ensembl.gti.seqstore.server.MetaDataServer.java

License:Apache License

public void run() throws Exception {
    log.info("Starting server");
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w  w w . j  ava 2 s  .  c o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4),
                                getHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        // Bind and start to accept incoming connections.
        log.info("Binding to port " + port);
        ChannelFuture f = b.bind(port).sync();

        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to
        // gracefully shut down your server.
        log.info("Syncing future");
        f.channel().closeFuture().sync();
    } finally {
        log.info("Shutting server server");
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:org.traccar.protocol.EelinkProtocol.java

License:Apache License

public EelinkProtocol() {
    setSupportedDataCommands(Command.TYPE_CUSTOM, Command.TYPE_POSITION_SINGLE, Command.TYPE_ENGINE_STOP,
            Command.TYPE_ENGINE_RESUME, Command.TYPE_REBOOT_DEVICE);
    addServer(new TrackerServer(false, getName()) {
        @Override//from w  ww.jav  a2s  .c o  m
        protected void addProtocolHandlers(PipelineBuilder pipeline) {
            pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 3, 2));
            pipeline.addLast(new EelinkProtocolEncoder(false));
            pipeline.addLast(new EelinkProtocolDecoder(EelinkProtocol.this));
        }
    });
    addServer(new TrackerServer(true, getName()) {
        @Override
        protected void addProtocolHandlers(PipelineBuilder pipeline) {
            pipeline.addLast(new EelinkProtocolEncoder(true));
            pipeline.addLast(new EelinkProtocolDecoder(EelinkProtocol.this));
        }
    });
}

From source file:org.traccar.protocol.GatorProtocol.java

License:Apache License

public GatorProtocol() {
    addServer(new TrackerServer(false, getName()) {
        @Override//from   w  w w  .j a va2s .  c  om
        protected void addProtocolHandlers(PipelineBuilder pipeline) {
            pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 3, 2));
            pipeline.addLast(new GatorProtocolDecoder(GatorProtocol.this));
        }
    });
    addServer(new TrackerServer(true, getName()) {
        @Override
        protected void addProtocolHandlers(PipelineBuilder pipeline) {
            pipeline.addLast(new GatorProtocolDecoder(GatorProtocol.this));
        }
    });
}

From source file:org.traccar.protocol.KhdProtocol.java

License:Apache License

public KhdProtocol() {
    setSupportedDataCommands(Command.TYPE_ENGINE_STOP, Command.TYPE_ENGINE_RESUME);
    addServer(new TrackerServer(false, getName()) {
        @Override/*from w  w  w  . j av  a2 s.co m*/
        protected void addProtocolHandlers(PipelineBuilder pipeline) {
            pipeline.addLast(new LengthFieldBasedFrameDecoder(512, 3, 2));
            pipeline.addLast(new KhdProtocolEncoder());
            pipeline.addLast(new KhdProtocolDecoder(KhdProtocol.this));
        }
    });
}