Search in sources :

Example 51 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project force-oneself by Force-oneself.

the class EventLoopClient method main.

public static void main(String[] args) throws InterruptedException {
    ChannelFuture channelFuture = new Bootstrap().group(new NioEventLoopGroup()).channel(NioSocketChannel.class).handler(new ChannelInitializer<NioSocketChannel>() {

        @Override
        protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
            nioSocketChannel.pipeline().addLast(new StringEncoder());
        }
    }).connect(new InetSocketAddress("localhost", 8080)).channel().writeAndFlush("Hello, World");
    // 处理回调
    channelFuture.addListener((ChannelFutureListener) channelFuture1 -> {
        Channel channel = channelFuture.channel();
        channel.writeAndFlush("hello world");
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StringEncoder(io.netty.handler.codec.string.StringEncoder) InetSocketAddress(java.net.InetSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 52 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 53 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 54 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 55 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)107 ChannelPipeline (io.netty.channel.ChannelPipeline)70 SocketChannel (io.netty.channel.socket.SocketChannel)35 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)33 ChannelFuture (io.netty.channel.ChannelFuture)32 Bootstrap (io.netty.bootstrap.Bootstrap)27 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)26 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)25 EventLoopGroup (io.netty.channel.EventLoopGroup)24 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)24 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)20 Channel (io.netty.channel.Channel)20 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)20 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)17 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)17 Test (org.junit.Test)17 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