PeerSocketHandler: make first message validation explicit

This commit is contained in:
Oscar Guindzberg
2015-03-27 15:40:34 -03:00
parent 2837ad581a
commit e5b52fb73b

View File

@@ -124,11 +124,12 @@ public abstract class PeerSocketHandler extends AbstractTimeoutHandler implement
buff.capacity() >= BitcoinSerializer.BitcoinPacketHeader.HEADER_LENGTH + 4);
try {
// Repeatedly try to deserialize messages until we hit a BufferUnderflowException
for (int i = 0; true; i++) {
boolean firstMessage = true;
while (true) {
// If we are in the middle of reading a message, try to fill that one first, before we expect another
if (largeReadBuffer != null) {
// This can only happen in the first iteration
checkState(i == 0);
checkState(firstMessage);
// Read new bytes into the largeReadBuffer
int bytesToGet = Math.min(buff.remaining(), largeReadBuffer.length - largeReadBufferPos);
buff.get(largeReadBuffer, largeReadBufferPos, bytesToGet);
@@ -149,7 +150,7 @@ public abstract class PeerSocketHandler extends AbstractTimeoutHandler implement
message = serializer.deserialize(buff);
} catch (BufferUnderflowException e) {
// If we went through the whole buffer without a full message, we need to use the largeReadBuffer
if (i == 0 && buff.limit() == buff.capacity()) {
if (firstMessage && buff.limit() == buff.capacity()) {
// ...so reposition the buffer to 0 and read the next message header
buff.position(0);
try {
@@ -176,6 +177,7 @@ public abstract class PeerSocketHandler extends AbstractTimeoutHandler implement
}
// Process our freshly deserialized message
processMessage(message);
firstMessage = false;
}
} catch (Exception e) {
exceptionCaught(e);