use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project incubator-pulsar by apache.
the class ServerCnxTest method resetChannel.
protected void resetChannel() throws Exception {
int MaxMessageSize = 5 * 1024 * 1024;
if (channel != null && channel.isActive()) {
serverCnx.close();
channel.close().get();
}
serverCnx = new ServerCnx(brokerService);
serverCnx.authRole = "";
channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4), serverCnx);
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project incubator-pulsar by apache.
the class PulsarChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
if (enableTLS) {
SslContext sslCtx = SecurityUtility.createNettySslContextForServer(serviceConfig.isTlsAllowInsecureConnection(), serviceConfig.getTlsTrustCertsFilePath(), serviceConfig.getTlsCertificateFilePath(), serviceConfig.getTlsKeyFilePath(), serviceConfig.getTlsCiphers(), serviceConfig.getTlsProtocols(), serviceConfig.getTlsRequireTrustedClientCertOnConnect());
ch.pipeline().addLast(TLS_HANDLER, sslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.ENCODER);
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(PulsarDecoder.MaxFrameSize, 0, 4, 0, 4));
ch.pipeline().addLast("handler", new ServerCnx(brokerService));
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder 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();
}
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project jMiniLang by bajdcc.
the class ModuleNetClient method run.
public void run() {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
if (addr.startsWith(":")) {
addr = "127.0.0.1" + addr;
}
URI uri = new URI("http://" + addr);
if (uri.getPort() < 0)
throw new URISyntaxException(addr, "invalid address");
Bootstrap b = new Bootstrap();
b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetClientHandler(msgQueue));
}
});
ChannelFuture f = b.connect(uri.getHost(), uri.getPort());
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
this.addr = f.channel().localAddress().toString();
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Client closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.LengthFieldBasedFrameDecoder in project jMiniLang by bajdcc.
the class ModuleNetServer method run.
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetServerHandler(msgQueue));
}
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port);
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Server closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
Aggregations