public static FsshttpbResponse DeserializeResponseFromByteArray(byte[] byteArray, int startIndex)
{
int index = startIndex;
FsshttpbResponse response = new FsshttpbResponse();
response.ProtocolVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
index += 2;
response.MinimumVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
index += 2;
response.Signature = LittleEndianBitConverter.ToUInt64(byteArray, index);
index += 8;
int length = 0;
StreamObjectHeaderStart streamObjectHeader;
if ((length = StreamObjectHeaderStart.TryParse(byteArray, index, out streamObjectHeader)) == 0)
{
throw new ResponseParseErrorException(index, "Failed to parse the response header", null);
}
if (!(streamObjectHeader is StreamObjectHeaderStart32bit))
{
throw new ResponseParseErrorException(index, "Unexpected 16-bit response stream object header, expect 32-bit stream object header for Response", null);
}
if (streamObjectHeader.Type != StreamObjectTypeHeaderStart.FsshttpbResponse)
{
throw new ResponseParseErrorException(index, "Failed to extract the response header type, unexpected value " + streamObjectHeader.Type, null);
}
if (streamObjectHeader.Length != 1)
{
throw new ResponseParseErrorException(index, "Response object over-parse error", null);
}
index += length;
response.ResponseStart = streamObjectHeader as StreamObjectHeaderStart32bit;
response.Status = (byteArray[index] & 0x1) == 0x1 ? true : false;
response.Reserved = (byte)(byteArray[index] >> 1);
index += 1;
try
{
if (response.Status)
{
response.ResponseError = StreamObject.GetCurrent<ResponseError>(byteArray, ref index);
response.DataElementPackage = null;
response.CellSubResponses = null;
}
else
{
DataElementPackage package;
if (StreamObject.TryGetCurrent<DataElementPackage>(byteArray, ref index, out package))
{
response.DataElementPackage = package;
}
response.CellSubResponses = new List<FsshttpbSubResponse>();
FsshttpbSubResponse subResponse;
while (StreamObject.TryGetCurrent<FsshttpbSubResponse>(byteArray, ref index, out subResponse))
{
response.CellSubResponses.Add(subResponse);
}
}
response.ResponseEnd = BasicObject.Parse<StreamObjectHeaderEnd16bit>(byteArray, ref index);
}
catch (StreamObjectParseErrorException streamObjectException)
{
throw new ResponseParseErrorException(index, streamObjectException);
}
catch (DataElementParseErrorException dataElementException)
{
throw new ResponseParseErrorException(index, dataElementException);
}
catch (KnowledgeParseErrorException knowledgeException)
{
throw new ResponseParseErrorException(index, knowledgeException);
}
if (index != byteArray.Length)
{
throw new ResponseParseErrorException(index, "Failed to pass the whole response, not reach the end of the byte array", null);
}
return response;
}
}