Search in sources :

Example 46 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project jackrabbit-oak by apache.

the class StandbyClient method connect.

void connect(String host, int port) throws Exception {
    final SslContext sslContext;
    if (secure) {
        sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslContext = null;
    }
    Bootstrap b = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, readTimeoutMs).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (sslContext != null) {
                p.addLast(sslContext.newHandler(ch.alloc()));
            }
            p.addLast(new ReadTimeoutHandler(readTimeoutMs, TimeUnit.MILLISECONDS));
            // Decoders
            p.addLast(new SnappyFramedDecoder(true));
            // Such a big max frame length is needed because blob
            // values are sent in one big message. In future
            // versions of the protocol, sending binaries in chunks
            // should be considered instead.
            p.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
            p.addLast(new ResponseDecoder());
            // Encoders
            p.addLast(new StringEncoder(CharsetUtil.UTF_8));
            p.addLast(new GetHeadRequestEncoder());
            p.addLast(new GetSegmentRequestEncoder());
            p.addLast(new GetBlobRequestEncoder());
            p.addLast(new GetReferencesRequestEncoder());
            // Handlers
            p.addLast(new GetHeadResponseHandler(headQueue));
            p.addLast(new GetSegmentResponseHandler(segmentQueue));
            p.addLast(new GetBlobResponseHandler(blobQueue));
            p.addLast(new GetReferencesResponseHandler(referencesQueue));
            // Exception handler
            p.addLast(new ExceptionHandler(clientId));
        }
    });
    channel = b.connect(host, port).sync().channel();
}
Also used : GetReferencesRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetReferencesRequestEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) GetSegmentRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetSegmentRequestEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) ResponseDecoder(org.apache.jackrabbit.oak.segment.standby.codec.ResponseDecoder) GetBlobRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetBlobRequestEncoder) GetHeadRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetHeadRequestEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) SnappyFramedDecoder(io.netty.handler.codec.compression.SnappyFramedDecoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) SslContext(io.netty.handler.ssl.SslContext)

Example 47 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project ProxProx by GoMint.

the class Pipeline method prepare.

public static void prepare(ChannelPipeline pipeline, ConnectionHandler connectionHandler) {
    pipeline.addLast(FRAME_DECODER, new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
    pipeline.addLast(PACKET_DECODER, new Decoder());
    pipeline.addLast(FRAME_PREPENDER, new LengthFieldPrepender(4));
    pipeline.addLast(PACKET_ENCODER, new Encoder());
    pipeline.addLast(CONNECTION_HANDLER, connectionHandler);
}
Also used : LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder)

Example 48 with LengthFieldBasedFrameDecoder

use of 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(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            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 : StringEncoder(io.netty.handler.codec.string.StringEncoder) ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 49 with LengthFieldBasedFrameDecoder

use of 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 = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class).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 = System.currentTimeMillis();
        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);
        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 = (System.currentTimeMillis() - 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) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) 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) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 50 with LengthFieldBasedFrameDecoder

use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project pravega by pravega.

the class PravegaConnectionListener method startListening.

// endregion
public void startListening() {
    // Configure SSL.
    final SslContext sslCtx;
    if (ssl) {
        try {
            sslCtx = SslContextBuilder.forServer(new File(this.certFile), new File(this.keyFile)).build();
        } catch (SSLException e) {
            throw new RuntimeException(e);
        }
    } else {
        sslCtx = null;
    }
    boolean nio = false;
    try {
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup();
    } catch (ExceptionInInitializerError | UnsatisfiedLinkError | NoClassDefFoundError e) {
        nio = true;
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(nio ? NioServerSocketChannel.class : EpollServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (sslCtx != null) {
                SslHandler handler = sslCtx.newHandler(ch.alloc());
                p.addLast(handler);
            }
            ServerConnectionInboundHandler lsh = new ServerConnectionInboundHandler();
            // p.addLast(new LoggingHandler(LogLevel.INFO));
            p.addLast(new ExceptionLoggingHandler(ch.remoteAddress().toString()), new CommandEncoder(null), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new AppendDecoder(), lsh);
            lsh.setRequestProcessor(new AppendProcessor(store, lsh, new PravegaRequestProcessor(store, lsh, statsRecorder, tokenVerifier), statsRecorder, tokenVerifier));
        }
    });
    // Start the server.
    serverChannel = b.bind(host, port).awaitUninterruptibly().channel();
}
Also used : EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) SSLException(javax.net.ssl.SSLException) ExceptionLoggingHandler(io.pravega.shared.protocol.netty.ExceptionLoggingHandler) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) AppendDecoder(io.pravega.shared.protocol.netty.AppendDecoder) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SSLException(javax.net.ssl.SSLException) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) File(java.io.File)

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