protobuf问题总结
来源:互联网 发布:时间 小人 js插件 编辑:程序博客网 时间:2024/05/18 06:19
今天遇到奇葩问题 解析string始终不对
这里搜集一下网上的说法:
The C++ implementation of protocol buffers returns the byte and string types as std::string
. This structure contains a length function telling you how long the corresponding data is (as well as the data itself.) Thus there is no special significance of embeded \0
characters.
The setting functions accept a string too, or there are versions that accept a buffer and length. If you want to set a field you can just do this:
pb.set_foo( std::string( data, data_length ) );
or
pb.set_foo( data, data_length )
A follow-up to my own previous comment - I figured out how to do enums and optional types. The latter is done via nullable types, I never saw that documented here.
If anyone knows how to encode default values, I would love to hear about it.
Meanwhile, here's a demo proto definition and its corresponding proto.net implemenation. Hope that helpful to someone:
// -----------------------------------------------------------------------------// DemoMessage.proto// -----------------------------------------------------------------------------message DemoMessage { message WildThing { enum Coolness { COOLNESS_NOT_SO_MUCH = 0; COOLNESS_EXTREMELY_SO = 1; } optional Coolness coolness = 1 [default = COOLNESS_NOT_SO_MUCH]; optional float toxicity_PPM = 2 [default = 0.12]; } optional InnerThing wildThing = 1; optional uint64 posixTime_Msec = 2; optional float volume_dBm = 3 [default = -112.0];}// -----------------------------------------------------------------------------// Proto.DemoMessage.cs// -----------------------------------------------------------------------------using System;using System.IO;using ProtoBuf;// -----------------------------------------------------------------------------namespace Whatever{ // ------------------------------------------------------------------------- [ProtoContract] public sealed class Proto_DemoMessage { [ProtoContract] public sealed class WildThingMessage { public enum CoolnessSetting { NotSoMuch = 0, ExtremelySo = 1 } [ProtoMember(1, IsRequired = false)] public CoolnessSetting? Coolness { get; set; } [ProtoMember(2, IsRequired = false)] public float? Toxicity_PPM { get; set; } } [ProtoMember(1, IsRequired = false)] public WildThingMessage WildThing { get; set; } [ProtoMember(2, IsRequired = false)] public UInt64? PosixTime_Msec { get; set; } [ProtoMember(3, IsRequired = false)] public float? Volume_dBm { get; set; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- public byte[] Serialize() { byte[] b = null; using (var ms = new MemoryStream()) { Serializer.Serialize<Proto_DemoMessage>(ms, this); b = new byte[ms.Position]; var fullB = ms.GetBuffer(); Array.Copy(fullB, b, b.Length); } return b; } // --------------------------------------------------------------------- public static Proto_DemoMessage Deserialize(byte[] serializationBytes) { Proto_DemoMessage m = null; using (var ms = new MemoryStream(serializationBytes)) { m = Serializer.Deserialize<Proto_DemoMessage>(ms); } return m; } }}Hello, standart BinnaryWriter? is faster then ProtoBuf?, why ? Here is my code:
private static void Main(string[] args) { Test test = new Test(); test.Name = "TestName"; //=====BinaryFormatter var st = new Stopwatch(); st.Start(); var formatter = new BinaryFormatter(); for (int i = 0; i < 100; i++) { using (var stream = new MemoryStream()) { stream.Position = 0; formatter.Serialize(stream, test); stream.Position = 0; var result = (Test)formatter.Deserialize(stream); string name = result.Name; stream.Close(); } } st.Stop(); Console.WriteLine("BinaryFormatter = " + st.ElapsedMilliseconds); //======ProtoBuf st.Reset(); st.Start(); for (int i = 0; i < 100; i++) { using (var stream = new MemoryStream()) { stream.Position = 0; Serializer.Serialize(stream, test); stream.Position = 0; var result = Serializer.Deserialize<Test>(stream); string name = result.Name; stream.Close(); } } st.Stop(); Console.WriteLine("ProtoBuff = " + st.ElapsedMilliseconds); } [ProtoContract] [Serializable] internal class Test { [ProtoMember(1)] public string Name { get; set; } }The result is: BinaryFormatter? = 6 msec ProtoBuf? = 352 msec
https://github.com/hultqvist/protobuf
- protobuf问题总结
- Protobuf总结
- Protobuf 总结
- google protobuf的应用及遇到的问题总结
- protobuf使用错误总结
- protobuf-c学习总结
- protobuf用法总结
- protobuf 学习总结
- Protobuf 3.3 使用总结
- protobuf问题集锦(一)
- protobuf一些问题
- protobuf编译问题
- protobuf 编译问题
- 关于protobuf的问题
- Protobuf 的安装使用总结
- 关于Google protobuf的总结
- protobuf-c的学习总结
- 关于Google protobuf的总结
- 图形浏览器
- inserAfter函数(js公共函数,类似insertBefore)
- oracle中的数值函数整理
- Linux 路由 学习笔记 之三 路由查找流程分析
- Metasploit - Tips for Evading Anti-Virus
- protobuf问题总结
- 虚函数与纯虚函数
- 64位WIN7上成功安装theano,并运行了g++, GPU
- C++ virtual详解
- 用C++11优化矩阵运算的空间和时间效率
- Bottle实例Todo-List—动态路由中使用正则表达式
- 黑马程序员—面向对象(1)
- Linux下的consolen(控制台)和terminal(终端)
- MySQL字符集编码