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

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

Introduction

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

Prototype

public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, ByteBuf... delimiters) 

Source Link

Document

Creates a new instance.

Usage

From source file:com.barry.netty.ServerInitializer.java

License:Apache License

@Override
public void initChannel(Channel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    byte[] bytes = { Constant.TAIL_BYTE };
    ByteBuf delimiter = Unpooled.wrappedBuffer(bytes);

    pipeline.addLast("logging", new LoggingHandler());

    // ??/*w w  w.  ja va2s .com*/
    pipeline.addLast("decoderContent", new MessageBufferDecoder());

    // ???
    pipeline.addLast("DelimiterDecode", new DelimiterBasedFrameDecoder(Constant.MAX_LEN, false, delimiter));

    // ???
    pipeline.addLast("decoderObj", new MsgDecoder());

    // 
    pipeline.addLast("handler", new ServerHandler());
}

From source file:com.doctor.netty5.example.chatroom.ChatRoomClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {/*  w ww  .ja  v a 2s  .  c  o m*/
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter()));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new ChatRoomClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.chatroom.ChatRoomServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from ww  w. j a v  a 2s  . c o  m
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter()));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new ChatRoomServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(ChatRoomServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

        channelFuture.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.github.nettybook.ch7.junit.DelimiterBasedFrameDecoderTest.java

License:Apache License

@Test
public void testDecoder() {
    String writeData = "\r\n\r\n";
    String firstResponse = "\r\n";
    String secondResponse = "\r\n";

    DelimiterBasedFrameDecoder decoder = new DelimiterBasedFrameDecoder(8192, false,
            Delimiters.lineDelimiter());
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(decoder);

    ByteBuf request = Unpooled.wrappedBuffer(writeData.getBytes());
    boolean result = embeddedChannel.writeInbound(request);
    assertTrue(result);/*from www . j a  va2 s. c  o m*/

    ByteBuf response = null;

    response = (ByteBuf) embeddedChannel.readInbound();
    assertEquals(firstResponse, response.toString(Charset.defaultCharset()));

    response = (ByteBuf) embeddedChannel.readInbound();
    assertEquals(secondResponse, response.toString(Charset.defaultCharset()));

    embeddedChannel.finish();
}

From source file:com.smart.fulfilcamel.HxClientChannelPipelineFactory.java

@Override
protected void initChannel(Channel ch) throws Exception {

    ChannelPipeline channelPipeline = ch.pipeline();
    channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.US_ASCII));
    channelPipeline.addLast("decoder-DELIM",
            new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
    channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.US_ASCII));
    channelPipeline.addLast("handler", new ClientChannelHandler(producer));
}

From source file:com.smart.fulfilcamel.hxServerInitialFactory.java

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

    channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));
    channelPipeline.addLast("decoder-DELIM",
            new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
    channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
    // here we add the default Camel ServerChannelHandler for the consumer, to allow Camel to route the message etc.
    channelPipeline.addLast("handler", new ServerChannelHandler(consumer));
}

From source file:com.smart.smartpayg.cgbBank.cgbInitializerFactory.CGBClientChannelPipelineFactory.java

@Override
protected void initChannel(Channel c) throws Exception {
    ChannelPipeline channelPipeline = c.pipeline();
    channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.US_ASCII));
    channelPipeline.addLast("decoder-DELIM",
            new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
    channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.US_ASCII));
    channelPipeline.addLast("handler", new ClientChannelHandler(producer));
}

From source file:com.smart.smartpayg.cgbBank.cgbInitializerFactory.CGBServerInitalFactory.java

@Override
protected void initChannel(Channel c) throws Exception {
    ChannelPipeline channelPipeline = c.pipeline();
    channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.US_ASCII));
    channelPipeline.addLast("decoder-DELIM",
            new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
    channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.US_ASCII));
    // here we add the default Camel ServerChannelHandler for the consumer, to allow Camel to route the message etc.
    channelPipeline.addLast("handler", new ServerChannelHandler(consumer));
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TCPServerSource.java

License:Apache License

private DelimiterBasedFrameDecoder buildDelimiterBasedFrameDecoder(String recordSeparatorStr) {
    final byte[] delimiterBytes = StringEscapeUtils.unescapeJava(recordSeparatorStr).getBytes();
    return new DelimiterBasedFrameDecoder(config.maxMessageSize, true, Unpooled.copiedBuffer(delimiterBytes));
}

From source file:com.zhuika.discard.DiscardClient.java

License:Apache License

private static void client(final String[] args, final byte[] byteMsg) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from w w  w.jav  a2 s . co m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("framer", new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter()));
                p.addLast(new ByteToMessageDec());
                p.addLast(new MessageToByteEnc());

                if (byteMsg != null && byteMsg.length > 0) {
                    p.addLast(new DiscardClientHandler(byteMsg));
                } else {
                    p.addLast(new DiscardClientHandler(args[2]));
                }
            }
        });
        final ChannelFuture f = b.connect(args[0], Integer.valueOf(args[1])).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}