Protobuf使用--go和C#
一、Go安装及使用protobuf工具
以下都是基于Linux系统;
1.安装
A) protobuf 编译工具安装
1、下载 protoBuf:
cd $GOPATH/src/
git clone https://github.com/protocolbuffers/protobuf.git
2、或者直接将压缩包拖入后解压
unzip protobuf.zip
3、安装依赖库
sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y
4、进入目录
cd protobuf/
5、自动生成configure配置文件:
./autogen.sh
6、配置环境:
./configure
7、编译源代码(时间比较长):
make
8、安装
sudo make install
9、刷新共享库 (很重要的一步啊)
sudo ldconfig
10、成功后需要使用命令测试
protoc -h
B) protobuf 的 go 语言插件安装
由于protobuf并没直接支持go语言需要我们手动安装相关插件
- 获取 proto包(Go语言的proto API接口)
go get -v -u github.com/golang/protobuf/proto
go get -v -u github.com/golang/protobuf/protoc-gen-go
2.编译
cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go/
go build
3.将生成的 protoc-gen-go可执行文件,放在/bin目录下
sudo cp protoc-gen-go /bin/
tips:上面第2步报错,由于go install可以直接安装protobuf了,跟错误提示安装;
go install google.golang.org/protobuf@latest
2.使用
pb语法就不介绍了;
.proto文件:
syntax="proto3"; //Proto协议
package pb; //当前包名
option csharp_namespace="Pb"; //给C#提供的选项
option go_package = "./"; //输出路径
message BroadCast{
int32 PID =1;
int32 Tp = 2;
oneof Data{
string Content = 3;
}
}
直接执行以下命令,该文件夹下.proto文件都会被编译成go文件;
protoc --go_out=. *.proto
Tips:我这里生成的go文件包名都是__,知道哪里出了错,手动改一下就完了;
具体使用方法:
package main
import (
"fmt"
"gameserver/pb"
"google.golang.org/protobuf/proto"
)
func main() {
data := &pb.BroadCast{
PID: 1,
Tp: 2,
Data: &pb.BroadCast_Content{
Content: "hello protobuf!perilla",
},
}
//序列化
binaryData, err := proto.Marshal(data)
if err != nil {
fmt.Println("marshal err:", err)
}
newdata := &pb.BroadCast{}
//反序列化
err = proto.Unmarshal(binaryData, newdata)
if err != nil {
fmt.Println("unmarshal err:",err)
}
fmt.Println(newdata)
}
二、C#使用protobuf工具
C#就不用geogle的protobuf了,用geogle另一个protobuf-net;
工程有重复dll,只留一个unity文件中的dll;
类加[ProtoBuf.ProtoContract]特性,类中字段 [ProtoBuf.ProtoMember(1)];
[ProtoBuf.ProtoContract]
public class Porperties
{
[ProtoBuf.ProtoMember(1)]
public int id;
[ProtoBuf.ProtoMember(2)]
public string name;
[ProtoBuf.ProtoMember(3)]
public List<int> attributes;
public void Init(int id, string name, List<int> attributes)
{
this.id = id;
this.name = name;
this.attributes = attributes;
}
}
void Start()
{
Porperties p = new Porperties();
int[] arr = { 1, 33, 44, 63 };
p.Init(1, "perilla", new List<int>(arr));
using (FileStream stream = File.OpenWrite("test.dat"))
{
//序列化
ProtoBuf.Serializer.Serialize<Porperties>(stream, p);
}
Porperties p2 = new Porperties();
using (FileStream stream = File.OpenRead("test.dat"))
{
//从文件中读取数据并反序列化
p2 = ProtoBuf.Serializer.Deserialize<Porperties>(stream);
}
Debug.Log(string.Format(p2.name,p2.id,p2.attributes[2]));
}
Protobuf使用--go和C#的更多相关文章
- python通过protobuf实现rpc
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...
- Protobuf使用规范分享
一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
- protobuf的编译安装
github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- protobuf学习(2)-相关学习资料
protobuf官方git地址 protobuf官方英文文档 (你懂的需要FQ) protobuf中文翻译文档 protobuf概述 (官方翻译 推荐阅读) protobuf入门 ...
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- c# (ENUM)枚举组合类型的谷歌序列化Protobuf
c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...
- dubbox 增加google-gprc/protobuf支持
好久没写东西了,今年实在太忙,基本都在搞业务开发,晚上来补一篇,作为今年的收官博客.google-rpc 正式发布以来,受到了不少人的关注,这么知名的rpc框架,不集成到dubbox中有点说不过去. ...
随机推荐
- [BUUCTF]REVERSE——不一样的flag
不一样的flag 附件 步骤 例行查壳儿,32位程序,无壳儿 32位ida载入,shift+f12检索程序里的字符串,看到了如图标记的字符串,加上下面又上下左右的选项,估计是道迷宫类型的题目 将迷宫字 ...
- cron 获取下次运行时间(基于 C# + Quartz.NET)
代码 Quartz 的 cron 支持秒,导致一些 cron 库无法准确的获得下次执行时间,这里使用 Quartz.Net 自带的方法来获取下次执行时间. //引用 Quartz CronExpres ...
- CF475A Bayan Bus 题解
Update \(\texttt{2020.10.6}\) 修改了一些笔误. Content 模拟一个核载 \(34\) 人的巴士上有 \(k\) 个人时的巴士的状态. 每个人都会优先选择有空位的最后 ...
- CF1581B Diameter of Graph 题解
Content \(\textsf{CQXYM}\) 想要构造一个包含 \(n\) 个点和 \(m\) 条边的无向连通图,并且他希望这个图满足下列条件: 该图中不存在重边和自环.也就是说,一条边应该连 ...
- CF628B New Skateboard 题解
Content 有一个长度为 \(n\) 的数字串 \(s\),求出有多少个子串能够被 \(4\) 整除. 数据范围:\(1\leqslant n\leqslant 3\times 10^5\). S ...
- 【九度OJ】题目1118:数制转换 解题报告
[九度OJ]题目1118:数制转换 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1118 题目描述: 求任意两个不同进制非 ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 1269 - Consecutive Sum
1269 - Consecutive Sum PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB ...
- 第三十三个知识点:Bellcore攻击是如何攻击使用CRT的RSA的?
第三十三个知识点:Bellcore攻击是如何攻击使用CRT的RSA的? 注意:这篇博客是由follow论密码计算中消除错误的重要性(On the importance of Eliminating E ...
- CoGAN
目录 概 主要内容 代码 Liu M., Tuzel O. Coupled Generative Adversarial Networks. NIPS, 2016. 概 用GAN和数据(从边缘分布中采 ...