Search in sources :

Example 56 with LengthFieldBasedFrameDecoder

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

the class ConnectionPoolingTest method setUp.

@Before
public void setUp() throws Exception {
    // Configure SSL.
    port = TestUtils.getAvailableListenPort();
    final SslContext sslCtx;
    if (ssl) {
        try {
            sslCtx = SslContextBuilder.forServer(new File(SecurityConfigDefaults.TLS_SERVER_CERT_PATH), new File(SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_PATH)).build();
        } catch (SSLException e) {
            throw new RuntimeException(e);
        }
    } else {
        sslCtx = null;
    }
    boolean nio = false;
    EventLoopGroup bossGroup;
    EventLoopGroup workerGroup;
    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());
                SSLEngine sslEngine = handler.engine();
                SSLParameters sslParameters = sslEngine.getSSLParameters();
                sslParameters.setEndpointIdentificationAlgorithm("LDAPS");
                sslEngine.setSSLParameters(sslParameters);
                p.addLast(handler);
            }
            p.addLast(new CommandEncoder(null, NO_OP_METRIC_NOTIFIER), new LengthFieldBasedFrameDecoder(MAX_WIRECOMMAND_SIZE, 4, 4), new CommandDecoder(), new EchoServerHandler());
        }
    });
    // Start the server.
    serverChannel = b.bind("localhost", port).awaitUninterruptibly().channel();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) CommandEncoder(io.pravega.shared.protocol.netty.CommandEncoder) SSLException(javax.net.ssl.SSLException) SSLParameters(javax.net.ssl.SSLParameters) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) CommandDecoder(io.pravega.shared.protocol.netty.CommandDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SSLException(javax.net.ssl.SSLException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) File(java.io.File) Before(org.junit.Before)

Example 57 with LengthFieldBasedFrameDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.LengthFieldBasedFrameDecoder in project web3sdk by FISCO-BCOS.

the class ChannelConnections method startConnect.

public void startConnect() throws Exception {
    if (running) {
        logger.debug("running");
        return;
    }
    logger.debug(" start connect. ");
    // init netty
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    bootstrap.group(workerGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    // set connect timeout
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout);
    final ChannelConnections selfService = this;
    final ThreadPoolTaskExecutor selfThreadPool = threadPool;
    SslContext sslContext = (EncryptType.encryptType == EncryptType.ECDSA_TYPE) ? initSslContext() : initSMSslContext();
    SslContext finalSslContext = sslContext;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            /*
                         * Each connection is fetched from the socketChannel, using the new
                         * handler connection information
                         */
            ChannelHandler handler = new ChannelHandler();
            handler.setConnections(selfService);
            handler.setThreadPool(selfThreadPool);
            SslHandler sslHandler = finalSslContext.newHandler(ch.alloc());
            /**
             * set ssl handshake timeout
             */
            sslHandler.setHandshakeTimeoutMillis(sslHandShakeTimeout);
            ch.pipeline().addLast(sslHandler, new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, -4, 0), new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS), handler);
        }
    });
    List<Tuple3<String, Integer, ChannelFuture>> tuple3List = new ArrayList<>();
    // try to connect to all nodes
    for (ConnectionInfo connectionInfo : connections) {
        String IP = connectionInfo.getHost();
        Integer port = connectionInfo.getPort();
        ChannelFuture channelFuture = bootstrap.connect(IP, port);
        tuple3List.add(new Tuple3<>(IP, port, channelFuture));
    }
    boolean atLeastOneConnectSuccess = false;
    List<String> errorMessageList = new ArrayList<>();
    // Wait for all connection operations to complete
    for (Tuple3<String, Integer, ChannelFuture> tuple3 : tuple3List) {
        ChannelFuture connectFuture = tuple3.getValue3().awaitUninterruptibly();
        if (!connectFuture.isSuccess()) {
            logger.error(" connect to {}:{}, error: {}", tuple3.getValue1(), tuple3.getValue2(), connectFuture.cause().getMessage());
            String connectFailedMessage = Objects.isNull(connectFuture.cause()) ? "connect to " + tuple3.getValue1() + ":" + tuple3.getValue2() + " failed" : connectFuture.cause().getMessage();
            errorMessageList.add(connectFailedMessage);
        } else {
            // tcp connect success and waiting for SSL handshake
            logger.trace(" connect to {}:{} success", tuple3.getValue1(), tuple3.getValue2());
            SslHandler sslhandler = connectFuture.channel().pipeline().get(SslHandler.class);
            if (Objects.isNull(sslhandler)) {
                String sslHandshakeFailedMessage = " ssl handshake failed:/" + tuple3.getValue1() + ":" + tuple3.getValue2();
                logger.debug(" SslHandler is null, host: {}, port: {}", tuple3.getValue1(), tuple3.getValue2());
                errorMessageList.add(sslHandshakeFailedMessage);
                continue;
            }
            Future<Channel> sshHandshakeFuture = sslhandler.handshakeFuture().awaitUninterruptibly();
            if (sshHandshakeFuture.isSuccess()) {
                atLeastOneConnectSuccess = true;
                logger.trace(" ssl handshake success {}:{}", tuple3.getValue1(), tuple3.getValue2());
            } else {
                String sslHandshakeFailedMessage = " ssl handshake failed:/" + tuple3.getValue1() + ":" + tuple3.getValue2();
                errorMessageList.add(sslHandshakeFailedMessage);
            }
        }
    }
    // All connections failed
    if (!atLeastOneConnectSuccess) {
        logger.error(" all connections have failed, " + errorMessageList.toString());
        throw new RuntimeException(" Failed to connect to nodes: " + errorMessageList.toString() + helpInfo);
    }
    running = true;
    logger.debug(" start connect end. ");
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ArrayList(java.util.ArrayList) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) SslHandler(io.netty.handler.ssl.SslHandler) BigInteger(java.math.BigInteger) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Tuple3(org.fisco.bcos.web3j.tuples.generated.Tuple3) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 58 with LengthFieldBasedFrameDecoder

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

