Java 类io.netty.handler.codec.DelimiterBasedFrameDecoder 实例源码

项目:neto    文件:ProtocolUnificationHandler.java   
private void switchToBinary(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new DelimiterBasedFrameDecoder(maxFrameLength, Delimiters.lineDelimiter()));
    p.addLast(new StringDecoder(Charset.forName(charset)));
    p.addLast(new NetoJsonToMessageDecoder(opcodeMap));

    NetoMessageToJsonEncoder netoMessageToJsonEncoder = new NetoMessageToJsonEncoder();
    netoMessageToJsonEncoder.setOpcodeMap(opcodeMap);

    p.addLast(netoMessageToJsonEncoder);
    p.addLast(new MessageHandler(redisService));
    p.remove(this);


    // 핸들러를 다시 등록 했으므로 이벤트를 전파
    ctx.fireChannelActive();
}
项目:neto    文件:ChannelServerInitializer.java   
@Override
protected void initChannel(SocketChannel ch) throws Exception {

    NetoJsonToMessageDecoder decoder = new NetoJsonToMessageDecoder(opcodeMap);
    NetoMessageToJsonEncoder encoder = new NetoMessageToJsonEncoder();

    if (opcodeMap instanceof BiMap) {
        encoder.setOpcodeMap((BiMap<Integer, Class<? extends NetoJsonMessage>>) opcodeMap);
    }

    MessageHandler handler = new MessageHandler(redisService);

    ChannelPipeline p = ch.pipeline();

    p.addLast(new DelimiterBasedFrameDecoder(maxFrameLength, Delimiters.lineDelimiter()));
    p.addLast(new StringDecoder(Charset.forName(charset)));
    p.addLast(decoder);
    p.addLast(encoder);
    p.addLast(handler);
}
项目:neto    文件:SecureChatClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new LoggingHandler());
    // 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.
    if (sslCtx != null)
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, SecureChatClient.PORT));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}
项目:waterrower-core    文件:TestRxtxChannelInitializer.java   
@Test
public void initChannel_configuresSerialChannel() throws Exception {
    when(channel.config()).thenReturn(config);
    when(config.getBaudrate()).thenReturn(19200);
    when(config.getDatabits()).thenReturn(DATABITS_8);
    when(config.getStopbits()).thenReturn(STOPBITS_1);
    when(config.getParitybit()).thenReturn(NONE);
    when(channel.pipeline()).thenReturn(pipeline);
    initializer.setRxTxSerialHandler(handler);

    initializer.initChannel(channel);

    // Check if serial config is correct:
    verify(config, times(1)).setBaudrate(19200);
    verify(config, times(1)).setDatabits(DATABITS_8);
    verify(config, times(1)).setStopbits(STOPBITS_1);
    verify(config, times(1)).setParitybit(NONE);

    // Check if encoders, decoders and handlers are set:
    verify(pipeline, times(1)).addLast(eq("framer"), any(DelimiterBasedFrameDecoder.class));
    verify(pipeline, times(1)).addLast(eq("decoder"), any(RxtxMessageFrameDecoder.class));
    verify(pipeline, times(1)).addLast(eq("encoder"), any(RxtxMessageFrameEncoder.class));
    verify(pipeline, times(1)).addLast(eq("handler"), eq(handler));
}
项目:raft-java    文件:StartServer.java   
@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ch.config().setAllowHalfClosure(true);
    ChannelPipeline pipeline = ch.pipeline();

    //IdleStateHandler 与客户端链接后,根据超出配置的时间自动触发userEventTriggered
    //readerIdleTime服务端长时间没有读到数据,则为读空闲,触发读空闲监听,并自动关闭链路连接,周期性按readerIdleTime的超时间触发空闲监听方法
    //writerIdleTime服务端长时间没有发送写请求,则为空闲,触发写空闲监听,空闲期间,周期性按writerIdleTime的超时间触发空闲监听方法
    //allIdleTime 服务端在allIdleTime时间内未接收到客户端消息,或者,也未去向客户端发送消息,则触发周期性操作
    pipeline.addLast("ping", new IdleStateHandler(10, 20, 35, TimeUnit.SECONDS));
    // 以("\n")为结尾分割的 解码器
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 自己的逻辑Handler
    pipeline.addLast("handler", new DataServerHandler(nodeInfo));
}
项目:raft-java    文件:StartServer.java   
@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ch.config().setAllowHalfClosure(true);
    ChannelPipeline pipeline = ch.pipeline();

    //IdleStateHandler 与客户端链接后,根据超出配置的时间自动触发userEventTriggered
    //readerIdleTime服务端长时间没有读到数据,则为读空闲,触发读空闲监听,并自动关闭链路连接,周期性按readerIdleTime的超时间触发空闲监听方法
    //writerIdleTime服务端长时间没有发送写请求,则为空闲,触发写空闲监听,空闲期间,周期性按writerIdleTime的超时间触发空闲监听方法
    //allIdleTime 服务端在allIdleTime时间内未接收到客户端消息,或者,也未去向客户端发送消息,则触发周期性操作
    pipeline.addLast("ping", new IdleStateHandler(10, 20, 35, TimeUnit.SECONDS));
    // 以("\n")为结尾分割的 解码器
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 自己的逻辑Handler
    pipeline.addLast("handler", new ElectionServerHandler(nodeInfo));
}
项目:bookish-meme    文件:ChatServerInitializer.java   
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline pipeline = socketChannel.pipeline();
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()))
            .addLast("decoder", new StringDecoder())
            .addLast("encoder", new StringEncoder())
            .addLast("json_to_ob",new JsonToObjectHandler())
            .addLast("register",new RegisterHandler())
            .addLast("authority", new AuthorityHandler())
            .addLast("enterGroup",new EnterGroupHandler())
            .addLast("channelManager", new ChannelManagerHandler())
            .addLast("createGroup", new CreateGroupHandler())
            .addLast("addGroup", new AddGroupHandler())
            .addLast("deleteGroup",new DeleteGroupHandler())
            .addLast("Limiter", new LimiterHandler())
            .addLast("log", new LoggerHandler())
            .addLast("response", new Responser());
}
项目:JavaAyo    文件:HelloClientInitializer.java   
@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    /*
     * 这个地方的 必须和服务端对应上。否则无法正常解码和编码
     * 
     * 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
     */
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
            Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // 客户端的逻辑
    pipeline.addLast("handler", new HelloClientHandler());
}
项目:JavaAyo    文件:SecureChatServerInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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 StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatServerHandler());
}
项目:JavaAyo    文件:SecureChatClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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(), SecureChatClient.HOST, SecureChatClient.PORT));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}
项目:JavaAyo    文件:TelnetServerInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(SERVER_HANDLER);
}
项目:JavaAyo    文件:TelnetClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), TelnetClient.HOST, TelnetClient.PORT));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(CLIENT_HANDLER);
}
项目:mintds    文件:NettyServerInitializer.java   
@Override
protected void initChannel(final SocketChannel ch) throws Exception {

    final ChannelPipeline pipeline = ch.pipeline();

    // decoders
    // Add the text line codec combination first,
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("stringDecoder", stringDecoder);
    pipeline.addLast("requestDecoder", requestDecoder);

    // encoders
    pipeline.addLast("stringEncoder", stringEncoder);
    pipeline.addLast("responseEncoder", responseEncoder);


    // business logic handler
    pipeline.addLast(group, "serverHandler", serverHandler);
    pipeline.addLast("exceptionHandler", exceptionHandler);

}
项目:netty4.0.27Learn    文件:SecureChatServerInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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 StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatServerHandler());
}
项目:netty4.0.27Learn    文件:SecureChatClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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(), SecureChatClient.HOST, SecureChatClient.PORT));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatClientHandler());
}
项目:netty4.0.27Learn    文件:TelnetServerInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(SERVER_HANDLER);
}
项目:netty4.0.27Learn    文件:TelnetClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), TelnetClient.HOST, TelnetClient.PORT));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(CLIENT_HANDLER);
}
项目:netty4.0.27Learn    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, true, false, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 }));
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:netty4.0.27Learn    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:netty-storm    文件:NettySpoutServerInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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(8*8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new NettySpoutServerHandler(spout));
}
项目:netty-storm    文件:NettyConnectionInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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(), host, port));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new NettyConnectionHandler());
}
项目:netty.book.kor    文件:DelimiterBasedFrameDecoderTest.java   
@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);

    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();
}
项目:remote-procedure-call    文件:ClientChannelInitializer.java   
@Override
protected void initChannel(SocketChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();

    // use the IdleStateHandler to get notified if you haven't received or sent data for dozens of seconds.
    // If this is the case, a heartbeat will be written to the remote peer, and if this fails the connection is closed.
    pipeline.addLast(this.executorGroup, "idleStateHandler", new IdleStateHandler(0, 0, Constants.HEARTBEAT_PERIOD, TimeUnit.SECONDS));
    pipeline.addLast(this.executorGroup, "heartbeatHandler", heartbeatHandler);

    if (this.compression) {
        // Enable stream compression
        pipeline.addLast(this.executorGroup, "deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        pipeline.addLast(this.executorGroup, "inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    // NUL (0x00) is a message delimiter
    pipeline.addLast(this.executorGroup, "framer", new DelimiterBasedFrameDecoder(8192, Delimiters.nulDelimiter()));

    // string encoder / decoder are responsible for encoding / decoding an UTF-8 string
    pipeline.addLast(this.executorGroup, "encoder", utf8Encoder);
    pipeline.addLast(this.executorGroup, "decoder", utf8Decoder);

    // client hander is responsible for as a remoting call stub
    pipeline.addLast(this.executorGroup, "clientHandler", clientHandler);
}
项目:remote-procedure-call    文件:ServerChannelInitializer.java   
@Override
protected void initChannel(SocketChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();

    if (this.compression) {
        // Enable stream compression
        pipeline.addLast(this.executorGroup, "deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        pipeline.addLast(this.executorGroup, "inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    // NUL (0x00) is a message delimiter
    pipeline.addLast(this.executorGroup, "framer", new DelimiterBasedFrameDecoder(8192, Delimiters.nulDelimiter()));

    // string encoder / decoder are responsible for encoding / decoding an UTF-8 string
    pipeline.addLast(this.executorGroup, "encoder", utf8Encoder);
    pipeline.addLast(this.executorGroup, "decoder", utf8Decoder);

    // server hander is responsible for as a remoting call skeleton
    pipeline.addLast(this.executorGroup, "serverHandler", serverHandler);
}
项目:javase-study    文件:DelimitedChient.java   
public void run(String host, int port) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .remoteAddress(host, port)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new DelimiterBasedFrameDecoder(20, Delimiters.lineDelimiter()))
                                    .addLast(new StringDecoder())
                                    .addLast(new DelimitedClientHandler());
                        }
                    });

            ChannelFuture cf = bootstrap.connect().sync();
            cf.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
