use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project Hydra by DataSecs.
the class HydraChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel 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));
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);
}
// Inform SessionListener about new session
protocol.callSessionListener(true, session);
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project carbondata by apache.
the class NonSecureDictionaryServer method bindToPort.
/**
* Binds dictionary server to an available port.
*/
@Override
public void bindToPort() {
long start = System.currentTimeMillis();
// Configure the server.
int i = 0;
while (i < 10) {
int newPort = port + i;
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
pipeline.addLast("NonSecureDictionaryServerHandler", nonSecureDictionaryServerHandler);
}
});
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
String hostToBind = findLocalIpAddress(LOGGER);
InetSocketAddress address = hostToBind == null ? new InetSocketAddress(newPort) : new InetSocketAddress(hostToBind, newPort);
bootstrap.bind(address).sync();
LOGGER.info("Dictionary Server started, Time spent " + (System.currentTimeMillis() - start) + " Listening on port " + newPort);
this.port = newPort;
this.host = hostToBind;
break;
} catch (Exception e) {
LOGGER.error(e, "Dictionary Server Failed to bind to port:");
if (i == 9) {
throw new RuntimeException("Dictionary Server Could not bind to any port");
}
}
i++;
}
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project java by wavefrontHQ.
the class PushAgent method startPickleListener.
protected void startPickleListener(String strPort, GraphiteFormatter formatter) {
if (prefix != null && !prefix.isEmpty()) {
preprocessors.forPort(strPort).forReportPoint().addTransformer(new ReportPointAddPrefixTransformer(prefix));
}
preprocessors.forPort(strPort).forReportPoint().addFilter(new ReportPointTimestampInRangeFilter(dataBackfillCutoffHours, dataPrefillCutoffHours));
int port = Integer.parseInt(strPort);
// Set up a custom handler
ChannelHandler handler = new ChannelByteArrayHandler(new PickleProtocolDecoder("unknown", customSourceTags, formatter.getMetricMangler(), port), new PointHandlerImpl(strPort, pushValidationLevel, pushBlockedSamples, getFlushTasks(strPort)), preprocessors.forPort(strPort));
// to the decoder.
class FrameDecoderFactoryImpl implements StreamIngester.FrameDecoderFactory {
@Override
public ChannelInboundHandler getDecoder() {
return new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN, 1000000, 0, 4, 0, 4, false);
}
}
startAsManagedThread(new StreamIngester(new FrameDecoderFactoryImpl(), handler, port).withChildChannelOptions(childChannelOptions), "listener-binary-pickle-" + port);
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project web3sdk by FISCO-BCOS.
the class ChannelConnections method startConnect.
public void startConnect() {
if (running) {
logger.debug("服务已启动");
return;
}
logger.debug("初始化connections connect");
// 初始化netty
EventLoopGroup workerGroup = new NioEventLoopGroup();
bootstrap.group(workerGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
final ChannelConnections selfService = this;
final ThreadPoolTaskExecutor selfThreadPool = threadPool;
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
final Resource keystoreResource = resolver.getResource(getClientKeystorePath());
final Resource caResource = resolver.getResource(getCaCertPath());
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
InputStream ksInputStream = keystoreResource.getInputStream();
ks.load(ksInputStream, getKeystorePassWord().toCharArray());
/*
* 每次连接使用新的handler 连接信息从socketChannel中获取
*/
ChannelHandler handler = new ChannelHandler();
handler.setConnections(selfService);
handler.setIsServer(false);
handler.setThreadPool(selfThreadPool);
SslContext sslCtx = SslContextBuilder.forClient().trustManager(caResource.getFile()).keyManager((PrivateKey) ks.getKey("client", getClientCertPassWord().toCharArray()), (X509Certificate) ks.getCertificate("client")).build();
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new LengthFieldBasedFrameDecoder(1024 * 1024 * 4, 0, 4, -4, 0), new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS), handler);
}
});
running = true;
Thread loop = new Thread() {
public void run() {
try {
while (true) {
if (!running) {
return;
}
// 尝试重连
reconnect();
Thread.sleep(heartBeatDelay);
}
} catch (InterruptedException e) {
logger.error("系统错误", e);
}
}
};
loop.start();
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project pravega by pravega.
the class AdminConnectionListenerTest method testCreateEncodingStack.
@Test
public void testCreateEncodingStack() {
@Cleanup AdminConnectionListener listener = new AdminConnectionListener(false, false, "localhost", 6622, mock(StreamSegmentStore.class), mock(TableStore.class), new PassingTokenVerifier(), null, null, SecurityConfigDefaults.TLS_PROTOCOL_VERSION);
List<ChannelHandler> stack = listener.createEncodingStack("connection");
// Check that the order of encoders is the right one.
Assert.assertTrue(stack.get(0) instanceof ExceptionLoggingHandler);
Assert.assertTrue(stack.get(1) instanceof CommandEncoder);
Assert.assertTrue(stack.get(2) instanceof LengthFieldBasedFrameDecoder);
Assert.assertTrue(stack.get(3) instanceof CommandDecoder);
}
Aggregations