the class HydraChannelInitializer method initChannel.

@Override
protected void initChannel(C channel) {
    ChannelPipeline pipeline = channel.pipeline();
    // In
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    pipeline.addLast(new PacketDecoder(protocol));
    // Out
    pipeline.addLast(new LengthFieldPrepender(4));
    pipeline.addLast(new PacketEncoder(protocol));
    /*
        if (!useUDP) {
            TCPHydraSession session = new TCPHydraSession(channel, protocol);
            pipeline.addLast(session);
        } else {
            UDPHydraSession session = new UDPHydraSession(channel, protocol);
            pipeline.addLast(session);
        }
         */
    HydraSession session = new HydraSession(channel, protocol);
    pipeline.addLast(session);
    // Add sessions to protocol, to keep track of them
    if (isServer) {
        protocol.addSession(session);
    } else {
        protocol.setClientSession(session);
    }
    if (!useUDP) {
        if (protocol.getSessionListener() != null) {
            // Inform SessionListener about new session
            protocol.callSessionListener(true, session);
        } else if (protocol.getSessionConsumer() != null) {
            // Inform SessionConsumer about new session
            protocol.callSessionConsumer(true, session);
        }
    }
}
Also used : PacketEncoder(de.datasecs.hydra.shared.protocol.packets.serialization.PacketEncoder) TCPHydraSession(de.datasecs.hydra.shared.handler.impl.TCPHydraSession) HydraSession(de.datasecs.hydra.shared.handler.impl.HydraSession) UDPHydraSession(de.datasecs.hydra.shared.handler.impl.UDPHydraSession) PacketDecoder(de.datasecs.hydra.shared.protocol.packets.serialization.PacketDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 59 with LengthFieldBasedFrameDecoder

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

the class PSClient method init.

/**
 * Init
 */
public void init() {
    bootstrap = new Bootstrap();
    Configuration conf = context.getConf();
    int workerNum = conf.getInt(AngelConf.ANGEL_PS_HA_SYNC_WORKER_NUM, AngelConf.DEFAULT_ANGEL_PS_HA_SYNC_WORKER_NUM);
    channelManager = new ChannelManager(bootstrap, workerNum);
    int sendBuffSize = conf.getInt(AngelConf.ANGEL_PS_HA_SYNC_SEND_BUFFER_SIZE, AngelConf.DEFAULT_ANGEL_PS_HA_SYNC_SEND_BUFFER_SIZE);
    final int maxMessageSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE);
    // TODO: use Epoll for linux future
    /*Class channelClass = null;
    String os = System.getProperty("os.name");
    if(os.toLowerCase().startsWith("win")) {
      LOG.info("os is windows, we use NioEventLoopGroup");
      channelClass = NioSocketChannel.class;
      eventGroup = new NioEventLoopGroup(nettyWorkerNum);
      ((NioEventLoopGroup)eventGroup).setIoRatio(70);
    } else if(os.toLowerCase().startsWith("linux")) {

    }
    */
    eventGroup = new NioEventLoopGroup(workerNum);
    ((NioEventLoopGroup) eventGroup).setIoRatio(70);
    bootstrap.group(eventGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_SNDBUF, sendBuffSize).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeLine = ch.pipeline();
            pipeLine.addLast(new LengthFieldBasedFrameDecoder(maxMessageSize, 0, 4, 0, 4));
            pipeLine.addLast(new LengthFieldPrepender(4));
            pipeLine.addLast(new PSClientHandler());
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelManager(com.tencent.angel.common.transport.ChannelManager) Configuration(org.apache.hadoop.conf.Configuration) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 60 with LengthFieldBasedFrameDecoder

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

the class ServiceChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    if (enableTLS) {
        SslContext sslCtx = SecurityUtility.createNettySslContextForServer(true, /* to allow InsecureConnection */
        serviceConfig.getTlsTrustCertsFilePath(), serviceConfig.getTlsCertificateFilePath(), serviceConfig.getTlsKeyFilePath(), serviceConfig.getTlsCiphers(), serviceConfig.getTlsProtocols(), serviceConfig.getTlsRequireTrustedClientCertOnConnect());
        ch.pipeline().addLast(TLS_HANDLER, sslCtx.newHandler(ch.alloc()));
    }
    ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(PulsarDecoder.MaxFrameSize, 0, 4, 0, 4));
    ch.pipeline().addLast("handler", new ProxyConnection(proxyService));
}
Also used : LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) SslContext(io.netty.handler.ssl.SslContext)

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