에 대한 도움말 내 framedecoder 다음 코드는 한 :의 Netty 3.5.8 - 결승 - FrameDecoder
protected Object decode(ChannelHandlerContext channelHandlerContext,
Channel channel, ChannelBuffer channelBuffer) throws Exception {
//Header 1 + 4 length bytes
if (channelBuffer.readableBytes() < 5) {
return null;
}
System.out.println(channelBuffer.readerIndex());
//mark the reader index
channelBuffer.markReaderIndex();
//extract the length bytes and convert to an int value
//**note: length value of the message is a decimal not hex
StringBuilder lengthBuilder = new StringBuilder("");
for (int ctr = 1; ctr < 5; ctr += 1) {
lengthBuilder.append(String.format("%02x",
channelBuffer.getByte(ctr)));
}
int length = Integer.parseInt(hexToASCII(lengthBuilder.toString()));
System.out.println("This is the length: " + length);
System.out.println("reader index: " + channelBuffer.readerIndex());
channelBuffer.readerIndex(4);
//get the remaining bytes
if (channelBuffer.readableBytes() <= length) {
channelBuffer.resetReaderIndex();
return null;
}
String s = "";
for (int ctr = 0; ctr < length + 5; ctr += 1) {
s += (char) channelBuffer.getByte(ctr);
}
return s;
}
모든 것이 int length = Integer.parseInt(hexToASCII(lengthBuilder.toString()));
라인까지 잘거야. 그 라인 이후, 디코더는 여전히 나머지 바이트를 읽을 수 있지만 실제 메시지를 길이 필드로 사용하여 구문 분석 예외 오류가 발생합니다.
말해, 전 메시지 [B is the header] [0005 - actual message length] [E - header 2] [HELLO - actual message]
가 .. 정수 값에 길이 필드를 파싱 한 후, 디코더는 길이 필드를 파싱 실제 메시지 HELL
의 처음 네 글자를 사용 B0005EHELLO
이 예외가 발생되고 있었다. 이 문제에 대해 친절하게 도와주세요.