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 ...
随机推荐
- python第一篇:Python 字符串编
Python字符串编码 字符串编码的前世今生 1. 一个字节由8个bit组成,所以1个字节能表示的最大数为255: 2. 计算机是美国人发明的,所以一个字节可以表示所有的字符了,所以ASCII就成为美 ...
- Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
- Codeforces 372B Counting Rectangles is Fun:dp套dp
题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...
- 分享知识-快乐自己:大数据(hadoop)环境搭建
大数据 hadoop 环境搭建: 一):大数据(hadoop)初始化环境搭建 二):大数据(hadoop)环境搭建 三):运行wordcount案例 四):揭秘HDFS 五):揭秘MapReduce ...
- 泛型,注解,反射配合优化BaseDao的猜想
package test; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.la ...
- 4_Prototype 原型
#Prototype ``` // 不好的做法 monster ghost demon sorcerer class Spawner { public: virtual ~Spawner() {} ; ...
- 【leetcode刷题笔记】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N x M的矩阵,请你数一数其中有多少个3 x 3的子矩阵可以构成三阶幻方? 如果3 x 3的矩阵中每一行.每一列 ...
- sql split函数
--DROP FUNCTION F_SQLSERVER_SPLIT GO CREATE FUNCTION F_SQLSERVER_SPLIT(@Long_str varchar(8000),@spli ...
- Codeforces Round #402 (Div. 2) 题解
Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...