2013-03-08 34 views
1

我有一個結構生成如何反序列化++「序列化Protobuffer」在C,其是用C#protobuf網

[ProtoContract] 
    public struct TenprintTemplateStructure 
    { 
     [ProtoMember(1)] 
     public byte[] FeatureTenprint { get; set; } //Tenpritn NTemplate's NBuffer 

     [ProtoMember(2)] 
     public int TemplateID { get; set; } //Template ID 

     [ProtoMember(3)] 
     public long TemplateSize { get; set; } //Template Size 

     [ProtoMember(4)] 
     public string PersonID { get; set; } //Person ID 

     [ProtoMember(5)] 
     public int IsActive { get; set; } // Person's Status 
    }; 

,我使用C#的protobuf與長度序列化這個結構的多個實例,以一個文件前綴Fixed32。代碼是下面,(tenprintTemplateStruct是結構im寫入)

ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, tenprintTemplateStruct, ProtoBuf.PrefixStyle.Fixed32, 0); 

我知道它可以使用C++反序列化。我嘗試了一些解決方案,但沒有成功。

有沒有人做過這個?

+0

BTW;側面的問題:爲什麼是'struct'?在我看來,每個骨骼和肌肉都在尖叫'class' ... – 2013-03-08 13:08:42

回答

3

在C++側,您有4個步驟來執行:

  • 生成使用protoc工具C++代碼;您可以手動編寫.proto架構,或者使用string proto = Serializer.GetProto<TenprintTemplateStructure>();(從您的.NET代碼)作爲起點
  • 從輸入中正好讀取4個字節並將其解析爲32位整數; Fixed32被記錄爲little-endian,因此:這樣做:
  • 將您實際的數據流封裝爲長度限制爲此長度的數據流;谷歌爲此
  • 提供LimitingInputStream按照C++ guide的實際反序列化 - 主要ParseFromIstream
+0

這是我的代碼:uint32 ByteSize = 0; \t \t而(coded_input-> ReadVarint32(&ByteSize)) \t \t { \t \t \t CodedInputStream ::極限codedLimit = coded_input-> PushLimit(ByteSize); \t \t \t TemplateData.ParseFromCodedStream(coded_input); \t \t \t coded_input-> PopLimit(codedLimit); \t \t \t FingerPrintNode * pFingerPrintNode = new FingerPrintNode; \t \t \t this-> translateFromProtoBuffToFingerPrintNode(&TemplateData,pFingerPrintNode); \t \t \t this-> AddToCache(pFingerPrintNode); \t \t} – Rezoan 2013-03-11 08:16:29

+0

我應該在while循環中使用ReadVarint32(&ByteSize)還是ReadLittleEndian32(&ByteSize)?有什麼區別嗎? – Rezoan 2013-03-11 08:18:52

+0

'ReadVarint32'讀取* varint *;與固定的32位數字非常不同。如果你用'Fixed32'存儲,那麼是'ReadLittleEndian32'聽起來是正確的方法,是的,它們之間有巨大的差別。 – 2013-03-11 08:25:03