if (_heldFrame != null)
{
return;
}
TransportException frameParsingError = null;
int size = _size;
State state = _state;
ByteBuffer oldIn = null;
boolean transportAccepting = true;
while(in.hasRemaining() && state != State.ERROR && transportAccepting)
{
switch(state)
{
case HEADER0:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[0])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[0], state);
state = State.ERROR;
break;
}
state = State.HEADER1;
}
else
{
break;
}
case HEADER1:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[1])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[1], state);
state = State.ERROR;
break;
}
state = State.HEADER2;
}
else
{
break;
}
case HEADER2:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[2])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[2], state);
state = State.ERROR;
break;
}
state = State.HEADER3;
}
else
{
break;
}
case HEADER3:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[3])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[3], state);
state = State.ERROR;
break;
}
state = State.HEADER4;
}
else
{
break;
}
case HEADER4:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[4])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[4], state);
state = State.ERROR;
break;
}
state = State.HEADER5;
}
else
{
break;
}
case HEADER5:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[5])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[5], state);
state = State.ERROR;
break;
}
state = State.HEADER6;
}
else
{
break;
}
case HEADER6:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[6])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[6], state);
state = State.ERROR;
break;
}
state = State.HEADER7;
}
else
{
break;
}
case HEADER7:
if(in.hasRemaining())
{
byte c = in.get();
if(c != HEADER[7])
{
frameParsingError = new TransportException("AMQP header mismatch value %x, expecting %x. In state: %s", c, HEADER[7], state);
state = State.ERROR;
break;
}
state = State.SIZE_0;
}
else
{
break;
}
case SIZE_0:
if(!in.hasRemaining())
{
break;
}
if(in.remaining() >= 4)
{
size = in.getInt();
state = State.PRE_PARSE;
break;
}
else
{
size = (in.get() << 24) & 0xFF000000;
if(!in.hasRemaining())
{
state = State.SIZE_1;
break;
}
}
case SIZE_1:
size |= (in.get() << 16) & 0xFF0000;
if(!in.hasRemaining())
{
state = State.SIZE_2;
break;
}
case SIZE_2:
size |= (in.get() << 8) & 0xFF00;
if(!in.hasRemaining())
{
state = State.SIZE_3;
break;
}
case SIZE_3:
size |= in.get() & 0xFF;
state = State.PRE_PARSE;
case PRE_PARSE:
;
if(size < 8)
{
frameParsingError = new TransportException("specified frame size %d smaller than minimum frame header "
+ "size %d",
size, 8);
state = State.ERROR;
break;
}
if(in.remaining() < size-4)
{
_frameBuffer = ByteBuffer.allocate(size-4);
_frameBuffer.put(in);
state = State.BUFFERING;
break;
}
case BUFFERING:
if(_frameBuffer != null)
{
if(in.remaining() < _frameBuffer.remaining())
{
_frameBuffer.put(in);
break;
}
else
{
ByteBuffer dup = in.duplicate();
dup.limit(dup.position()+_frameBuffer.remaining());
in.position(in.position()+_frameBuffer.remaining());
_frameBuffer.put(dup);
oldIn = in;
_frameBuffer.flip();
in = _frameBuffer;
state = State.PARSING;
}
}
case PARSING:
int dataOffset = (in.get() << 2) & 0x3FF;
if(dataOffset < 8)
{
frameParsingError = new TransportException("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8);
state = State.ERROR;
break;
}
else if(dataOffset > size)
{
frameParsingError = new TransportException("specified frame data offset %d larger than the frame size %d", dataOffset, _size);
state = State.ERROR;
break;
}
// type
int type = in.get() & 0xFF;
int channel = in.getShort() & 0xFF;
if(type != 0)
{
frameParsingError = new TransportException("unknown frame type: %d", type);
state = State.ERROR;
break;
}
// note that this skips over the extended header if it's present
if(dataOffset!=8)
{
in.position(in.position()+dataOffset-8);
}
// oldIn null iff not working on duplicated buffer
final int frameBodySize = size - dataOffset;
if(oldIn == null)
{
oldIn = in;
in = in.duplicate();
final int endPos = in.position() + frameBodySize;
in.limit(endPos);
oldIn.position(endPos);
}
try
{
if (frameBodySize > 0)
{
_decoder.setByteBuffer(in);
Object val = _decoder.readObject();
Binary payload;
if(in.hasRemaining())
{
byte[] payloadBytes = new byte[in.remaining()];
in.get(payloadBytes);
payload = new Binary(payloadBytes);
}
else
{
payload = null;
}
if(val instanceof FrameBody)
{
FrameBody frameBody = (FrameBody) val;
if(TRACE_LOGGER.isLoggable(Level.FINE))
{
TRACE_LOGGER.log(Level.FINE, "IN: CH["+channel+"] : " + frameBody + (payload == null ? "" : "[" + payload + "]"));
}
TransportFrame frame = new TransportFrame(channel, frameBody, payload);
if(_frameHandler.isHandlingFrames())
{
_tail_closed = _frameHandler.handleFrame(frame);
}
else
{
transportAccepting = false;
_heldFrame = frame;
}
}
else
{
throw new TransportException("Frameparser encountered a "
+ (val == null? "null" : val.getClass())
+ " which is not a " + FrameBody.class);
}
}
else
{
if(TRACE_LOGGER.isLoggable(Level.FINEST))
{
TRACE_LOGGER.finest("Ignored empty frame");
}
}
reset();
in = oldIn;
oldIn = null;
_frameBuffer = null;
state = State.SIZE_0;
}
catch (DecodeException ex)
{
state = State.ERROR;
frameParsingError = new TransportException(ex);
}
break;
case ERROR:
// do nothing
}
}
if (_tail_closed)
{
if (in.hasRemaining()) {
state = State.ERROR;
frameParsingError = new TransportException("framing error");
} else if (state != State.SIZE_0) {
state = State.ERROR;
frameParsingError = new TransportException("connection aborted");
} else {
_frameHandler.closed();
}
}
_state = state;
_size = size;
if(_state == State.ERROR)
{
_tail_closed = true;
if(frameParsingError != null)
{
_parsingError = frameParsingError;
throw frameParsingError;
}
else
{
throw new TransportException("Unable to parse, probably because of a previous error");
}
}
}