Search in sources :

Example 6 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder 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 StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project netty by netty.

the class RxtxClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {

            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler());
            }
        });
        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxDeviceAddress(io.netty.channel.rxtx.RxtxDeviceAddress) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxChannel(io.netty.channel.rxtx.RxtxChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 8 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project netty by netty.

the class SocketStartTlsTest method testStartTls.

private void testStartTls(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    final EventExecutorGroup executor = SocketStartTlsTest.executor;
    SSLEngine sse = serverCtx.newEngine(PooledByteBufAllocator.DEFAULT);
    SSLEngine cse = clientCtx.newEngine(PooledByteBufAllocator.DEFAULT);
    final StartTlsServerHandler sh = new StartTlsServerHandler(sse, autoRead);
    final StartTlsClientHandler ch = new StartTlsClientHandler(cse, autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast("logger", new LoggingHandler(LOG_LEVEL));
            p.addLast(new LineBasedFrameDecoder(64), new StringDecoder(), new StringEncoder());
            p.addLast(executor, sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast("logger", new LoggingHandler(LOG_LEVEL));
            p.addLast(new LineBasedFrameDecoder(64), new StringDecoder(), new StringEncoder());
            p.addLast(executor, ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    while (cc.isActive()) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (sh.channel.isActive()) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().awaitUninterruptibly();
    cc.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    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 : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) Channel(io.netty.channel.Channel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) IOException(java.io.IOException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder)

Example 9 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder 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 10 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project camel by apache.

the class MultipleCodecsTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    // START SNIPPET: registry-beans
    ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
    StringDecoder stringDecoder = new StringDecoder();
    registry.bind("length-decoder", lengthDecoder);
    registry.bind("string-decoder", stringDecoder);
    LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
    StringEncoder stringEncoder = new StringEncoder();
    registry.bind("length-encoder", lengthEncoder);
    registry.bind("string-encoder", stringEncoder);
    List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
    decoders.add(lengthDecoder);
    decoders.add(stringDecoder);
    List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
    encoders.add(lengthEncoder);
    encoders.add(stringEncoder);
    registry.bind("encoders", encoders);
    registry.bind("decoders", decoders);
    // END SNIPPET: registry-beans
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) StringEncoder(io.netty.handler.codec.string.StringEncoder) ArrayList(java.util.ArrayList) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)14 StringDecoder (io.netty.handler.codec.string.StringDecoder)12 ChannelPipeline (io.netty.channel.ChannelPipeline)6 Channel (io.netty.channel.Channel)4 ChannelHandler (io.netty.channel.ChannelHandler)4 SocketChannel (io.netty.channel.socket.SocketChannel)4 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)4 ChannelFuture (io.netty.channel.ChannelFuture)3 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)3 JndiRegistry (org.apache.camel.impl.JndiRegistry)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 SslContext (io.netty.handler.ssl.SslContext)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ShutdownArgs (com.github.dreamhead.moco.bootstrap.ShutdownArgs)1