Search in sources :

Example 31 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project pravega by pravega.

the class AppendProcessorTest method createChannel.

private EmbeddedChannel createChannel(StreamSegmentStore store) {
    ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(new ExceptionLoggingHandler(""), new CommandEncoder(null, MetricNotifier.NO_OP_METRIC_NOTIFIER), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new AppendDecoder(), lsh);
    lsh.setRequestProcessor(AppendProcessor.defaultBuilder().store(store).connection(new TrackedConnection(lsh)).nextRequestProcessor(new PravegaRequestProcessor(store, mock(TableStore.class), lsh)).build());
    return channel;
}
Also used : AppendDecoder(io.pravega.shared.protocol.netty.AppendDecoder) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 32 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project baseio by generallycloud.

the class NettyClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = NettyUtil.newEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
                pipeline.addLast("handler", new HelloClient());
            }
        });
        System.out.println("################## Test start ####################");
        long old = Util.now_f();
        ChannelFuture f = b.connect("127.0.0.1", 8300).sync();
        System.out.println(f.isSuccess());
        AttributeKey<String> key = AttributeKey.valueOf("test");
        Channel channel = f.channel();
        channel.attr(key).set("999999999999");
        System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
        for (int i = 0; i < time; i++) {
            String s = "hello Service! ---> :" + i;
            ChannelFuture f1 = channel.writeAndFlush(s);
            f1.isDone();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long spend = Util.past(old);
        System.out.println("## Execute Time:" + time);
        System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
        System.out.println("## Expend Time:" + spend);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ChannelPipeline(io.netty.channel.ChannelPipeline) BigDecimal(java.math.BigDecimal) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 33 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project baseio by generallycloud.

the class TestLoadEchoClient1 method prepare.

@Override
public void prepare() throws Exception {
    eventHandleAdaptor = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            addCount(1024);
        }
    };
    Bootstrap b = new Bootstrap();
    b.group(group);
    b.channel(NettyUtil.newSocketChannel());
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
            pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast("handler", eventHandleAdaptor);
        }
    });
    f = b.connect("localhost", 8300).sync();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 34 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project baseio by generallycloud.

the class MyNettyServer method service.

public static void service() throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup);
    bootstrap.channel(NettyUtil.newServerSocketChannel());
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new IdleStateHandler(5, 10, 20));
            pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast(new LengthFieldPrepender(4));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new TcpServerHandler());
        }
    });
    ChannelFuture f = bootstrap.bind(IP, PORT).sync();
    f.channel().closeFuture().sync();
    System.out.println("TCP服务器已启动");
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 35 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project baseio by generallycloud.

the class NettyClientThread method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = NettyUtil.newEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
                pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
                pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
                pipeline.addLast("handler", new HelloClient());
            }
        });
        System.out.println("################## Test start ####################");
        ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
        System.out.println(f.isSuccess());
        Channel channel = f.channel();
        System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
        int len = 1024 * 64;
        StringBuilder s = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            s.append(len % 10);
        }
        final String msg = s.toString();
        Util.exec(() -> {
            int i = 0;
            for (; ; ) {
                // String s = "hello Service! ---> :" + i;
                ChannelFuture f1 = channel.writeAndFlush(msg);
                Util.sleep(1);
                System.out.println(f1.isDone() + "--------" + i);
                i++;
            }
        });
        Util.sleep(Integer.MAX_VALUE);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Aggregations

LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)71 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)37 SocketChannel (io.netty.channel.socket.SocketChannel)35 ChannelPipeline (io.netty.channel.ChannelPipeline)31 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)25 Bootstrap (io.netty.bootstrap.Bootstrap)20 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)20 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)19 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)18 Channel (io.netty.channel.Channel)14 ChannelFuture (io.netty.channel.ChannelFuture)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 StringEncoder (io.netty.handler.codec.string.StringEncoder)13 SslContext (io.netty.handler.ssl.SslContext)13 StringDecoder (io.netty.handler.codec.string.StringDecoder)12 IOException (java.io.IOException)11 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)9 CommandDecoder (io.pravega.shared.protocol.netty.CommandDecoder)8 CommandEncoder (io.pravega.shared.protocol.netty.CommandEncoder)8 ExceptionLoggingHandler (io.pravega.shared.protocol.netty.ExceptionLoggingHandler)7