本文整理汇总了Java中io.netty.handler.codec.LengthFieldBasedFrameDecoder类的典型用法代码示例。如果您正苦于以下问题:Java LengthFieldBasedFrameDecoder类的具体用法?Java LengthFieldBasedFrameDecoder怎么用?Java LengthFieldBasedFrameDecoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LengthFieldBasedFrameDecoder类属于io.netty.handler.codec包,在下文中一共展示了LengthFieldBasedFrameDecoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
if (enableTLS) {
File tlsCert = new File(serviceConfig.getTlsCertificateFilePath());
File tlsKey = new File(serviceConfig.getTlsKeyFilePath());
SslContextBuilder builder = SslContextBuilder.forServer(tlsCert, tlsKey);
if (serviceConfig.isTlsAllowInsecureConnection()) {
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
} else {
if (serviceConfig.getTlsTrustCertsFilePath().isEmpty()) {
// Use system default
builder.trustManager((File) null);
} else {
File trustCertCollection = new File(serviceConfig.getTlsTrustCertsFilePath());
builder.trustManager(trustCertCollection);
}
}
SslContext sslCtx = builder.clientAuth(ClientAuth.OPTIONAL).build();
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 ServerConnection(discoveryService));
}
示例2: run
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
public void run() {
workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
// b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new MsgPackDecode());
pipeline.addLast("encoder", new MsgPackEncode());
pipeline.addLast(new ClientHandler());
}
});
channel = b.connect(clientProperties.getServerHost(), clientProperties.getServerPort()).sync().channel();
status = Status.START;
channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
status = Status.STOP;
}
示例3: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pip = ch.pipeline();
int maxLength = 1048576;
int lengthFieldLength = 4;
int ignoreLength = -4;
int offset = 0;
pip.addLast(new LengthFieldBasedFrameDecoder(maxLength, offset, lengthFieldLength, ignoreLength, lengthFieldLength));
pip.addLast(new MessageDecoder(builder.getImessageandhandler()));
pip.addLast(new LengthFieldPrepender(4, true));
pip.addLast(new MessageEncoder(builder.getImessageandhandler()));
pip.addLast(new MessageExecutor(builder.getConsumer(), builder.getListener()));
for (ChannelHandler handler : builder.getExtraHandlers()) {
pip.addLast(handler);
}
}
示例4: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
/**
* Init channel.
*/
public void initChannel(Channel ch) {
ChannelPipeline cp = ch.pipeline();
//
cp.addLast("frame", new LengthFieldBasedFrameDecoder(this.maxFrameLength, 0, lengthFieldLength, 0, lengthFieldLength));
cp.addLast("prepender", this.prepender);
if (isAutoConnect()) {
cp.addLast("autoConnect", new AutoConnectHandler(this));
}
// Event Handler
if (!handlers.isEmpty()) {
for (Pair<String, ChannelHandler> pair : handlers) {
cp.addLast(pair.getLeft(), pair.getRight());
}
}
}
示例5: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
if (enableTLS) {
File tlsCert = new File(serviceConfig.getTlsCertificateFilePath());
File tlsKey = new File(serviceConfig.getTlsKeyFilePath());
SslContextBuilder builder = SslContextBuilder.forServer(tlsCert, tlsKey);
if (serviceConfig.isTlsAllowInsecureConnection()) {
builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
} else {
if (serviceConfig.getTlsTrustCertsFilePath().isEmpty()) {
// Use system default
builder.trustManager((File) null);
} else {
File trustCertCollection = new File(serviceConfig.getTlsTrustCertsFilePath());
builder.trustManager(trustCertCollection);
}
}
SslContext sslCtx = builder.clientAuth(ClientAuth.OPTIONAL).build();
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 ServerCnx(brokerService));
}
示例6: startMockBrokerService
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
public void startMockBrokerService() throws Exception {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
final int numThreads = 2;
final int MaxMessageSize = 5 * 1024 * 1024;
try {
workerGroup = EventLoopUtil.newEventLoopGroup(numThreads, threadFactory);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup, workerGroup);
bootstrap.channel(EventLoopUtil.getServerSocketChannelClass(workerGroup));
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
ch.pipeline().addLast("handler", new MockServerCnx());
}
});
// Bind and start to accept incoming connections.
bootstrap.bind(brokerServicePort).sync();
} catch (Exception e) {
throw e;
}
}
示例7: service
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
public static void service() throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new TcpServerHandler());
}
});
ChannelFuture f = bootstrap.bind(IP, PORT).sync();
f.channel().closeFuture().sync();
System.out.println("TCP服务器已启动");
}
示例8: getBootstrap
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
/**
* 初始化Bootstrap
* @return
*/
public static final Bootstrap getBootstrap(){
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class);
b.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel 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 TcpClientHandler());
}
});
b.option(ChannelOption.SO_KEEPALIVE, true);
return b;
}
示例9: run
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
protected static void run() throws Exception {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public 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(new TcpServerHandler());
}
});
b.bind(IP, PORT).sync();
System.out.println("TCP服务器已启动");
}
示例10: provideHandlerProviders
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Provides
@EppProtocol
static ImmutableList<Provider<? extends ChannelHandler>> provideHandlerProviders(
Provider<SslServerInitializer<NioSocketChannel>> sslServerInitializerProvider,
Provider<ProxyProtocolHandler> proxyProtocolHandlerProvider,
@EppProtocol Provider<ReadTimeoutHandler> readTimeoutHandlerProvider,
Provider<LengthFieldBasedFrameDecoder> lengthFieldBasedFrameDecoderProvider,
Provider<LengthFieldPrepender> lengthFieldPrependerProvider,
Provider<EppServiceHandler> eppServiceHandlerProvider,
Provider<LoggingHandler> loggingHandlerProvider,
Provider<FullHttpRequestRelayHandler> relayHandlerProvider) {
return ImmutableList.of(
proxyProtocolHandlerProvider,
sslServerInitializerProvider,
readTimeoutHandlerProvider,
lengthFieldBasedFrameDecoderProvider,
lengthFieldPrependerProvider,
eppServiceHandlerProvider,
loggingHandlerProvider,
relayHandlerProvider);
}
示例11: testFailSlowTooLongFrameRecovery
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Test
public void testFailSlowTooLongFrameRecovery() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(
new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4, false));
for (int i = 0; i < 2; i ++) {
assertFalse(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 })));
try {
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 })));
fail(DecoderException.class.getSimpleName() + " must be raised.");
} catch (TooLongFrameException e) {
// Expected
}
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' }));
ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release();
}
}
示例12: testFailFastTooLongFrameRecovery
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Test
public void testFailFastTooLongFrameRecovery() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(
new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4));
for (int i = 0; i < 2; i ++) {
try {
assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 })));
fail(DecoderException.class.getSimpleName() + " must be raised.");
} catch (TooLongFrameException e) {
// Expected
}
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0, 0, 1, 'A' }));
ByteBuf buf = releaseLater((ByteBuf) ch.readInbound());
assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1));
buf.release();
}
}
示例13: connect
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
public void connect() throws Exception {
workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap =
new Bootstrap()
.group(workerGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
pipeline.addLast(new ProtobufDecoder(Protocol.BaseMessage.getDefaultInstance()));
clientHandler = new ClientHandler();
pipeline.addLast(clientHandler);
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new ProtobufEncoder());
}
});
ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
channel = channelFuture.channel();
}
示例14: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
LOGGER.info("Setting up Server channel !!");
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast("encoder", new LengthFieldPrepender(4));
//ch.pipeline().addLast("logger", new LoggingHandler());
// Create server metric for this handler and add to aggregate if present
NettyServerMetrics serverMetric =
new NettyServerMetrics(_registry, NettyTCPServer.class.getName() + "_" + Utils.getUniqueId() + "_");
if (null != _globalMetrics) {
_globalMetrics.addTransportClientMetrics(serverMetric);
}
ch.pipeline().addLast("request_handler",
new NettyChannelInboundHandler(_handlerFactory.createNewRequestHandler(), serverMetric, _defaultLargeQueryLatencyMs));
}
示例15: initChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
if (sslContext != null) {
ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), host, port));
}
// In
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
if (cryptoFunction != null) {
ch.pipeline().addLast(cryptoFunction.getDecoder());
}
ch.pipeline().addLast(new PacketDecoder(protocol));
// Out
ch.pipeline().addLast(new LengthFieldPrepender(4));
if (cryptoFunction != null) {
ch.pipeline().addLast(cryptoFunction.getEncoder());
}
ch.pipeline().addLast(new PacketEncoder(protocol));
// Handler
ch.pipeline().addLast(new CascadeSession(ch, protocol, sessionListener));
}