本文整理匯總了Java中io.netty.handler.codec.LengthFieldPrepender類的典型用法代碼示例。如果您正苦於以下問題:Java LengthFieldPrepender類的具體用法?Java LengthFieldPrepender怎麽用?Java LengthFieldPrepender使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LengthFieldPrepender類屬於io.netty.handler.codec包,在下文中一共展示了LengthFieldPrepender類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: run
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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;
}
示例2: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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);
}
}
示例3: service
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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服務器已啟動");
}
示例4: getBootstrap
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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;
}
示例5: run
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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服務器已啟動");
}
示例6: provideHandlerProviders
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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);
}
示例7: testPrependLengthInLittleEndian
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
@Test
public void testPrependLengthInLittleEndian() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(ByteOrder.LITTLE_ENDIAN, 4, 0, false));
ch.writeOutbound(msg);
ByteBuf buf = (ByteBuf) ch.readOutbound();
assertEquals(5, buf.readableBytes());
byte[] writtenBytes = new byte[buf.readableBytes()];
buf.getBytes(0, writtenBytes);
assertEquals(1, writtenBytes[0]);
assertEquals(0, writtenBytes[1]);
assertEquals(0, writtenBytes[2]);
assertEquals(0, writtenBytes[3]);
assertEquals('A', writtenBytes[4]);
buf.release();
assertFalse("The channel must have been completely read", ch.finish());
}
示例8: connect
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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();
}
示例9: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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));
}
示例10: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的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));
}
示例11: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
// Enable TCPS if necessary.
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast("logger", new LoggingHandler(LogLevel.DEBUG));
p.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, Integer.MAX_VALUE, 0, 4, 0, 4, true));
p.addLast(new ByteBufferDecoder());
p.addLast(new LengthFieldPrepender(4, 0, false) {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
ByteBuf outWithLittleEndian = out.order(ByteOrder.LITTLE_ENDIAN);
super.encode(ctx, msg, outWithLittleEndian);
}
});
p.addLast(new ByteBufferEncoder());
p.addLast(handler);
}
示例12: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
// Enable TCPS if necessary.
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast("logger", new LoggingHandler(LogLevel.DEBUG));
p.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, Integer.MAX_VALUE, 0, 4, 0, 4, true));
p.addLast(new ByteBufferDecoder());
p.addLast(new LengthFieldPrepender(4, 0, false) {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
ByteBuf outWithLittleEndian = out.order(ByteOrder.LITTLE_ENDIAN);
super.encode(ctx, msg, outWithLittleEndian);
}
});
p.addLast(new ByteBufferEncoder());
p.addLast(new TcpHandler(transportFactory, path, connectionListener));
}
示例13: initChannel
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
SSLEngine engine =
SecureSocketSslContextFactory.getClientContext().createSSLEngine();
engine.setUseClientMode(true);
pipeline.addLast("ssl", new SslHandler(engine));
// On top of the SSL handler, add the text line codec.
// pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("length-decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("String-decoder", new StringDecoder());
pipeline.addLast("length-encoder", new LengthFieldPrepender(4));
pipeline.addLast("String-encoder", new StringEncoder());
pipeline.addLast("handler", new SecureSocketClientHandler());
}
示例14: run
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
public void run() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.localAddress(port)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LengthFieldPrepender(4)).addLast(new LengthFieldServerHandler());
}
});
ChannelFuture cf = bootstrap.bind().sync();
cf.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
示例15: run
import io.netty.handler.codec.LengthFieldPrepender; //導入依賴的package包/類
public void run(String host, int port) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG))
.addLast(new LengthFieldPrepender(4))
.addLast(new StringEncoder())
.addLast(new ObjectToJsonStringEncoder())
.addLast(new ObjectEchoClientHandler());
}
});
bootstrap.connect(host, port).sync().channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}