/// <summary> /// Writes whole Protobuf to Protobuf_Streamer. /// </summary> /// <param name="streamer">Protobuf_Streamer for writing.</param> public void WriteToStream(Protobuf_Streamer streamer) { foreach (KeyValuePair <ushort, Protobuf_Field> pair in this.p_fields) { pair.Value.WriteToStream(streamer); } }
/// <summary> /// Writes field to Protobuf_Streamer. /// </summary> /// <param name="streamer">Protobuf_Streamer for writing field to.</param> public void WriteToStream(Protobuf_Streamer streamer) { Int64 head = (this.p_fn << 3) | (ushort)(this.p_wt); streamer.WriteVarint(head); if (this.p_wt == Protobuf.WireType.LengthDelimited) { streamer.WriteVarint(this.p_data.Length); } streamer.WriteBytes(this.p_data, this.p_data.Length); }
/// <summary> /// Creates new Protobuf class instance by reading it from Protobuf_Streamer. /// </summary> /// <param name="streamer">Protobuf_Streamer used for reading.</param> public Protobuf(Protobuf_Streamer streamer) { p_fields = new Dictionary <ushort, Protobuf_Field>(); Protobuf_Field field = null; while (true) { try { field = new Protobuf_Field(streamer); } catch (Exception) { break; } this.p_fields[field.GetFieldNumber()] = field; } }
/// <summary> /// Creates new Protobuf_Field class instance by reading and decoding it from Protobuf_Streamer. /// </summary> /// <param name="streamer">Protobuf_Streamer for reading.</param> public Protobuf_Field(Protobuf_Streamer streamer) { Int64 head = streamer.ReadVarint(); if (head == 0) // end of stream or read error { throw new Exception("End of underlaying stream of read error"); } this.p_wt = (Protobuf.WireType)(head & 0x07); this.p_fn = (ushort)(head >> 3); this.p_data = null; this.p_idata = 0; switch (this.p_wt) { case Protobuf.WireType.Varint: this.p_idata = streamer.ReadVarint(); break; case Protobuf.WireType.Fixed64bit: this.p_data = streamer.ReadBytes(8); break; case Protobuf.WireType.LengthDelimited: int len = (int)(streamer.ReadVarint()); this.p_data = streamer.ReadBytes(len); break; /*case Protobuf.WireType_StartGroup: -- deprecated * break; * case Protobuf.WireType_EndGroup: -- deprecated * break;*/ case Protobuf.WireType.Fixed32bit: this.p_data = streamer.ReadBytes(4); break; default: throw new Exception("Unknown protobuf wire type " + this.p_wt); } }