use of io.netty.handler.codec.LengthFieldPrepender 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.LengthFieldPrepender in project camel by apache.
the class MultipleCodecsTest method createRegistry.
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
// START SNIPPET: registry-beans
ChannelHandlerFactory lengthDecoder = ChannelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4);
StringDecoder stringDecoder = new StringDecoder();
registry.bind("length-decoder", lengthDecoder);
registry.bind("string-decoder", stringDecoder);
LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
StringEncoder stringEncoder = new StringEncoder();
registry.bind("length-encoder", lengthEncoder);
registry.bind("string-encoder", stringEncoder);
List<ChannelHandler> decoders = new ArrayList<ChannelHandler>();
decoders.add(lengthDecoder);
decoders.add(stringDecoder);
List<ChannelHandler> encoders = new ArrayList<ChannelHandler>();
encoders.add(lengthEncoder);
encoders.add(stringEncoder);
registry.bind("encoders", encoders);
registry.bind("decoders", decoders);
// END SNIPPET: registry-beans
return registry;
}
use of io.netty.handler.codec.LengthFieldPrepender in project herddb by diennea.
the class NettyChannelAcceptor method start.
public void start() throws Exception {
if (ssl) {
if (sslCertFile == null) {
LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate");
if (sslCiphers != null) {
LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
}
SelfSignedCertificate ssc = new SelfSignedCertificate();
try {
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers).build();
} finally {
ssc.delete();
}
} else {
LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath());
if (sslCiphers != null) {
LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
}
sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword).ciphers(sslCiphers).build();
}
}
if (callbackThreads == 0) {
callbackExecutor = Executors.newCachedThreadPool();
} else {
callbackExecutor = Executors.newFixedThreadPool(callbackThreads, new ThreadFactory() {
private final AtomicLong count = new AtomicLong();
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "herddb-callbacks-" + count.incrementAndGet());
}
});
}
InetSocketAddress address = new InetSocketAddress(host, port);
LOGGER.log(Level.SEVERE, "Starting HerdDB network server at {0}:{1}", new Object[] { host, port + "" });
ChannelInitializer<io.netty.channel.Channel> channelInitialized = new ChannelInitializer<io.netty.channel.Channel>() {
@Override
public void initChannel(io.netty.channel.Channel ch) throws Exception {
NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor);
if (acceptor != null) {
acceptor.createConnection(session);
}
// Add SSL handler first to encrypt and decrypt everything.
if (ssl) {
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
//
ch.pipeline().addLast("messageencoder", new DataMessageEncoder());
ch.pipeline().addLast("messagedecoder", new DataMessageDecoder());
ch.pipeline().addLast(new InboundMessageHandler(session));
}
};
if (enableRealNetwork) {
if (NetworkUtils.isEnableEpoolNative()) {
bossGroup = new EpollEventLoopGroup(workerThreads);
workerGroup = new EpollEventLoopGroup(workerThreads);
LOGGER.log(Level.INFO, "Using netty-native-epoll network type");
} else {
bossGroup = new NioEventLoopGroup(workerThreads);
workerGroup = new NioEventLoopGroup(workerThreads);
LOGGER.log(Level.INFO, "Using nio network type");
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NetworkUtils.isEnableEpoolNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class).childHandler(channelInitialized).option(ChannelOption.SO_BACKLOG, 128);
ChannelFuture f = b.bind(address).sync();
this.channel = f.channel();
}
if (enableJVMNetwork) {
localBossGroup = new DefaultEventLoopGroup(workerThreads);
localWorkerGroup = new DefaultEventLoopGroup(workerThreads);
ServerBootstrap b_local = new ServerBootstrap();
b_local.group(localBossGroup, localWorkerGroup).channel(LocalServerChannel.class).childHandler(channelInitialized);
String hostAddress = NetworkUtils.getAddress(address);
LocalServerRegistry.registerLocalServer(hostAddress, port, ssl);
ChannelFuture local_f = b_local.bind(new LocalAddress(hostAddress + ":" + port + ":" + ssl)).sync();
this.local_channel = local_f.channel();
}
}
use of io.netty.handler.codec.LengthFieldPrepender 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 io.netty.handler.codec.LengthFieldPrepender in project tutorials-java by Artister.
the class NettyClient method run.
public void run() {
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());
}
});
for (int i = 0; i < 10; i++) {
ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
f.channel().writeAndFlush("hello Service!" + Thread.currentThread().getName() + ":--->:" + i);
f.channel().closeFuture().sync();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
Aggregations