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,
        int lengthAdjustment, int initialBytesToStrip) 

Source Link

Document

Creates a new instance.

Usage

From source file:com.github.mrstampy.gameboot.otp.netty.server.ClearServerInitializer.java

License:Open Source License

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new LengthFieldPrepender(4));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast(utils.getBean(OtpClearNettyHandler.class));
}

From source file:com.github.mrstampy.gameboot.otp.netty.server.EncryptedServerInitializer.java

License:Open Source License

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new SslHandler(createSslEngine()));
    pipeline.addLast(new LengthFieldPrepender(2));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast(utils.getBean(OtpEncryptedNettyHandler.class));
}

From source file:com.github.spapageo.jannel.channel.ChannelHandlerProvider.java

License:Open Source License

public ChannelHandler getChangeHandler(HandlerType handlerType, ClientSessionConfiguration sessionConfiguration,
        SessionCallbackHandler clientSession, Transcoder transcoder) {
    switch (handlerType) {
    case MESSAGE_LOGGER:
        return MESSAGE_LOGGER;
    case SESSION_WRAPPER:
        return new SessionWrapperHandler(clientSession);
    case WRITE_TIMEOUT_HANDLER:
        return new WriteTimeoutHandler(sessionConfiguration.getWriteTimeout(), TimeUnit.MILLISECONDS);
    case MESSAGE_DECODER:
        return new MessageDecoder(transcoder);
    case MESSAGE_ENCODER:
        return new MessageEncoder(transcoder);
    case LENGTH_FRAME_DECODER:
        return new LengthFieldBasedFrameDecoder(MAXIMUM_MESSAGE_BYTE_SIZE, MESSAGE_FIELD_OFFSET,
                LENGTH_FIELD_SIZE, 0, LENGTH_FIELD_SIZE);
    case LENGTH_FRAME_ENCODER:
        return new LengthFieldPrepender(LENGTH_FIELD_SIZE, false);
    default:/*from ww w  .j a  va2 s . co  m*/
        throw new IllegalArgumentException("Invalid handler type");
    }
}

From source file:com.hipishare.chat.server.handler.SecureChatInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // HandlerPipeline?InboundHandler?OutboundHandler?
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    //        pipeline.addLast(sslCtx.newHandler(ch.alloc()));

    // On top of the SSL handler, add the text line codec.
    //        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
    /*pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());*/
    pipeline.addLast(new MsgObjectDecoder());
    pipeline.addLast(new MsgObjectEncoder());

    // /*from   ww w. j a  v  a  2 s. c om*/
    pipeline.addLast(new IdleStateHandler(160, 130, 0, TimeUnit.SECONDS));
    pipeline.addLast(new HeartBeatHandler());

    // and then business logic.
    pipeline.addLast(new SecureChatHandler());
}

From source file:com.janlr.sanxiao.game.handler.GpbTcpServer.java

License:Apache License

@Override
protected ChannelHandler newChannelInitializer() {
    return new ChannelInitializer<NioSocketChannel>() {
        @Override//w w  w  . java 2  s .  c om
        protected void initChannel(NioSocketChannel ch) throws Exception {
            ChannelPipeline cp = ch.pipeline();
            cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
            cp.addLast("prepender", FRAME_PREPENDER);
            cp.addLast("decoder", GPB_DECODER_HANDLER);
            cp.addLast("encoder", GPB_ENCODER_HANDLER);
            // handler
            cp.addLast("handler", serverHandler);
            //                cp.addLast("handler", new ServerHandler());
        }
    };
}

From source file:com.kixeye.kixmpp.p2p.node.NodeClient.java

License:Apache License

public void initialize(final String host, int port, final EventLoopGroup workerGroup,
        final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener)
        throws InterruptedException {
    // prepare connection
    Bootstrap boot = new Bootstrap();
    boot.group(workerGroup);/*w  w  w .  j a va  2  s.  c o m*/
    boot.channel(NioSocketChannel.class);
    boot.option(ChannelOption.SO_KEEPALIVE, true);
    boot.option(ChannelOption.TCP_NODELAY, true);
    boot.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();

            //p.addLast(new LoggingHandler());

            // encoders
            p.addLast(new LengthFieldPrepender(4));
            p.addLast(new ProtostuffEncoder(messageRegistry));

            // decoders
            p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4));
            p.addLast(new ProtostuffDecoder(messageRegistry));
            p.addLast(channelListener);
        }
    });

    // connect
    channel = boot.connect(host, port).sync().channel();
}

From source file:com.kixeye.kixmpp.p2p.node.NodeServer.java

License:Apache License

public void initialize(final String host, final int port, final EventLoopGroup bossGroup,
        final EventLoopGroup workerGroup, final MessageRegistry messageRegistry,
        final ChannelInboundHandlerAdapter channelListener) {
    ServerBootstrap boot = new ServerBootstrap();
    boot.group(bossGroup, workerGroup);//from   w  w  w. jav a2  s .  c om
    boot.channel(NioServerSocketChannel.class);
    boot.option(ChannelOption.SO_BACKLOG, 32);
    boot.childOption(ChannelOption.SO_KEEPALIVE, true);
    boot.childOption(ChannelOption.TCP_NODELAY, true);
    boot.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();

            //p.addLast(new LoggingHandler());

            // encoders
            p.addLast(new LengthFieldPrepender(4));
            p.addLast(new ProtostuffEncoder(messageRegistry));

            // decoders
            p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4));
            p.addLast(new ProtostuffDecoder(messageRegistry));
            p.addLast(channelListener);
        }
    });

    // start accepting connection
    try {
        logger.info("Starting NodeServer on [{}]...", port);

        if (host == null) {
            acceptChannel = boot.bind(port).sync().channel();
        } else {
            acceptChannel = boot.bind(host, port).sync().channel();
        }

        logger.info("NodeServer listening on [{}]...", port);
    } catch (InterruptedException e) {
        logger.error("Binding to port {} failed", port, e);
    }

}

From source file:com.newlandframework.rpc.netty.handler.JdkNativeRecvHandler.java

License:Apache License

public void handle(Map<String, Object> handlerMap, ChannelPipeline pipeline) {
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, MessageCodecUtil.MESSAGE_LENGTH, 0,
            MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new LengthFieldPrepender(MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new MessageRecvHandler(handlerMap));
}

From source file:com.newlandframework.rpc.netty.handler.JdkNativeSendHandler.java

License:Apache License

public void handle(ChannelPipeline pipeline) {
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, MessageCodecUtil.MESSAGE_LENGTH, 0,
            MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new LengthFieldPrepender(MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new MessageSendHandler());
}

From source file:com.nus.mazegame.client.GameClient.java

public static void init(String host, final int port, final String type, SocketAddress localAddr)
        throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override/*w  w w . j a  v a 2s .  c om*/
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            // Decoders
            p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
            p.addLast("bytesDecoder", new ByteArrayDecoder());

            // Encoder
            p.addLast("frameEncoder", new LengthFieldPrepender(4));
            p.addLast("bytesEncoder", new ByteArrayEncoder());
            p.addLast(GameClientHandler.instance);
            GameInfo.instance.setType(type);
            GameInfo.instance.setHostPort(port);
        }
    });

    // Make the connection attempt.
    SocketAddress address = new InetSocketAddress(host, port);
    ChannelFuture f;
    if (localAddr == null)
        f = b.connect(address).sync();
    else
        f = b.connect(address, localAddr).sync();

    // Wait until the connection is closed.
    f.channel().closeFuture().sync();
}