我已经为正在构建的应用程序实现了帧解码器。到目前为止,当我运行代码时,数据包已正确解码,并且可以看到接收到的数据包是正确的。但是我得到一个例外:
decode() method must read at least one byte if it returned a frame (caused by: class com.smsgh.unitysmpp.Plugins.Smp.SmpPduFrameDecoder)
at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:434)
at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:84)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:471)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:332)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
这是我写的代码:
/ **
* @作者Arsene
*
* /
public class SmpPduFrameDecoder extends FrameDecoder {
private static Logger logger = LoggerFactory.getLogger(SmpPduFrameDecoder.class);
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer) throws Exception {
ChannelBuffer leFrame = null;
// Check the Endianness of the packet sent
if(buffer.order() == ByteOrder.BIG_ENDIAN){
if(buffer.hasArray()){
leFrame = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, buffer.array(), buffer.arrayOffset(), buffer.capacity());
}
else{
leFrame = ChannelBuffers.wrappedBuffer(buffer.toByteBuffer().order(ByteOrder.LITTLE_ENDIAN));
}
}
// Read the byte length
// wait until the length prefix is available
if (leFrame.readableBytes() < 4) {
logger.debug("Unable to Read the Length");
return null;
}
// parse the frame length (first 4 bytes)
int frameLength = leFrame.getInt(leFrame.readerIndex());
logger.info("Packet Received Length " + frameLength);
// wait until the whole data is available
if (leFrame.readableBytes() < frameLength) {
logger.debug("Unable to read the full PDU received");
return null;
}
leFrame.skipBytes(4);
int readerIndex = leFrame.readerIndex();
ChannelBuffer frame = leFrame.readBytes(frameLength);
leFrame.readerIndex(readerIndex+frameLength);
byte[] byteArray = frame.array();
StringBuilder sb = new StringBuilder();
for(byte b : byteArray){
sb.append(HexUtil.toHexString(b));
}
logger.info("Decode Frame Received without the length " +sb.toString());
logger.debug("Full PDU has been read");
return frame;
}
}
有人可以告诉我我的代码有什么问题并帮助我解决该异常吗?
最佳答案
我已经解决了问题。不必将缓冲区复制到新缓冲区中,我只需要使用swapInt即可获取实际的帧长度,如以下代码所示:
public class SmpPduFrameDecoder extends FrameDecoder {
private static Logger logger = LoggerFactory.getLogger(SmpPduFrameDecoder.class);
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer) throws Exception {
// Read the byte length
// wait until the length prefix is available
if (buffer.readableBytes() < 4) {
logger.debug("Unable to Read the Length");
return null;
}
// parse the frame length (first 4 bytes)
//int frameLength = leFrame.getInt(leFrame.readerIndex());
int frameLength = ChannelBuffers.swapInt(buffer.getInt(buffer.readerIndex()));
logger.info("Packet Received Length " + frameLength);
// wait until the whole data is available
if (buffer.readableBytes() < frameLength) {
logger.debug("Unable to read the full PDU received");
return null;
}
buffer.skipBytes(4);
ChannelBuffer frame = buffer.readBytes(frameLength);
buffer.readerIndex(frameLength + 4);
byte[] byteArray = frame.array();
StringBuilder sb = new StringBuilder();
for(byte b : byteArray){
sb.append(HexUtil.toHexString(b));
}
logger.info("Decode Frame Received without the length " +sb.toString());
logger.debug("Full PDU has been read");
return frame;
}
}
关于java - Netty Framedecoder错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15362788/