ProtoBuf练习(二)
重复数据类型
protobuf语言的重复字段类型相当于C++的std::list数据类型
工程目录结构
$ ls proto/
TServer.proto TSession.proto
proto文件
$ cat TSession.proto
syntax = "proto3";
//枚举类型可以放外面,也可以放message里面
enum Status
{
INVALID = 0;
VALID = 1;
};
message TSession
{
string owner = 1;
Status status = 2;
};
$ cat TServer.proto
syntax = "proto3";
import "TSession.proto";
//通过repeated来模拟链表的可变长度
message TServer
{
repeated TSession sessions = 1;
};
读写源文件
$ cat writer.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "TServer.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
TServer srv;
std::string owner;
while(true)
{
std::getline(std::cin, owner);
if (owner.empty())
break;
//自动生成一个新的节点,并返回对象指针
TSession* s = srv.add_sessions();
s->set_owner(owner);
s->set_status(Status::VALID);
}
fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!srv.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
}
$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "TServer.pb.h"
using namespace std;
int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl;
TServer srv;
if (!srv.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
}
cout << "First Method" << endl;
for (int i = 0; i < srv.sessions_size(); i++)
srv.sessions(i).PrintDebugString();
cout << "Second Method" << endl;
auto sessions = srv.sessions();
for (auto iter = sessions.begin(); iter != sessions.end(); iter++)
iter->PrintDebugString();
cout << "Deserialize end." << endl;
input.close();
return 0;
}
ProtoBuf练习(二)的更多相关文章
- netty + Protobuf (整合二)
[正文]Protobuf 消息设计 疯狂创客圈 死磕Netty 系列之12 [博客园 总入口 ] 本文说明 本篇是 netty+Protobuf 实战的第二篇,完成一个 基于Netty + Proto ...
- Sword protobuf学习二
编写protobuf消息文件 文件格式: xxx.proto //标明使用哪个版本的protobuf,默认2.0版本 syntax = "proto3"; //类似于c++中的na ...
- 【咸鱼教程】protobuf在websocket通讯中的使用
教程目录一 protobuf简介二 使用protobuf三 Demo下载 参考: CSDN:Egret项目中使用protobuf(protobufjs) TS项目中使用Protobuf的解决方案(ba ...
- erlang抽象码与basho的protobuf
erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...
- Go - 如何编写 ProtoBuf 插件 (三) ?
目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...
- 【ProtoBuffer】windows上安装ProtoBuffer3.1.0 (附已编译资源)
------- 17.9.17更新 --- 以下这些方法都是扯淡,对我的机器不适用,我后来花了最后成功安装并亲测可用的方法不是靠vs编过的,vs生成的库引入后函数全部报undefine refere ...
- ubuntu 16.04 安装caffe2的方法及问题解决
工作需要安装caffe2,从用户体验上来讲,caffe2的安装绝对是体验比较差的那种,花费了我那么多时间去倒腾,这样的用户体验的产品,估计后面是比较危险的. 废话少说,直接上步骤: 官网上有安装目录, ...
- Golang gRPC 和 gRPC-gateway 结合使用
一.安装 go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/g ...
- Netty 粘包/半包原理与拆包实战
Java NIO 粘包 拆包 (实战) - 史上最全解读 - 疯狂创客圈 - 博客园 https://www.cnblogs.com/crazymakercircle/p/9941658.html 本 ...
- 疯狂创客圈 JAVA死磕系列 总目录
无编程不创客,无案例不学习.疯狂创客圈,一大波高手正在交流.学习中! 疯狂创客圈 Java 死磕系列: [博客园 总入口] QQ群:104131248 [Java 聊天室] 实战从0开始,打造100 ...
随机推荐
- codevs1281 Xn数列
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...
- codevs1199 开车旅行
[问题描述]小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为H i ,城市 ...
- Spark- RDD持久化
官方原文: RDD Persistence One of the most important capabilities in Spark is persisting (or caching) a d ...
- Luogu P1196 [NOI2002]银河英雄传说:带权并查集
题目链接:https://www.luogu.org/problemnew/show/P1196 题意: 有30000个战舰队列,编号1...30000. 有30000艘战舰,编号1...30000, ...
- 【Educational Codeforces Round 38】D. Buy a Ticket 堆优化Dijkstra
题意 给定一张无向图,对每个点$i\in S$求$\min_{j\in S} {2\times d(i,j)+a_j}$ 考虑多源多汇最短路会超时,换个角度考虑每个$j$,如果$j=i$,那么答案为$ ...
- codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)
题目链接: A. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 615E Hexagons (二分+找规律)
E. Hexagons time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- uoj279温暖会指引我们前行
暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- 【C++】标准库sort函数的自定义排序
自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...
- 手动导入XMPPFramework框架
环境: Xcode 8.2.1 XMPPFramework 3.6.5 (下载地址) Objective-C (项目使用的语言,最新版的3.7.0要求convert to swift) 1.下载XMP ...