public bool DecodeArrayLen(MsgPackCode code, out uint len) { len = 0; if (code == MsgPackCode.CodeUnkonw) { if (!ReadCode(out code)) { return(false); } } switch (code) { case MsgPackCode.CodeArray16: ushort v16 = BigEndian.Uint16(buffer); len = (uint)v16; return(true); case MsgPackCode.CodeArray32: uint v32 = BigEndian.Uint32(buffer); len = (uint)v32; return(true); default: if (code >= MsgPackCode.CodeFixedArrayLow && code <= MsgPackCode.CodeFixedArrayHigh) { len = (uint)code & (uint)MsgPackCode.FixedArrayMask; return(true); } return(false); } }
public bool BinaryLen(MsgPackCode code, out int len) { len = 0; if (code == MsgPackCode.CodeUnkonw) { if (!ReadCode(out code)) { return(false); } } switch (code) { case MsgPackCode.CodeBin8: len = (int)buffer.ReadByte(); return(true); case MsgPackCode.CodeBin16: len = (int)BigEndian.Uint16(buffer); return(true); case MsgPackCode.CodeBin32: len = (int)BigEndian.Uint32(buffer); return(true); } return(false); }
public bool DecodeExtValue(MsgPackCode code, out byte[] valuedata) { valuedata = null; if (code == MsgPackCode.CodeUnkonw) { if (!ReadCode(out code)) { return(false); } } uint btlen = 0; switch (code) { case MsgPackCode.CodeFixExt1: btlen = 2; break; case MsgPackCode.CodeFixExt2: btlen = 3; break; case MsgPackCode.CodeFixExt4: btlen = 5; break; case MsgPackCode.CodeFixExt8: btlen = 9; break; case MsgPackCode.CodeFixExt16: btlen = 17; break; case MsgPackCode.CodeExt8: byte bt = (byte)buffer.ReadByte(); btlen = (uint)bt + 1; break; case MsgPackCode.CodeExt16: ushort v16 = BigEndian.Uint16(buffer); btlen = (uint)v16 + 1; break; case MsgPackCode.CodeExt32: uint v32 = BigEndian.Uint32(buffer); btlen = v32 + 1; break; default: return(false); } valuedata = new byte[btlen]; if (buffer.Read(valuedata, 0, (int)btlen) != btlen) { valuedata = null; return(false); } return(true); }
public bool DecodeMapLen(MsgPackCode code, out uint len) { len = 0; if (code == MsgPackCode.CodeUnkonw) { bool result = ReadCode(out code); if (!result) { return(false); } } switch (code) { case MsgPackCode.CodeMap16: ushort v = BigEndian.Uint16(buffer); len = (uint)v; return(true); case MsgPackCode.CodeMap32: len = BigEndian.Uint32(buffer); return(true); default: if (code >= MsgPackCode.CodeFixedMapLow && code <= MsgPackCode.CodeFixedMapHigh) { len = (uint)code & (uint)MsgPackCode.FixedMapMask; return(true); } return(false); } }
void WriteUint64(ulong v, MsgPackCode code) { if (code != MsgPackCode.CodeUnkonw) { databuffer.WriteByte((byte)code); } BigEndian.PutUint64(databuffer, v); }
public bool ReadCode(out MsgPackCode code) { if (buffer.Length - buffer.Position < 1) { code = MsgPackCode.CodeUnkonw; return(false); } code = (MsgPackCode)buffer.ReadByte(); return(true); }
public bool DecodeBinary(MsgPackCode code, out byte[] value) { int btlen; value = null; if (!BinaryLen(code, out btlen)) { return(false); } if (btlen > 0) { value = new byte[btlen]; buffer.Read(value, 0, btlen); } return(true); }
public bool DecodeString(MsgPackCode code, out string value) { value = ""; if (code == MsgPackCode.CodeUnkonw) { bool result = ReadCode(out code); if (!result) { return(false); } } int strlen = 0; switch (code) { case MsgPackCode.CodeStr8: strlen = buffer.ReadByte(); break; case MsgPackCode.CodeStr16: strlen = (int)BigEndian.Uint16(buffer); break; case MsgPackCode.CodeStr32: strlen = (int)BigEndian.Uint32(buffer); break; default: if ((int)code < 0xa0 || (int)code > 0xbf) { value = ""; return(false); } strlen = (int)code & (int)MsgPackCode.FixedStrMask; break; } if (strlen > 0) { byte[] strbyte = new byte[strlen]; int readlen = buffer.Read(strbyte, 0, strlen); value = System.Text.Encoding.UTF8.GetString(strbyte); return(strlen == readlen); } return(false); }
public bool DecodeMap(MsgPackCode code, out DxRecordValue value) { value = null; if (code == MsgPackCode.CodeUnkonw) { bool result = ReadCode(out code); if (!result) { return(false); } } uint maplen; if (!DecodeMapLen(code, out maplen)) { return(false); } if (maplen == 0) { return(true); } if (!ReadCode(out code) || !IsStr(code)) { return(false); } value = new DxRecordValue(); if (!DecodeMapKv(value, code)) { return(false); } for (int i = 1; i < maplen; i++) { if (!DecodeMapKv(value, MsgPackCode.CodeUnkonw)) { return(false); } } return(true); }
public bool Decode2Array(MsgPackCode code, DxArrayValue value) { if (code == MsgPackCode.CodeUnkonw) { if (!ReadCode(out code)) { return(false); } } uint arrlen; if (!DecodeArrayLen(code, out arrlen)) { return(false); } for (int i = 0; i < arrlen; i++) { if (!DecodeArrayElement(value, i)) { return(false); } } return(true); }
public static bool IsStr(MsgPackCode code) { return(code >= MsgPackCode.CodeFixedStrLow && code <= MsgPackCode.CodeFixedStrHigh || code >= MsgPackCode.CodeStr8 && code <= MsgPackCode.CodeStr32); }
public bool DecodeMapKv(DxRecordValue mapValue, MsgPackCode code) { string key; if (!DecodeString(code, out key)) { return(false); } if (!ReadCode(out code)) { return(false); } if (IsStr(code)) { string value; if (!DecodeString(code, out value)) { return(false); } mapValue.SetString(key, value); } else if (IsFixedNum(code)) { mapValue.SetInt(key, (int)(sbyte)code); } else if (IsInt(code)) { long v; if (!DecodeInt(code, out v)) { return(false); } if (v > int.MaxValue || v < int.MinValue) { mapValue.SetInt64(key, v); } else { mapValue.SetInt(key, (int)v); } } else if (IsMap(code)) { DxRecordValue v; if (!DecodeMap(code, out v)) { return(false); } mapValue[key] = v; } else if (IsArray(code)) { DxArrayValue arr = new DxArrayValue(); if (!Decode2Array(code, arr)) { return(false); } mapValue[key] = arr; } else if (IsBin(code)) { byte[] binary; if (!DecodeBinary(code, out binary)) { return(false); } if (binary != null) { DxBinaryValue binaryValue = new DxBinaryValue(); binaryValue.Bytes = binary; mapValue[key] = binaryValue; } } else if (IsExt(code)) { byte[] binary; if (!DecodeExtValue(code, out binary)) { return(false); } if (binary.Length == 13 && (sbyte)binary[0] == -1) //96位日期格式 { //32位纳秒,64位秒 Stream stream = new MemoryStream(binary); stream.Position = 1; uint nsec = BigEndian.Uint32(stream); ulong sec = BigEndian.Uint64(stream); stream.Dispose(); System.DateTime dateTime = new System.DateTime(1970, 1, 1); double sec1 = nsec / (1000 * 1000); mapValue.SetDateTime(key, dateTime.AddSeconds(sec + sec1)); } else { mapValue[key] = new DxExtValue(binary); } } else { switch (code) { case MsgPackCode.CodeTrue: mapValue.SetBool(key, true); return(true); case MsgPackCode.CodeFalse: mapValue.SetBool(key, false); return(true); case MsgPackCode.CodeNil: mapValue[key] = null; return(true); case MsgPackCode.CodeFloat: uint v = BigEndian.Uint32(buffer); mapValue.SetFloat(key, (float)System.BitConverter.Int64BitsToDouble((long)v)); return(true); case MsgPackCode.CodeDouble: ulong v1 = BigEndian.Uint64(buffer); mapValue.SetDouble(key, System.BitConverter.Int64BitsToDouble((long)v1)); return(true); case MsgPackCode.CodeFixExt4: if (!ReadCode(out code)) { return(false); } if ((sbyte)code == -1) //时间 { uint seconds = BigEndian.Uint32(buffer); System.DateTime dateTime = new System.DateTime(1970, 1, 1); mapValue.SetDateTime(key, dateTime.AddSeconds((double)seconds)); } else { byte[] bt = new byte[5]; buffer.Read(bt, 1, 4); DxExtValue dxExtValue = new DxExtValue(bt); mapValue[key] = dxExtValue; } return(true); case MsgPackCode.CodeFixExt8: //64位时间格式 if (!ReadCode(out code)) { return(false); } if ((sbyte)code == -1) //时间 { ulong seconds = BigEndian.Uint64(buffer); long nsec = (long)(seconds >> 34); seconds &= 0x00000003ffffffff; System.DateTime dateTime = new System.DateTime(1970, 1, 1); double sec1 = nsec / (1000 * 1000); mapValue.SetDateTime(key, dateTime.AddSeconds(seconds + sec1)); } else { byte[] bt = new byte[9]; buffer.Read(bt, 1, 8); DxExtValue dxExtValue = new DxExtValue(bt); mapValue[key] = dxExtValue; } return(true); } } return(true); }
public static bool IsInt(MsgPackCode code) { return(code <= MsgPackCode.PosFixedNumHigh || code >= MsgPackCode.NegFixedNumLow || code >= MsgPackCode.CodeUint8 && code <= MsgPackCode.CodeUint64 || code >= MsgPackCode.CodeInt8 && code <= MsgPackCode.CodeInt64); }
public static bool IsFixedNum(MsgPackCode code) { return(code <= MsgPackCode.PosFixedNumHigh || code >= MsgPackCode.NegFixedNumLow); }
public static bool IsMap(MsgPackCode code) { return(code >= MsgPackCode.CodeFixedMapLow && code <= MsgPackCode.CodeFixedMapHigh || code == MsgPackCode.CodeMap16 || code == MsgPackCode.CodeMap32); }
public static bool IsExt(MsgPackCode code) { return((code >= MsgPackCode.CodeFixExt1 && code <= MsgPackCode.CodeFixExt16 && code != MsgPackCode.CodeFixExt4 && code != MsgPackCode.CodeFixExt8) || (code >= MsgPackCode.CodeExt8 && code <= MsgPackCode.CodeExt32)); }
public static bool IsArray(MsgPackCode code) { return(code >= MsgPackCode.CodeFixedArrayLow && code <= MsgPackCode.CodeFixedArrayHigh || code == MsgPackCode.CodeArray16 || code == MsgPackCode.CodeArray32); }
public static bool IsBin(MsgPackCode code) { return(code >= MsgPackCode.CodeBin8 && code <= MsgPackCode.CodeBin32); }
public bool DecodeInt(MsgPackCode code, out long value) { value = 0; if (code == MsgPackCode.CodeUnkonw) { bool result = ReadCode(out code); if (!result) { return(false); } } if (code <= MsgPackCode.PosFixedNumHigh) { value = (long)code; return(true); } if ((int)code >= 0xe0) { value = (long)((sbyte)code); return(true); } switch (code) { case MsgPackCode.CodeInt8: case MsgPackCode.CodeUint8: byte bt = (byte)buffer.ReadByte(); if (code == MsgPackCode.CodeInt8) { value = (long)((sbyte)bt); } else { value = (long)bt; } return(true); case MsgPackCode.CodeInt16: case MsgPackCode.CodeUint16: ushort word = BigEndian.Uint16(buffer); if (code == MsgPackCode.CodeInt16) { value = (long)((short)word); } else { value = (long)word; } return(true); case MsgPackCode.CodeInt32: case MsgPackCode.CodeUint32: uint dword = BigEndian.Uint32(buffer); if (code == MsgPackCode.CodeInt32) { value = (long)(int)code; } else { value = (long)code; } return(true); case MsgPackCode.CodeInt64: case MsgPackCode.CodeUint64: ulong w = BigEndian.Uint64(buffer); value = (long)w; return(true); default: return(false); } }