Protobuf-net study notes

Protobuf usage
1 using Protobuf characteristics defined in the class cs marker so that it can be serialized binary
a ==用Protobuf的特性来标记cs的类==

[ProtoContract]
public class LoginInfo
{
    [ProtoMember(1)]
    public string userName
    {
        get;
        set;
    }

    [ProtoMember(2)]
    public string password
    {
        get;
        set;
    }
}

b ==将标记的类序列化到文本中保存==

    char[] chars = { 'a','b','c','d','e','f','g','1','2','3','4','5','6','7','A','B','D'};
    List<LoginInfo> list = new List<LoginInfo>();
    for (int i = 0; i < 10; i++)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int j = 0; j < 8; j++)
        {
            int index = random.Next(0,14);
            Console.WriteLine(j+"---"+index);
            stringBuilder.Append(chars[index]);
        }
        Console.WriteLine(stringBuilder);
        LoginInfo info = new LoginInfo { userName = "damon"+i,password = stringBuilder.ToString()};
        list.Add(info);
    }
    
    string path = @"D:\Damon\Year_2019\Private\Project_C#\服务器\Protobuf_demo01\bin\Debug\test.bat";
    using (FileStream fileStream = File.Create(path))
    {
        Serializer.Serialize<List<LoginInfo>>(fileStream, list);
    }


c ==将序列化的数据反序列化==

    using (FileStream stream = File.Open(path,FileMode.Open))
    {
        List<LoginInfo> infos = Serializer.Deserialize<List<LoginInfo>>(stream);
        for (int i = 0; i < infos.Count; i++)
        {
            Console.WriteLine(infos[i].userName+"--"+infos[i].password);
        }
    }

2 using the syntax structure Protobuf data Protobuf
Keyword a modified item

there must attribute required

optional attributes may have

repeated Array

b set the namespace

1 protobuf file name as the namespace, the message class name defined in cs

c corresponding data type
.proto data types c # type
double double
float float
int32 int32
int64 int64
uint32 uint32
uint64 uint64
sint32
sint64
fixed32
fixed64
sfixed32
sfixed64
bool bool
string string
bytes
d enumerated type

Enum constant is preferably controlled within a range of integer numbers of uint32 enumeration using variable encoding, and efficient enough for a plurality of support
using a positive integer recommendations

message LoginReq
{
	enum CommandType
	{
		Login=0;
		Register=1;
		CreateRole=2;
	}

	required CommandType type=1;
}


Notes written e

// use as a comment

message LoginReq
{
	enum CommandType
	{
		Login=0;
		Register=1;
		CreateRole=2;//这里需要使用;
	}

	required CommandType type=1;
}
f serialization and deserialization

Static generic method using the specified instance object serialized stream Stream

    //将一个类用Protobuf的特性进行标注
    [ProtoContract]
    public class LoginInfo
    {
        [ProtoMember(1)]
        public string userName
        {
            get;
            set;
        }

        [ProtoMember(2)]
        public string password
        {
            get;
            set;
        }
    }

    //实例化被protobuf标注特性的类
    LoginInfo login01 = new LoginInfo();
    login01.userName = "程文浩";
    login01.password = "222222";

    //将序列化的结果存放在内存流中
    byte[] bytes01 = null;
    using (MemoryStream stream = new MemoryStream())
    {
        Serializer.Serialize<LoginInfo>(stream,login01);
        bytes01 = stream.ToArray();
    }

Deserialization

Deserialize and serialize the opposite appears to need to be in a stream into the stream

    using (MemoryStream stream = new MemoryStream(bytes01))
    {
        LoginInfo info = Serializer.Deserialize<LoginInfo>(stream);
        Console.WriteLine(info.userName + "--" + info.password);
    }

Guess you like

Origin blog.csdn.net/weixin_36285892/article/details/94056523