@@ -197,6 +197,7 @@ public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder { |
197 | 197 | private boolean discardingTooLongFrame; |
198 | 198 | private long tooLongFrameLength; |
199 | 199 | private long bytesToDiscard; |
| 200 | + private int frameLengthInt = -1; |
200 | 201 |
|
201 | 202 | /** |
202 | 203 | * Creates a new instance. |
@@ -394,38 +395,40 @@ private static void failOnFrameLengthLessThanInitialBytesToStrip(ByteBuf in, |
394 | 395 | * be created. |
395 | 396 | */ |
396 | 397 | protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { |
397 | | - if (discardingTooLongFrame) { |
398 | | - discardingTooLongFrame(in); |
399 | | - } |
| 398 | + long frameLength = 0; |
| 399 | + if (frameLengthInt == -1) { // new frame |
400 | 400 |
|
401 | | - if (in.readableBytes() < lengthFieldEndOffset) { |
402 | | - return null; |
403 | | - } |
| 401 | + if (discardingTooLongFrame) { |
| 402 | + discardingTooLongFrame(in); |
| 403 | + } |
404 | 404 |
|
405 | | - int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset; |
406 | | - long frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder); |
| 405 | + if (in.readableBytes() < lengthFieldEndOffset) { |
| 406 | + return null; |
| 407 | + } |
407 | 408 |
|
408 | | - if (frameLength < 0) { |
409 | | - failOnNegativeLengthField(in, frameLength, lengthFieldEndOffset); |
410 | | - } |
| 409 | + int actualLengthFieldOffset = in.readerIndex() + lengthFieldOffset; |
| 410 | + frameLength = getUnadjustedFrameLength(in, actualLengthFieldOffset, lengthFieldLength, byteOrder); |
| 411 | + |
| 412 | + if (frameLength < 0) { |
| 413 | + failOnNegativeLengthField(in, frameLength, lengthFieldEndOffset); |
| 414 | + } |
411 | 415 |
|
412 | | - frameLength += lengthAdjustment + lengthFieldEndOffset; |
| 416 | + frameLength += lengthAdjustment + lengthFieldEndOffset; |
413 | 417 |
|
414 | | - if (frameLength < lengthFieldEndOffset) { |
415 | | - failOnFrameLengthLessThanLengthFieldEndOffset(in, frameLength, lengthFieldEndOffset); |
416 | | - } |
| 418 | + if (frameLength < lengthFieldEndOffset) { |
| 419 | + failOnFrameLengthLessThanLengthFieldEndOffset(in, frameLength, lengthFieldEndOffset); |
| 420 | + } |
417 | 421 |
|
418 | | - if (frameLength > maxFrameLength) { |
419 | | - exceededFrameLength(in, frameLength); |
420 | | - return null; |
| 422 | + if (frameLength > maxFrameLength) { |
| 423 | + exceededFrameLength(in, frameLength); |
| 424 | + return null; |
| 425 | + } |
| 426 | + // never overflows because it's less than maxFrameLength |
| 427 | + frameLengthInt = (int) frameLength; |
421 | 428 | } |
422 | | - |
423 | | - // never overflows because it's less than maxFrameLength |
424 | | - int frameLengthInt = (int) frameLength; |
425 | | - if (in.readableBytes() < frameLengthInt) { |
| 429 | + if (in.readableBytes() < frameLengthInt) { // frameLengthInt exist , just check buf |
426 | 430 | return null; |
427 | 431 | } |
428 | | - |
429 | 432 | if (initialBytesToStrip > frameLengthInt) { |
430 | 433 | failOnFrameLengthLessThanInitialBytesToStrip(in, frameLength, initialBytesToStrip); |
431 | 434 | } |
@@ -436,6 +439,7 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception |
436 | 439 | int actualFrameLength = frameLengthInt - initialBytesToStrip; |
437 | 440 | ByteBuf frame = extractFrame(ctx, in, readerIndex, actualFrameLength); |
438 | 441 | in.readerIndex(readerIndex + actualFrameLength); |
| 442 | + frameLengthInt = -1; // start processing the next frame |
439 | 443 | return frame; |
440 | 444 | } |
441 | 445 |
|
|