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();
}
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. ");
}
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);
}
}
}
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());
}
});
}
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));
}
Aggregations