void Read(CodedInputStream stream) { var links = new List <DagLink>(); bool done = false; while (!stream.IsAtEnd && !done) { var tag = stream.ReadTag(); switch (WireFormat.GetTagFieldNumber(tag)) { case 1: DataBytes = stream.ReadSomeBytes(stream.ReadLength()); done = true; break; case 2: using (var ms = new MemoryStream(stream.ReadSomeBytes(stream.ReadLength()))) { links.Add(new DagLink(ms)); } break; default: throw new InvalidDataException("Unknown field number"); } } if (DataBytes == null) { DataBytes = new byte[0]; } Links = links.ToArray(); }
object ReadValue(CodedInputStream inputStream, object value, Type type, Type targetType, IValueConverter converter) { if (type == typeof(Guid)) { _ = inputStream.ReadLength(); var guidAsBytes = inputStream.ReadBytes(); value = new Guid(guidAsBytes.ToByteArray()); } else if (type == typeof(string)) { _ = inputStream.ReadLength(); value = inputStream.ReadString(); } else if (type == typeof(int)) { value = inputStream.ReadInt32(); } else if (type == typeof(long)) { value = inputStream.ReadInt64(); } else if (type == typeof(uint)) { value = inputStream.ReadUInt32(); } else if (type == typeof(ulong)) { value = inputStream.ReadUInt64(); } else if (type == typeof(float)) { value = inputStream.ReadFloat(); } else if (type == typeof(double)) { value = inputStream.ReadDouble(); } else if (type == typeof(bool)) { value = inputStream.ReadBool(); } else if (type == typeof(DateTimeOffset) || type == typeof(DateTime)) { value = DateTimeOffset.FromUnixTimeMilliseconds(inputStream.ReadInt64()); if (type == typeof(DateTime)) { value = ((DateTimeOffset)value).UtcDateTime; } } if (converter != null) { value = converter.ConvertFrom(targetType, value); } return(value); }
public ProtoBufPackageInfo Filter(BufferList data, out int rest) { rest = 0; var buffStream = new BufferStream(); buffStream.Initialize(data); var stream = new CodedInputStream(buffStream); var varint32 = (int)stream.ReadLength(); if (varint32 <= 0) { return(default(ProtoBufPackageInfo)); } var total = data.Total; var packageLen = varint32 + (int)stream.Position; if (total >= packageLen) { rest = total - packageLen; stream = new CodedInputStream(buffStream); var body = stream.ReadBytes(); var message = MessageWrapper.Parser.ParseFrom(body); var requestInfo = new ProtoBufPackageInfo(message); return(requestInfo); } return(default(ProtoBufPackageInfo)); }
/// <summary> /// Adds the entries from the given input stream, decoding them with the specified codec. /// </summary> /// <param name="input">The input stream to read from.</param> /// <param name="codec">The codec to use in order to read each entry.</param> public void AddEntriesFrom(CodedInputStream input, FieldCodec <T> codec) { // TODO: Inline some of the Add code, so we can avoid checking the size on every // iteration. uint tag = input.LastTag; var reader = codec.ValueReader; // Non-nullable value types can be packed or not. if (FieldCodec <T> .IsPackedRepeatedField(tag)) { int length = input.ReadLength(); if (length > 0) { int oldLimit = input.PushLimit(length); while (!input.ReachedLimit) { Add(reader(input)); } input.PopLimit(oldLimit); } // Empty packed field. Odd, but valid - just ignore. } else { // Not packed... (possibly not packable) do { Add(reader(input)); } while (input.MaybeConsumeTag(tag)); } }
private Task <Request> HandleRequest(TcSocketConnection client) { var inputStream = new CodedInputStream(client.Connection.GetStream()); var outputStream = new CodedOutputStream(client.Connection.GetStream()); while (client.Connection.Connected) { Int32 varintLength = inputStream.ReadLength(); Console.WriteLine("message length: {0}", varintLength); if (varintLength > 4) { throw new System.ArgumentOutOfRangeException("varint"); } try { var request = Request.Parser.ParseFrom(inputStream.ReadBytes()); Console.WriteLine("message: {0} type {1}", request.CalculateSize(), request.ValueCase); outputStream.WriteBytes(inputStream.ReadBytes()); } catch (Exception ex) { } } return(null); }
void Read(CodedInputStream stream) { while (!stream.IsAtEnd) { var tag = stream.ReadTag(); switch (WireFormat.GetTagFieldNumber(tag)) { case 1: using (var ms = new MemoryStream(stream.ReadSomeBytes(stream.ReadLength()))) { Hash = new MultiHash(ms).ToBase58(); } break; case 2: Name = stream.ReadString(); break; case 3: Size = stream.ReadInt64(); break; default: throw new InvalidDataException("Unknown field number"); } } }
///<exception cref="Exception"></exception> private TMessage extractMessages <TMessage>(System.IO.Stream stream) where TMessage : IMessage <TMessage>, new() { TMessage result = default(TMessage); try { CodedInputStream protoStream = new CodedInputStream(stream); var msgBytesLength = protoStream.ReadLength() + protoStream.Position; if (stream.Length >= msgBytesLength) { var builder = new TMessage(); stream.SetLength(msgBytesLength); stream.Seek(protoStream.Position, SeekOrigin.Begin); builder.MergeFrom(stream); result = builder; } } catch (Exception ex) { Debug.WriteLine("fail to extract the proto message:" + ex.Message + Environment.NewLine + ex.StackTrace); } return(result); }
public Request HandleRequest() { var inputStream = new CodedInputStream(Connection.GetStream()); var outputStream = new CodedOutputStream(Connection.GetStream()); while (Connection.Connected) { Int32 varintLength = inputStream.ReadLength(); Console.WriteLine("message length: {0}", varintLength); // if (varintLength > 4) // { // throw new System.ArgumentOutOfRangeException("varint"); // } //try //{ var request = Request.Parser.ParseFrom(inputStream.ReadBytes()); //Console.WriteLine("New client message: {0} type {1}", request.CalculateSize(), request.ValueCase); outputStream.WriteBytes(inputStream.ReadBytes()); UpdateConnectionName(request); return request; //return request; //} //catch (Exception ex) //{ // throw ex; //} } return null; }
public void AddEntriesFrom(CodedInputStream input, FieldCodec <T> codec) { // TODO: Inline some of the Add code, so we can avoid checking the size on every // iteration and the mutability. uint tag = input.LastTag; var reader = codec.ValueReader; // Value types can be packed or not. if (typeof(T).IsValueType() && WireFormat.GetTagWireType(tag) == WireFormat.WireType.LengthDelimited) { int length = input.ReadLength(); if (length > 0) { int oldLimit = input.PushLimit(length); while (!input.ReachedLimit) { Add(reader(input)); } input.PopLimit(oldLimit); } // Empty packed field. Odd, but valid - just ignore. } else { // Not packed... (possibly not packable) do { Add(reader(input)); } while (input.MaybeConsumeTag(tag)); } }
private static ValueType HandleValueType(CodedInputStream cis) { var valueType = new ValueType(); long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: valueType.Type = cis.ReadInt64(); break; case 2: valueType.Unit = cis.ReadInt64(); break; default: cis.SkipLastField(); break; } } } return(valueType); }
private static T ReadSingleField <T>(CodedInputStream input, Func <CodedInputStream, T> read) { var length = input.ReadLength(); var endPosition = input.Position + length; uint number; WireType wireType; var value = default(T); while (input.Position < endPosition && input.TryReadTag(out number, out wireType)) { switch (number) { case 1: value = read.Invoke(input); break; default: SkipUnknown(input, wireType); break; } } return(value); }
private static OriginatorInfo ReadOriginatorInfo(CodedInputStream input) { var length = input.ReadLength(); var endPosition = input.Position + length; uint number; WireType wireType; var senderId = new PeerId(); string senderEndPoint = null; string initiatorUserName = null; while (input.Position < endPosition && input.TryReadTag(out number, out wireType)) { switch (number) { case 1: senderId = ReadPeerId(input); break; case 2: senderEndPoint = input.ReadString(); break; case 5: initiatorUserName = input.ReadString(); break; default: SkipUnknown(input, wireType); break; } } return(new OriginatorInfo(senderId, senderEndPoint, null, initiatorUserName)); }
private static Location HandleLocation(CodedInputStream cis) { var location = new Location(); var lines = new List <Line>(); location.Lines = lines; long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: location.Id = cis.ReadUInt64(); break; case 2: location.MappingId = cis.ReadUInt64(); break; case 3: location.Address = cis.ReadUInt64(); break; case 4: lines.Add(HandleLine(cis)); break; case 5: location.IsFolded = cis.ReadInt32() != 0; break; default: cis.SkipLastField(); break; } } else { break; } } return(location); }
private Task <Request> HandleRequest(TcSocketConnection client) { var inputStream = new CodedInputStream(client.Connection.GetStream()); var outputStream = new CodedOutputStream(client.Connection.GetStream()); //while(client.Connected) //{ Int32 varintLength = inputStream.ReadLength(); Console.WriteLine("message length: {0}", varintLength); if (varintLength > 4) { throw new System.ArgumentOutOfRangeException("varint"); } try { var request = Request.Parser.ParseFrom(inputStream.ReadBytes()); Console.WriteLine("message: {0} type {1}", request.CalculateSize(), request.ValueCase); outputStream.WriteBytes(inputStream.ReadBytes()); } catch (Exception ex) { } //byte[] msgLengthField = new byte[varintLength]; //stream.ReadAsync(msgLengthField, 1, varintLength); //Int64 msgLengthLong = BitConverter.ToInt64(msgLengthField, 0); //if (msgLengthLong > Int32.MaxValue) //{ // throw new System.ArgumentOutOfRangeException("messageLength"); //} //Int32 messageLength = (Int32)msgLengthLong; //CodedInputStream. //Request request = Request.Parser.ParseFrom() //} //if(varintLength) return(null); }
/// <summary> /// Reads the binary representation of the CID from the specified <see cref="CodedInputStream"/>. /// </summary> /// <param name="stream"> /// The <see cref="CodedInputStream"/> to read from. /// </param> /// <returns> /// A new <see cref="Cid"/>. /// </returns> public static Cid Read(CodedInputStream stream) { var cid = new Cid(); var length = stream.ReadLength(); if (length == 34) { cid.Version = 0; } else { cid.Version = stream.ReadInt32(); cid.ContentType = stream.ReadMultiCodec().Name; } cid.Hash = new MultiHash(stream); return cid; }
private static Label HandleLabel(CodedInputStream cis) { var label = new Label(); long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: label.Key = cis.ReadInt64(); break; case 2: label.Str = cis.ReadInt64(); break; case 3: label.Num = cis.ReadInt64(); break; case 4: label.NumUnit = cis.ReadInt64(); break; default: cis.SkipLastField(); break; } } else { break; } } return(label); }
public async Task <byte[]> ReadFile(CancellationToken token = default) { var length = pbInputStream.ReadLength(); var readed = 0; var result = new byte[length]; while (length - readed > 0) { var r = await stream.ReadAsync(result, readed, length - readed, token); readed += r; } pbOutputStream.WriteMessage(new Proto.Action { Type = Proto.Action.Types.ActionType.Default }); pbOutputStream.Flush(); return(result); }
void Read(CodedInputStream stream) { byte code = (byte)stream.ReadTag(); byte digestSize = (byte)stream.ReadLength(); Algorithm = HashingAlgorithm.Codes[code]; if (Algorithm == null) { Algorithm = HashingAlgorithm.Register("ipfs-" + code, code, digestSize); RaiseUnknownHashingAlgorithm(Algorithm); } else if (digestSize != Algorithm.DigestSize) { throw new InvalidDataException(string.Format("The digest size {0} is wrong for {1}; it should be {2}.", digestSize, Algorithm.Name, Algorithm.DigestSize)); } Digest = stream.ReadSomeBytes(digestSize); }
void Read(CodedInputStream stream) { var code = stream.ReadInt32(); var digestSize = stream.ReadLength(); HashingAlgorithm.Codes.TryGetValue(code, out HashingAlgorithm a); Algorithm = a; if (Algorithm == null) { Algorithm = HashingAlgorithm.Register("ipfs-" + code, code, digestSize); RaiseUnknownHashingAlgorithm(Algorithm); } else if (Algorithm.DigestSize != 0 && digestSize != Algorithm.DigestSize) { throw new InvalidDataException(string.Format("The digest size {0} is wrong for {1}; it should be {2}.", digestSize, Algorithm.Name, Algorithm.DigestSize)); } Digest = stream.ReadSomeBytes(digestSize); }
/// <summary> /// Reads one byte of a varint into lengthStream /// </summary> /// <param name="s">The stream to read</param> /// <returns> -1 if not finished reading or the value of the varint if finished </returns> private int ReadVarintByte(MemoryStream s) { byte nextByte = (byte)s.ReadByte(); State.LengthStream.WriteByte(nextByte); int endOfInt = 0x80 & nextByte; //The most significant bit of each byte is set when there is still more to read if (endOfInt == 0) { //We have the entire varint, so read it CodedInputStream coded = new CodedInputStream(State.LengthStream, true); State.LengthStream.Seek(0, SeekOrigin.Begin); int result = coded.ReadLength(); coded.Dispose(); return(result); } return(-1); }
private static Line HandleLine(CodedInputStream cis) { var line = new Line(); long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: line.FunctionId = cis.ReadUInt64(); break; case 2: line.LineNumber = cis.ReadInt64(); break; default: cis.SkipLastField(); break; } } else { break; } } return(line); }
public void WriteTo_PackedInt32() { uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); var field = new RepeatedField <int> { 10, 1000, 1000000 }; var stream = new MemoryStream(); var output = new CodedOutputStream(stream); field.WriteTo(output, FieldCodec.ForInt32(tag)); output.Flush(); stream.Position = 0; var input = new CodedInputStream(stream); input.AssertNextTag(tag); var length = input.ReadLength(); Assert.AreEqual(10, input.ReadInt32()); Assert.AreEqual(1000, input.ReadInt32()); Assert.AreEqual(1000000, input.ReadInt32()); Assert.IsTrue(input.IsAtEnd); Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length); }
private static Mapping HandleMapping(CodedInputStream cis) { var mapping = new Mapping(); long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: mapping.Id = cis.ReadUInt64(); break; case 2: mapping.MemoryStart = cis.ReadUInt64(); break; case 3: mapping.MemoryLimit = cis.ReadUInt64(); break; case 4: mapping.FileOffset = cis.ReadUInt64(); break; case 5: mapping.FileNameIndex = cis.ReadInt64(); break; case 6: mapping.BuildIdIndex = cis.ReadInt64(); break; case 7: mapping.HasFunctions = cis.ReadInt32() != 0; break; case 8: mapping.HasFilenames = cis.ReadInt32() != 0; break; case 9: mapping.HasLineNumbers = cis.ReadInt32() != 0; break; case 10: mapping.HasInlineFrames = cis.ReadInt32() != 0; break; default: cis.SkipLastField(); break; } } else { break; } } return(mapping); }
private static Sample HandleSample(CodedInputStream cis) { var sample = new Sample(); var lables = new List <Label>(); sample.Labels = lables; var locationIds = new List <ulong>(); sample.LocationIds = locationIds; var values = new List <long>(); sample.Values = values; long pos = cis.ReadLength(); pos += cis.Position; while (pos - cis.Position > 0) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: { long posLocations = cis.ReadLength(); posLocations += cis.Position; while (posLocations - cis.Position > 0) { locationIds.Add(cis.ReadUInt64()); } break; } case 2: { long posValues = cis.ReadLength(); posValues += cis.Position; while (posValues - cis.Position > 0) { values.Add(cis.ReadInt64()); } break; } case 3: lables.Add(HandleLabel(cis)); break; default: cis.SkipLastField(); break; } } else { break; } } return(sample); }
private static Stream ReadStream(CodedInputStream input) { var length = input.ReadLength(); return(new MemoryStream(input.ReadRawBytes(length))); }
public static void Main(string[] args) { var profile = new Profile(); RomanNumeral y = new RomanNumeral(); BinaryNumeral x = y; using (var fs = new FileStream(@"C:\users\muks\desktop\perf.pb", FileMode.Open, FileAccess.Read)) { using (var cis = new CodedInputStream(fs)) { var valueTypeList = new List <ValueType>(); var samplesList = new List <Sample>(); var mappingList = new List <Mapping>(); var locationList = new List <Location>(); var stringTable = new List <string>(); profile.SampleTypes = valueTypeList; profile.Samples = samplesList; profile.Locations = locationList; profile.StringTable = stringTable; profile.Mappings = mappingList; while (true) { var tag = cis.ReadTag(); if (tag > 0) { var fieldNumber = WireFormat.GetTagFieldNumber(tag); switch (fieldNumber) { case 1: valueTypeList.Add(HandleValueType(cis)); break; case 2: samplesList.Add(HandleSample(cis)); break; case 3: mappingList.Add(HandleMapping(cis)); break; case 4: locationList.Add(HandleLocation(cis)); break; case 5: cis.SkipLastField(); break; case 6: var strLength = cis.ReadLength(); stringTable.Add(System.Text.Encoding.UTF8.GetString(cis.ReadRawBytes(strLength), 0, strLength)); break; default: cis.SkipLastField(); break; } } else { break; } } } } for (int i = 0; i < profile.Mappings.Count; ++i) { var mapping = profile.Mappings[i]; var filename = profile.StringTable[(int)mapping.FileNameIndex]; var rangeBegin = mapping.MemoryStart; var rangeEnd = mapping.MemoryLimit; Console.WriteLine($"Filename: {filename}, Range Start: {rangeBegin}, Rang End: {rangeEnd}"); } }
public void WriteTo_PackedInt32() { uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); var field = new RepeatedField<int> { 10, 1000, 1000000 }; var stream = new MemoryStream(); var output = new CodedOutputStream(stream); field.WriteTo(output, FieldCodec.ForInt32(tag)); output.Flush(); stream.Position = 0; var input = new CodedInputStream(stream); input.AssertNextTag(tag); var length = input.ReadLength(); Assert.AreEqual(10, input.ReadInt32()); Assert.AreEqual(1000, input.ReadInt32()); Assert.AreEqual(1000000, input.ReadInt32()); Assert.IsTrue(input.IsAtEnd); Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length); }
public override void ReadValue(CodedInputStream stream) { stream.ReadLength(); MultiHash = new MultiHash(stream); Value = MultiHash.ToBase58(); }
public void AddEntriesFrom(CodedInputStream input, FieldCodec <T> codec) { uint lastTag = input.LastTag; Func <CodedInputStream, T> valueReader = codec.ValueReader; if (RepeatedField <T> .smethod_1(typeof(T).TypeHandle).IsValueType()) { goto IL_79; } goto IL_189; uint arg_147_0; while (true) { IL_142: uint num; switch ((num = (arg_147_0 ^ 349581016u)) % 13u) { case 0u: arg_147_0 = ((input.MaybeConsumeTag(lastTag) ? 2792542167u : 2493560502u) ^ num * 4283985340u); continue; case 1u: arg_147_0 = (((WireFormat.GetTagWireType(lastTag) == WireFormat.WireType.LengthDelimited) ? 4182580420u : 2445968179u) ^ num * 3573087188u); continue; case 2u: arg_147_0 = (num * 2407926176u ^ 627495612u); continue; case 3u: { int num2; arg_147_0 = (((num2 > 0) ? 704969730u : 1347903514u) ^ num * 2106852334u); continue; } case 4u: { int oldLimit; input.PopLimit(oldLimit); arg_147_0 = (num * 2913868755u ^ 1655185686u); continue; } case 6u: { int num2; int oldLimit = input.PushLimit(num2); arg_147_0 = (num * 3972840873u ^ 320789690u); continue; } case 7u: goto IL_79; case 8u: this.Add(valueReader(input)); arg_147_0 = 1293084732u; continue; case 9u: arg_147_0 = ((!input.ReachedLimit) ? 862879642u : 1176527045u); continue; case 10u: return; case 11u: goto IL_189; case 12u: { int num2 = input.ReadLength(); arg_147_0 = (num * 1888486294u ^ 430467554u); continue; } } break; } return; IL_79: arg_147_0 = 431838837u; goto IL_142; IL_189: this.Add(valueReader(input)); arg_147_0 = 320588544u; goto IL_142; }