项目:javase-study    文件:DelimitedChient.java   
public void run(String host, int port) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .remoteAddress(host, port)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new DelimiterBasedFrameDecoder(10, Unpooled.copiedBuffer(" ", CharsetUtil.UTF_8)))
                                    .addLast(new StringDecoder())
                                    .addLast(new DelimitedClientHandler());
                        }
                    });

            ChannelFuture cf = bootstrap.connect().sync();
            cf.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
项目:netty4study    文件:SecureChatClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.

    SSLEngine engine =
        SecureChatSslContextFactory.getClientContext().createSSLEngine();
    engine.setUseClientMode(true);

    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
            8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // and then business logic.
    pipeline.addLast("handler", new SecureChatClientHandler());
}
项目:netty4study    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, true, false, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 }));
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:netty4study    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:aj8    文件:JagGrabChannelHandler.java   
@Override
protected void initChannel(SocketChannel channel) {
    ChannelPipeline pipeline = channel.pipeline();

    // decoders
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(MAX_REQUEST_LENGTH, DOUBLE_LINE_FEED_DELIMITER));
    pipeline.addLast("string-decoder", new StringDecoder(JAGGRAB_CHARSET));
    pipeline.addLast("jaggrab-decoder", new JagGrabRequestDecoder());

    // encoders
    pipeline.addLast("jaggrab-encoder", new JagGrabResponseEncoder());

    // handler
    pipeline.addLast("timeout", new IdleStateHandler(NetworkConstants.IDLE_TIME, 0, 0));
    pipeline.addLast("handler", handler);
}
项目:aj8    文件:JagGrabChannelHandler.java   
@Override
protected void initChannel(SocketChannel channel) {
    ChannelPipeline pipeline = channel.pipeline();

    // decoders
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(MAX_REQUEST_LENGTH, DOUBLE_LINE_FEED_DELIMITER));
    pipeline.addLast("string-decoder", new StringDecoder(JAGGRAB_CHARSET));
    pipeline.addLast("jaggrab-decoder", new JagGrabRequestDecoder());

    // encoders
    pipeline.addLast("jaggrab-encoder", new JagGrabResponseEncoder());

    // handler
    pipeline.addLast("timeout", new IdleStateHandler(NetworkConstants.IDLE_TIME, 0, 0));
    pipeline.addLast("handler", handler);
}
项目:disthene    文件:CarbonServer.java   
public void run() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 100)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new DelimiterBasedFrameDecoder(MAX_FRAME_LENGTH, false, Delimiters.lineDelimiter()));
                    p.addLast(new CarbonServerHandler(bus, configuration.getCarbon().getBaseRollup()));
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    logger.error(cause);
                    super.exceptionCaught(ctx, cause);
                }
            });

    // Start the server.
    b.bind(configuration.getCarbon().getBind(), configuration.getCarbon().getPort()).sync();
}
项目:netty-netty-5.0.0.Alpha1    文件:SecureChatClientInitializer.java   
@Override
public void initChannel(SocketChannel ch) throws Exception {
    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.

    SSLEngine engine =
        SecureChatSslContextFactory.getClientContext().createSSLEngine();
    engine.setUseClientMode(true);

    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
            8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // and then business logic.
    pipeline.addLast("handler", new SecureChatClientHandler());
}
项目:netty-netty-5.0.0.Alpha1    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, true, false, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 }));
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:netty-netty-5.0.0.Alpha1    文件:DelimiterBasedFrameDecoderTest.java   
@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(
            new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()));

    for (int i = 0; i < 2; i ++) {
        try {
            assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 1, 2 })));
            fail(DecoderException.class.getSimpleName() + " must be raised.");
        } catch (TooLongFrameException e) {
            // Expected
        }

        ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 'A', 0 }));
        ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
        assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
    }
}
项目:whale    文件:IotNettyTcpServerInitializer.java   
public void initChannel(AbstractChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(iotNettyTcpHandler);
}
项目:nutzWx    文件:IotServerInitializer.java   
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(iotServerHandler);
}
项目:micrometer    文件:CacheSample.java   
/** skip things that aren't words, roughly **/
private static DelimiterBasedFrameDecoder wordDecoder() {
    return new DelimiterBasedFrameDecoder(256,
        IntStream.of('\r', '\n', ' ', '\t', '.', ',', ';', ':', '-')
            .mapToObj(delim -> wrappedBuffer(new byte[] { (byte) delim }))
            .toArray(ByteBuf[]::new));
}
项目:netty_op    文件:TimeServer.java   
@Override
protected void initChannel(SocketChannel ch) throws Exception {
    //使用'\r\n'作为输入分隔符
    ByteBuf byteBuf = Delimiters.lineDelimiter()[0];
    ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, byteBuf));

    //对输入数据进行字符串解码
    ch.pipeline().addLast(new StringDecoder());

    //对输入数据进行业务逻辑处理
    ch.pipeline().addLast(new TimeServerHandler());

    //对输出数据进行字符串编码
    ch.pipeline().addLast(new StringEncoder());
}
项目:L2J-Global    文件:TelnetServerInitializer.java   
@Override
public void initChannel(SocketChannel ch)
{
    final ChannelPipeline pipeline = ch.pipeline();

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);
    pipeline.addLast(HANDLER);
}