private function parseRecord(stream:IDataInput):void {
var p:ByteArray;
while(_state!=STATE_CLOSED && stream.bytesAvailable>4) {
if (_packetQueue.length>0) {
var packet:Object = _packetQueue.shift();
p = packet.data;
if (stream.bytesAvailable+p.length>=packet.length) {
// we have a whole packet. put together.
stream.readBytes(p, p.length, packet.length-p.length);
parseOneRecord(packet.type, packet.length, p);
// do another loop to parse any leftover record
continue;
} else {
// not enough. grab the data and park it.
stream.readBytes(p, p.length, stream.bytesAvailable);
_packetQueue.push(packet);
continue;
}
}
var type:uint = stream.readByte();
var ver:uint = stream.readShort();
var length:uint = stream.readShort();
if (length>16384+2048) { // support compression and encryption overhead.
throw new TLSError("Excessive TLS Record length: "+length, TLSError.record_overflow);
}
// Can pretty much assume that if I'm here, I've got a default config, so let's use it.
if (ver != _securityParameters.version ) {
throw new TLSError("Unsupported TLS version: "+ver.toString(16), TLSError.protocol_version);
}
p = new ByteArray;
var actualLength:uint = Math.min(stream.bytesAvailable, length);
stream.readBytes(p, 0, actualLength);
if (actualLength == length) {
parseOneRecord(type, length, p);
} else {
_packetQueue.push({type:type, length:length, data:p});
}
}
}