public long[] ReadLongArray(int tag, bool isRequire) { long[] lr = null; if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.LIST: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } lr = new long[size]; for (int i = 0; i < size; ++i) { lr[i] = ReadLong(0, true); } break; } default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(lr); }
internal static object Read(Type type, int tag, bool isRequire, TarsInputStream jis) { TarsStructInfo info = TarsHelper.GetStructInfo(type); if (info == null) { throw new TarsDecodeException("the class type[" + type.FullName + "] no attribute Struct"); } if (jis.SkipToTag(tag)) { HeadData hd = new HeadData(); jis.ReadHead(hd); if (hd.Type != TarsStructBase.STRUCT_BEGIN) { throw new TarsDecodeException("type mismatch."); } Object result = info.ConstructorInvoker.Invoke(); List <TarsStructPropertyInfo> propertysList = info.PropertyList; foreach (var propertyInfo in propertysList) { Object value = jis.Read(propertyInfo.Type, propertyInfo.Order, propertyInfo.IsRequire); propertyInfo.PropertyAccessor.SetValue(result, value); } jis.SkipToStructEnd(); return(result); } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(null); }
public TarsStructBase ReadTarsStructBase(Type type, int tag, bool isRequire) { TarsStructBase reff = null; if (SkipToTag(tag)) { try { reff = (TarsStructBase)Activator.CreateInstance(type); } catch (Exception e) { throw new TarsDecodeException(e.Message); } var hd = new HeadData(); ReadHead(hd); if (hd.Type != TarsStructBase.STRUCT_BEGIN) { throw new TarsDecodeException("type mismatch."); } reff.ReadFrom(this); SkipToStructEnd(); } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(reff); }
public long ReadLong(int tag, bool isRequire) { if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.ZERO_TAG: return(0x0); case TarsStructBase.BYTE: return((int)buffer.ReadByte()); case TarsStructBase.SHORT: return(buffer.ReadShort()); case TarsStructBase.INT: return(buffer.ReadInt()); case TarsStructBase.LONG: return(buffer.ReadLong()); default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(0x0); }
public double ReadDouble(int tag, bool isRequire) { if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.ZERO_TAG: return(0x0); case TarsStructBase.FLOAT: return(buffer.ReadFloat()); case TarsStructBase.DOUBLE: return(buffer.ReadDouble()); default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(0x0); }
/// <summary> /// 读取头信息,但不移动缓冲区的当前偏移 /// </summary> /// <param name="hd"></param> /// <returns></returns> private int PeakHead(HeadData hd) { int len = ReadHead(hd); Skip(-1 * len); return(len); }
public TarsStructBase DirectRead(TarsStructBase o, int tag, bool isRequire) { TarsStructBase reff = null; if (SkipToTag(tag)) { try { reff = o.NewInit(); } catch (Exception e) { throw new TarsDecodeException(e.Message); } var hd = new HeadData(); ReadHead(hd); if (hd.Type != TarsStructBase.STRUCT_BEGIN) { throw new TarsDecodeException("type mismatch."); } reff.ReadFrom(this); SkipToStructEnd(); } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(reff); }
/// <summary> /// 跳到指定的tag的数据之前 /// </summary> /// <param name="tag"></param> /// <returns></returns> public bool SkipToTag(int tag) { try { var hd = new HeadData(); while (true) { int len = PeakHead(hd); if (hd.Type == TarsStructBase.STRUCT_END) { return(false); } if (tag <= hd.Tag) { return(tag == hd.Tag); } Skip(len); SkipField(hd.Type); } } catch (IndexOutOfRangeException e) { } catch (TarsDecodeException e) { } return(false); }
/// <summary> /// 跳过一个字段 /// </summary> private void SkipField() { var hd = new HeadData(); ReadHead(hd); SkipField(hd.Type); }
public string ReadString(int tag, bool isRequire) { if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.STRING1: { int len = buffer.ReadByte(); if (len < 0) { len += 256; } byte[] ss = new byte[len]; buffer.ReadBytes(ss); try { return(sServerEncoding.GetString(ss)); } catch { return(Encoding.UTF8.GetString(ss)); } } case TarsStructBase.STRING4: { int len = buffer.ReadInt(); if (len > TarsStructBase.MAX_STRING_LENGTH || len < 0) { throw new TarsDecodeException("string too long: " + len); } byte[] ss = new byte[len]; buffer.ReadBytes(ss); try { return(sServerEncoding.GetString(ss)); } catch { return(Encoding.UTF8.GetString(ss)); } } default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(null); }
/// <summary> /// 跳到当前结构体的结束位置 /// </summary> public void SkipToStructEnd() { var hd = new HeadData(); do { ReadHead(hd); SkipField(hd.Type); } while (hd.Type != TarsStructBase.STRUCT_END); }
public byte[] ReadByteArray(int tag, bool isRequire) { byte[] lr = null; if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.SIMPLE_LIST: { var hh = new HeadData(); ReadHead(hh); if (hh.Type != TarsStructBase.BYTE) { throw new TarsDecodeException("type mismatch, tag: " + tag + ", type: " + hd.Type + ", " + hh.Type); } int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("invalid size, tag: " + tag + ", type: " + hd.Type + ", " + hh.Type + ", size: " + size); } lr = new byte[size]; buffer.ReadBytes(lr); break; } case TarsStructBase.LIST: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } lr = new byte[size]; for (int i = 0; i < size; ++i) { lr[i] = ReadByte(0, true); } break; } default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(lr); }
/// <summary> /// 读取数据头 /// </summary> /// <param name="hd"></param> /// <param name="bb"></param> /// <returns></returns> public int ReadHead(HeadData hd, IByteBuffer bb) { byte b = bb.ReadByte(); hd.Type = (byte)(b & 15); hd.Tag = ((b & (15 << 4)) >> 4); if (hd.Tag == 15) { hd.Tag = bb.ReadByte() & 0x00ff; return(2); } return(1); }
public IDictionary ReadMap(Type type, int tag, bool isRequire) { IDictionary dic = (IDictionary)Activator.CreateInstance(type); Type[] argsType = type.GetGenericArguments(); var keyType = argsType[0]; var valueType = argsType[1];; if (SkipToTag(tag)) { HeadData hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.MAP: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } for (int i = 0; i < size; ++i) { var mk = Read(keyType, 0, true); var mv = Read(valueType, 1, true); if (dic.Contains(mk)) { dic[mk] = mv; } else { dic.Add(mk, mv); } } } break; default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(dic); }
public IDictionary <K, V> ReadMap <K, V>(IDictionary <K, V> m, int tag, bool isRequire) { if (m == null) { return(null); } if (SkipToTag(tag)) { HeadData hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.MAP: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } for (int i = 0; i < size; ++i) { K mk = (K)Read(typeof(K), 0, true); V mv = (V)Read(typeof(V), 1, true); if (m.ContainsKey(mk)) { m[mk] = mv; } else { m.Add(mk, mv); } } } break; default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(m); }
internal Dictionary <string, string> ReadMap(int tag, bool isRequire) { Dictionary <string, string> dic = new Dictionary <string, string>(); if (SkipToTag(tag)) { HeadData hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.MAP: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } for (int i = 0; i < size; ++i) { string mk = ReadString(0, true); string mv = ReadString(1, true); if (dic.ContainsKey(mk)) { dic[mk] = mv; } else { dic.Add(mk, mv); } } } break; default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(dic); }
public IList ReadList(Type type, int tag, bool isRequire) { IList list = (IList)Activator.CreateInstance(type); Type[] argsType = type.GetGenericArguments(); var valueType = argsType[0]; if (SkipToTag(tag)) { var hd = new HeadData(); ReadHead(hd); switch (hd.Type) { case TarsStructBase.LIST: { int size = ReadInt(0, true); if (size < 0) { throw new TarsDecodeException("size invalid: " + size); } for (int i = 0; i < size; ++i) { var t = Read(valueType, 0, true); list.Add(t); } break; } default: throw new TarsDecodeException("type mismatch."); } } else if (isRequire) { throw new TarsDecodeException("require field not exist."); } return(list); }
public int ReadHead(HeadData hd) => ReadHead(hd, buffer);
private void SkipField(byte type) { switch (type) { case TarsStructBase.BYTE: Skip(1); break; case TarsStructBase.SHORT: Skip(2); break; case TarsStructBase.INT: Skip(4); break; case TarsStructBase.LONG: Skip(8); break; case TarsStructBase.FLOAT: Skip(4); break; case TarsStructBase.DOUBLE: Skip(8); break; case TarsStructBase.STRING1: { int len = buffer.ReadByte(); if (len < 0) { len += 256; } Skip(len); break; } case TarsStructBase.STRING4: { Skip(buffer.ReadInt()); break; } case TarsStructBase.MAP: { int size = ReadInt(0, true); for (int i = 0; i < size * 2; ++i) { SkipField(); } break; } case TarsStructBase.LIST: { int size = ReadInt(0, true); for (int i = 0; i < size; ++i) { SkipField(); } break; } case TarsStructBase.SIMPLE_LIST: { var hd = new HeadData(); ReadHead(hd); if (hd.Type != (TarsStructBase.BYTE)) { throw new TarsDecodeException("skipField with invalid type, type value: " + type + ", " + hd.Type); } int size = ReadInt(0, true); Skip(size); break; } case TarsStructBase.STRUCT_BEGIN: SkipToStructEnd(); break; case TarsStructBase.STRUCT_END: case TarsStructBase.ZERO_TAG: break; default: throw new TarsDecodeException("invalid type."); } }