我有一个基于netty 4.0的应用程序,我需要在其中接收/发送消息,这些消息在LITTLE_ENDIAN中带有 2 字节的消息大小。
我弄清楚了接收部分,我正在成功接收消息。
但我不知道如何为Prepender做同样的事情,它当前在默认值BIG_ENDIAN中添加长度。请建议我该如何改变这一点。
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
// Decoder - This part works as expected
.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 4096 /*maximum length*/, 0 /* size field offset */, 2 /* size field length */, 0, 2, false))
.addLast("protobufDecoder", new ProtobufDecoder(MoopRrAdmin.AdminResponse.getDefaultInstance(), extensionRegistry))
// Encoder the Prepender adds length in BIG ENDIAN which is wrong
.addLast("frameEncoder", new LengthFieldPrepender(2 /* size filed length */, 0 /* length adjustment */, true /* size includes size field */))
.addLast("protobufEncoder", new ProtobufEncoder())
// Handler
.addLast(new MoopConnectionHandler(requester, instanceConnectionManager));
}
编辑:
只是为了分享实际有效的临时解决方案。问题仍然存在,因为我不相信这是预期的方式......
...
// Encoder
.addLast("frameEncoder", new LengthFieldPrepender(2 /* size filed length */, 0 /* length adjustment */, false /* size includes size field */) {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
ByteBuf outWithLittleEndian = out.order(ByteOrder.LITTLE_ENDIAN);
super.encode(ctx, msg, outWithLittleEndian);
}
})
...