private static void SetHeader16(byte[] buffer, FieldEncodingType fieldencodingtype, Int16 encodedLength) { //Debug.WriteLine($"Set16: FieldType: {fieldencodingtype}\tencodedLength: {encodedLength}"); SetFieldEncodingType(buffer, fieldencodingtype); buffer[__HEADER_ENDCODEDLENGTH + 1] = (byte)(encodedLength >> 0); buffer[__HEADER_ENDCODEDLENGTH + 0] = (byte)(encodedLength >> 8); }
public static Int64 GetInt64(byte[] buffer, ref int fieldBufferBase) { Int64 decodedValue = default(Int64); Int64 value; Int32 encodedLength = 0; FieldEncodingType fieldencodingtype = GetFieldEncodingType(buffer, fieldBufferBase); switch (fieldencodingtype) { case FieldEncodingType.Int64Hdr16: case FieldEncodingType.UInt64Hdr16: { encodedLength = GetEncodedLength16(buffer, fieldBufferBase); value = -encodedLength; encodedLength = 0; decodedValue = value; break; } case FieldEncodingType.Int64Hdr32: case FieldEncodingType.UInt64Hdr32: { encodedLength = GetEncodedLength32(buffer, fieldBufferBase); value = -encodedLength; encodedLength = (__HEADER32_LENGTH - __HEADER16_LENGTH); decodedValue = value; break; } case FieldEncodingType.Int64Hdr16Data: case FieldEncodingType.UInt64Hdr16Data: { encodedLength = GetEncodedLength16(buffer, fieldBufferBase); value = ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 7] << 0) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 6] << 8) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 5] << 16) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 4] << 24) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 3] << 32) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 2] << 40) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 1] << 48) | ((Int64)buffer[fieldBufferBase + __HEADER16_LENGTH + 0] << 56); decodedValue = value; break; } default: // e.g. FieldEncodingType.Unknown { encodedLength = buffer.Length - __HEADER16_LENGTH; decodedValue = default(Int64); break; } } fieldBufferBase += __HEADER16_LENGTH + encodedLength; return(decodedValue); }
private static void SetHeader32(byte[] buffer, FieldEncodingType fieldencodingtype, byte b0, byte b1, byte b2, byte b3) { //Debug.WriteLine($"Set32b: FieldType: {fieldencodingtype}\tb0,b1,b2,b3: {b0} {b1} {b2} {b3}"); SetFieldEncodingType(buffer, fieldencodingtype); buffer[__HEADER_ENDCODEDLENGTH + 3] = b3; buffer[__HEADER_ENDCODEDLENGTH + 2] = b2; buffer[__HEADER_ENDCODEDLENGTH + 1] = b1; buffer[__HEADER_ENDCODEDLENGTH + 0] = b0; }
private void SerializeField(StringBuilder sb, FieldInfo fieldInfo, int tabSize) { var fieldAttr = fieldInfo.GetCustomAttribute <FieldAttribute>(); var fieldType = fieldInfo.FieldType; var isMessage = fieldType.GetCustomAttribute <MessageAttribute>() != null; var ind = "".PadLeft(tabSize, ' '); sb.Append(ind + "{\n"); var indentation = ind + " "; FieldEncodingType encodingType = GetFieldEncodingType(fieldType); int head = (fieldAttr.ID << 3) | (int)encodingType; sb.Append(indentation + WriteFiledComment(fieldInfo)); sb.AppendFormat(indentation + WriteLiteral32(fieldInfo.Name + "_head", head)); if (encodingType == FieldEncodingType.FieldEncodingType32Bits || encodingType == FieldEncodingType.FieldEncodingType64Bits) { sb.AppendFormat(indentation + WriteLVal(fieldInfo.Name + "_", encodingType == FieldEncodingType.FieldEncodingType32Bits ? 4 : 8)); } else if (fieldType == typeof(string)) { sb.AppendFormat(indentation + WriteRVal(fieldInfo.Name, fieldInfo.Name + "_.length()", 32)); sb.AppendFormat(indentation + "ss << {0}_;\n", fieldInfo.Name); } else if (isMessage) { string strName = string.Format("tmp_{0}", fieldInfo.Name); sb.AppendFormat(indentation + "std::string {0} = {1}_.SerializeAsString();\n", strName, fieldInfo.Name); sb.AppendFormat(indentation + WriteRVal(strName, strName + ".length()", 32)); sb.AppendFormat(indentation + "ss << {0};\n", strName); } sb.Append(ind + "}\n"); }
private void DeseializeField(StringBuilder sb, FieldInfo fieldInfo, int tabSize) { var fieldAttr = fieldInfo.GetCustomAttribute <FieldAttribute>(); var fieldType = fieldInfo.FieldType; var isMessage = fieldType.GetCustomAttribute <MessageAttribute>() != null; var ind = "".PadLeft(tabSize, ' '); sb.AppendFormat(" else if (tag == {0}) {{\n", fieldAttr.ID); sb.Append(ind + WriteFiledComment(fieldInfo)); FieldEncodingType encodingType = GetFieldEncodingType(fieldType); if (encodingType == FieldEncodingType.FieldEncodingType32Bits || encodingType == FieldEncodingType.FieldEncodingType64Bits) { sb.AppendFormat(ind + ReadVal(fieldInfo.Name + "_", encodingType == FieldEncodingType.FieldEncodingType32Bits ? 4 : 8)); } else if (fieldType == typeof(string)) { sb.AppendFormat(ind + "int length = 0;\n"); sb.AppendFormat(ind + ReadVal("length", 4)); sb.AppendFormat(ind + "{0}_.resize(length);\n", fieldInfo.Name); sb.AppendFormat(ind + ReadString(fieldInfo.Name + "_", "length")); } else if (isMessage) { sb.AppendFormat(ind + "int length = 0;\n"); sb.AppendFormat(ind + ReadVal("length", 4)); sb.AppendFormat(ind + "std::string buffer;\n"); sb.AppendFormat(ind + "buffer.resize(length);\n"); sb.AppendFormat(ind + ReadString("buffer", "length")); sb.AppendFormat(ind + "{0}_.ParseFromString(buffer);\n", fieldInfo.Name); } sb.AppendFormat("".PadLeft(tabSize - 2, ' ') + "}}"); }
public static byte[] GetFieldBuffer(UInt64 value) { byte[] buffer = GetFieldBuffer((Int64)value); FieldEncodingType fieldencodingtype = GetFieldEncodingType(buffer, 0); switch (fieldencodingtype) { case FieldEncodingType.Int64Hdr16: { SetFieldEncodingType(buffer, FieldEncodingType.UInt64Hdr16); break; } case FieldEncodingType.Int64Hdr32: { SetFieldEncodingType(buffer, FieldEncodingType.UInt64Hdr32); break; } case FieldEncodingType.Int64Hdr16Data: { SetFieldEncodingType(buffer, FieldEncodingType.UInt64Hdr16Data); break; } } return(buffer); }
public static void SetFieldEncodingType(byte[] fieldBuffer, FieldEncodingType fieldencodingtype) // 16 and 32 { fieldBuffer[__HEADER_FIELDENCODINGTYPE] = (byte)fieldencodingtype; }
public const Int32 ENCODED32_VALUE_MAX = 0x0fffffff; // 268,435,455 public static FieldEncodingType GetFieldEncodingType(byte[] fieldBuffer, int fieldBufferBase) // 16 and 32 { FieldEncodingType fieldencodingtype = (FieldEncodingType)fieldBuffer[fieldBufferBase + __HEADER_FIELDENCODINGTYPE]; return(fieldencodingtype); }
public static byte[] GetByteArray(byte[] buffer, ref int fieldBufferBase, FieldEncodingType fieldEncodingOverride = FieldEncodingType.NoOverride) { byte[] decodedValue = default(byte[]); FieldEncodingType fieldencodingtype = GetFieldEncodingType(buffer, fieldBufferBase); if (fieldEncodingOverride != FieldEncodingType.NoOverride) { fieldencodingtype = fieldEncodingOverride; //Guid } switch (fieldencodingtype) { case FieldEncodingType.ByteArrayHdr16: { Int16 encodedLength = GetEncodedLength16(buffer, fieldBufferBase); if (encodedLength == __ENCODED16_NULL || encodedLength == __ENCODED16_ERROR || encodedLength == __ENCODED16_EXCEPTION) { decodedValue = null; encodedLength = 0; } else if (encodedLength >= -1 - byte.MaxValue && encodedLength <= -1) { decodedValue = new byte[1]; decodedValue[0] = (byte)(-encodedLength - 1); encodedLength = 0; } else if (encodedLength == 0) { decodedValue = Array.Empty <byte>(); encodedLength = 0; } else { // Should never end up here decodedValue = null; encodedLength = 0; } fieldBufferBase += __HEADER16_LENGTH + encodedLength; break; } case FieldEncodingType.ByteArrayHdr16Data: { Int16 encodedLength = GetEncodedLength16(buffer, fieldBufferBase); if (encodedLength > 0) { decodedValue = new byte[encodedLength]; for (int i = 0; i < encodedLength; i++) { decodedValue[i] = buffer[fieldBufferBase + __HEADER16_LENGTH + i]; } } else { // Should never end up here decodedValue = null; encodedLength = 0; } fieldBufferBase += __HEADER16_LENGTH + encodedLength; break; } case FieldEncodingType.ByteArrayHdr32: { Int32 encodedLength; switch (buffer[__HEADER_ENDCODEDLENGTH + 0]) { case __ENCODED32_ENCODEDVALUE_1BYTE: { // Should get here ever decodedValue = null; encodedLength = 0; break; } case __ENCODED32_ENCODEDVALUE_2BYTES: { decodedValue = new byte[2]; decodedValue[0] = buffer[fieldBufferBase + __HEADER_ENDCODEDLENGTH + 1]; decodedValue[1] = buffer[fieldBufferBase + __HEADER_ENDCODEDLENGTH + 2]; encodedLength = 0; break; } case __ENCODED32_ENCODEDVALUE_3BYTES: { decodedValue = new byte[3]; decodedValue[0] = buffer[fieldBufferBase + __HEADER_ENDCODEDLENGTH + 1]; decodedValue[1] = buffer[fieldBufferBase + __HEADER_ENDCODEDLENGTH + 2]; decodedValue[2] = buffer[fieldBufferBase + __HEADER_ENDCODEDLENGTH + 3]; encodedLength = 0; break; } default: { encodedLength = GetEncodedLength32(buffer, fieldBufferBase); decodedValue = new byte[encodedLength]; for (int i = 0; i < encodedLength; i++) { decodedValue[i] = buffer[fieldBufferBase + __HEADER32_LENGTH + i]; } break; } } fieldBufferBase += __HEADER32_LENGTH + encodedLength; break; } case FieldEncodingType.ByteArrayHdr32Data: { Int32 encodedLength; encodedLength = GetEncodedLength32(buffer, fieldBufferBase); if (encodedLength > 0) { decodedValue = new byte[encodedLength]; for (int i = 0; i < encodedLength; i++) { decodedValue[i] = buffer[fieldBufferBase + __HEADER32_LENGTH + i]; } } else { // Should never end up here decodedValue = null; encodedLength = 0; } fieldBufferBase += __HEADER32_LENGTH + encodedLength; break; } default: { fieldBufferBase += buffer.Length; decodedValue = null; break; } } return(decodedValue); }