Search in sources :

Example 6 with DelimiterBasedFrameDecoder

use of io.netty.handler.codec.DelimiterBasedFrameDecoder in project netty by netty.

the class SecureChatServerInitializer method initChannel.

@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());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 7 with DelimiterBasedFrameDecoder

use of io.netty.handler.codec.DelimiterBasedFrameDecoder in project netty by netty.

the class TelnetServerInitializer method initChannel.

@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);
}
Also used : DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 8 with DelimiterBasedFrameDecoder

use of io.netty.handler.codec.DelimiterBasedFrameDecoder in project netty by netty.

the class SocketStringEchoTest method testStringEcho.

private static void testStringEcho(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    final StringEchoHandler sh = new StringEchoHandler(autoRead);
    final StringEchoHandler ch = new StringEchoHandler(autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
            sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addAfter("decoder", "handler", sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
            sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addAfter("decoder", "handler", ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    for (String element : data) {
        String delimiter = random.nextBoolean() ? "\r\n" : "\n";
        cc.writeAndFlush(element + delimiter);
    }
    while (ch.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (sh.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().sync();
    ch.channel.close().sync();
    sc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) Channel(io.netty.channel.Channel) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 9 with DelimiterBasedFrameDecoder

use of io.netty.handler.codec.DelimiterBasedFrameDecoder in project newts by OpenNMS.

the class GraphiteInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 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(new GraphiteHandler(m_repository, this));
}
Also used : DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)9 ChannelPipeline (io.netty.channel.ChannelPipeline)5 StringDecoder (io.netty.handler.codec.string.StringDecoder)4 StringEncoder (io.netty.handler.codec.string.StringEncoder)4 ByteBuf (io.netty.buffer.ByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)2 Test (org.junit.Test)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 Base64Decoder (io.netty.handler.codec.base64.Base64Decoder)1 Base64Encoder (io.netty.handler.codec.base64.Base64Encoder)1 IOException (java.io.IOException)1 EncryptionManager (jgnash.util.EncryptionManager)1