国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C# 使用google protocolbuffer協(xié)議

2019-11-17 02:20:37
字體:
供稿:網(wǎng)友

C# 使用google PRotocolbuffer協(xié)議

簡介

google protocolbuffer,google 提供了三種語言的實(shí)現(xiàn):java、c++和python,每一種實(shí)現(xiàn)都包含了相應(yīng)語言的編譯器以及庫文件。由于它是一種二進(jìn)制的格式,比使用xml進(jìn)行數(shù)據(jù)交換快許多。

可以把它用于分布式應(yīng)用之間的數(shù)據(jù)通信或者異構(gòu)環(huán)境下的數(shù)據(jù)交換。作為一種效率和兼容性都很優(yōu)秀的二進(jìn)制數(shù)據(jù)傳輸格式,可以用于諸如網(wǎng)絡(luò)傳輸、配置文件、數(shù)據(jù)存儲等諸多領(lǐng)域。

以下介紹下C#使用protocolbuffer


下載protobuf-csharp-port-2.4.1.521-release-binaries.zip

http://code.google.com/p/protobuf-csharp-port/


proto 文件

//import "Base.proto"; 添加引用文件方式

package Proto;

message LoginRsp{ required int32 Result = 1;//0:失敗,1:成功 optional bytes Cause = 2; optional bytes guid = 4;//唯一標(biāo)識請求,可選}

required 必填項(xiàng)

optional 可選項(xiàng)

repeated list項(xiàng)


協(xié)議生成

把下載文件中的protoc.exe、ProtoGen.exe、Google.ProtocolBuffers.dll這些文件和bat文件、proto文件放入同一個(gè)文件夾

生成協(xié)議bat文件格式:

@echo onprotoc --descriptor_set_out=LoginRsp.protobin --include_imports LoginRsp.proto protogen LoginRsp.protobin

會(huì)在該文件中生成 LoginRsp.cs


生成要發(fā)送協(xié)議數(shù)據(jù)

Proto.LoginRsp.Builder builder = new Proto.LoginRsp.Builder();
builder.Result = 1;
builder.Cause = ByteString.CopyFrom(Cause, Encoding.Default);
builder.Guid= ByteString.CopyFrom(Guid, Encoding.Default); //string 轉(zhuǎn) ByteString , ByteString 是 Protocol buffer 的類型。byte[] sendData = builder.Build().ToByteArray();  //發(fā)送data

解析協(xié)議:

Proto.LoginRsp Rsp = Proto.LoginRsp.ParseFrom(netMsg);  //netMsg 生成的協(xié)議 byte[] sendData = builder.Build().ToByteArray();

StringConvertToByteString
public class StringConvertToByteString    {        public static ByteString ConvertToByteString(string text)        {            if (text != null)                return ByteString.CopyFrom(text, Encoding.Default);            else                return ByteString.Empty;        }        public static ByteString ConvertToByteString(string text, Encoding encoding)        {            if (text != null)                return ByteString.CopyFrom(text, Encoding.Default);            else                return ByteString.Empty;        }        public static string ConvertToString(ByteString text)        {            return text.ToString(Encoding.Default);        }        public static string ConvertToString(string value)        {            if (!string.IsNullOrEmpty(value))                return value;            else                return string.Empty;        }    }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泗阳县| 剑河县| 长岭县| 辽阳市| 上蔡县| 黄陵县| 凤城市| 临沂市| 南靖县| 泾川县| 基隆市| 大兴区| 连南| 江源县| 荃湾区| 安义县| 榆树市| 尖扎县| 电白县| 安吉县| 云阳县| 呼玛县| 奈曼旗| 焦作市| 长治县| 武平县| 荥阳市| 揭西县| 罗江县| 仙居县| 项城市| 新郑市| 定襄县| 彝良县| 石首市| 东光县| 应用必备| 原阳县| 宜州市| 黄平县| 伊川县|