Search in sources :

Example 21 with StringEncoder

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

the class HelloClientInitializer method initChannel.

@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());
}
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 22 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.

the class ProxyTest method testCloseOnBothSide.

@Test
public void testCloseOnBothSide() throws Exception {
    serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new StringEncoder());
            ch.pipeline().addLast(new StringDecoder());
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    logger.info("[channelRead] msg: {}", msg);
                    super.channelRead(ctx, msg);
                }

                @Override
                public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                    logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
                    super.channelUnregistered(ctx);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
                    super.channelInactive(ctx);
                }
            });
        }
    }).bind(8009).sync();
    ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
        }
    });
    future.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            future.channel().close().sync();
        }
    });
    Thread.sleep(1000 * 3);
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) SocketChannel(io.netty.channel.socket.SocketChannel) SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) StringDecoder(io.netty.handler.codec.string.StringDecoder) Test(org.junit.Test)

Example 23 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.

the class ProxyTest method testSendFrequently.

@Test
public void testSendFrequently() throws InterruptedException {
    AtomicInteger counter = new AtomicInteger(0);
    int N = 10000;
    serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new StringEncoder());
            ch.pipeline().addLast(new StringDecoder());
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    Assert.assertTrue(msg instanceof String);
                    String message = (String) msg;
                    Assert.assertTrue(message.toLowerCase().contains("ok") || message.toLowerCase().contains("proxy"));
                    if (message.toLowerCase().contains("ok")) {
                        String[] strs = message.split("\r\n");
                        for (String str : strs) {
                            if (str.toLowerCase().contains("ok")) {
                                counter.getAndIncrement();
                            }
                        }
                    }
                    super.channelRead(ctx, msg);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    // Assert.assertEquals(N, counter.get());
                    logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
                    System.out.println("Complete" + counter.get());
                    super.channelInactive(ctx);
                }
            });
        }
    }).bind(8009).sync();
    ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
            Thread.sleep(1);
            for (int i = 0; i < N; i++) {
                future.channel().writeAndFlush(new SimpleStringParser("OK").format());
            }
        }
    });
    Thread.sleep(1000 * 10);
    logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) StringDecoder(io.netty.handler.codec.string.StringDecoder) StringEncoder(io.netty.handler.codec.string.StringEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 24 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.

the class ProxyTest method testCloseOnBothSide2.

@Test
public void testCloseOnBothSide2() throws Exception {
    serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new StringEncoder());
            ch.pipeline().addLast(new StringDecoder());
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    logger.info("[channelRead] msg: {}", msg);
                    ctx.channel().close();
                    super.channelRead(ctx, msg);
                }

                @Override
                public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                    logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
                    super.channelUnregistered(ctx);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
                    super.channelInactive(ctx);
                }
            });
        }
    }).bind(8009).sync();
    ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
        }
    });
    future.channel().closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            logger.info("[close] close future");
        }
    });
    Thread.sleep(1000 * 3);
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) SocketChannel(io.netty.channel.socket.SocketChannel) SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) StringDecoder(io.netty.handler.codec.string.StringDecoder) Test(org.junit.Test)

Example 25 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.

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(createSslHandler(initSSLContext()));
    // 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)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)119 StringDecoder (io.netty.handler.codec.string.StringDecoder)109 ChannelPipeline (io.netty.channel.ChannelPipeline)73 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)40 SocketChannel (io.netty.channel.socket.SocketChannel)40 ChannelFuture (io.netty.channel.ChannelFuture)31 Bootstrap (io.netty.bootstrap.Bootstrap)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)28 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)27 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)25 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)24 EventLoopGroup (io.netty.channel.EventLoopGroup)23 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)23 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)17 Channel (io.netty.channel.Channel)16 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)16 Test (org.junit.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)15 NettyServerAndClient (org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient)15 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)15