Skip to content

ayrb13/binproto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 

Repository files navigation

#binproto ##一个顺序的C++序列化反序列化库

Binproto可以方便的使用C++宏来定义所需要进行序列化反序列化的结构体,你只需要include它就够了

##特性

  • 使用标准C++编写,支持windows和unix系统,便于移植
  • 使用方便,只需要#include “binproto.hpp”
  • 使用二进制流表示数据,节省带宽,性能和效率超越protobuf
  • 二进制格式透明,便于根据协议文档使用该类库编写代码

##例子

###定义Teacher这个结构体,有三个成员

  • 第一个成员name是一个变长字符串,可以使用第一个字节保存长度,后续字节为name的内容;
  • 第二个成员是age,取值范围可为0-255,使用一个字节表示;
  • 第三个成员是sex,使用10字节的定长字符串来表示;
  • 第四个成员是subject是一个变长字符串,可以使用第一个字节保存长度,后续字节为subject的内容;

Teacher

序号字段名字节数描述
1size of teacher's name1number of size
2teacher's namenstring of name
3age1number of teacher's age
4sex10fixed length string,left-justified,fill empty \0
5size of teacher's subject1number of size
6teacher's subjectnstring of subject
BINPROTO_DEFINE_PACKET_P04(Teacher
	,binproto::variable_len_string<1>,name
	,binproto::uint8_obj,age
	,binproto::fixed_len_string<10>,sex
	,binproto::variable_len_string<1>,subject);

###定义Student这个结构体,有三个成员

  • 第一个成员name是一个变长字符串,可以使用第一个字节保存长度,后续字节为name的内容;
  • 第二个成员是age,取值范围可为0-255,使用一个字节表示;
  • 第三个成员是sex,使用10字节的定长字符串来表示;

Student

序号字段名字节数描述
1size of student's name1number of size
2student's namenstring of name
3age1number of student's age
4sex10fixed length string,left-justified,fill empty \0
BINPROTO_DEFINE_PACKET_P03(Student
	,binproto::variable_len_string<1>,name
	,binproto::uint8_obj,age
	,binproto::fixed_len_string<10>,sex);

###定义SchoolClass这个结构体,有三个成员

  • 第一个成员class number是一个正整数,取值范围可为0-255,用一个字节表示;
  • 第二个成员teacher用上面定义的teacher结构体来表示;
  • 第三个成员student list, 学生的数量最多255个,所以用第一个字节表示长度,后续的字节循环表示student结构体

SchoolClass

序号字段名字节数描述
1classnum1number of classnum
2size of teacher's name1number of size
3teacher's namenstring of name
4age1number of teacher's age
5sex10fixed length string,left-justified,fill empty \0
6size of teacher's subject1number of size
7teacher's subjectnstring of subject
8student count1number of student count
9size of student's name1number of size
10student's namenstring of name
11age1number of student's age
12sex10fixed length string,left-justified,fill empty \0
13next student...repeat 9 - 12
BINPROTO_DEFINE_PACKET_P03(SchoolClass
	,binproto::uint8_obj,classnum
	,Teacher,teacher
	,binproto::binary_obj_list<Student BINPROTO_COMMA 1>,stulist);

###定义main函数

#define BUFF_SIZE 1024*1024
int main()
{
	try
	{
		char *buffer = new char[BUFF_SIZE];
		//序列化流程
		{
			//定义并填充结构体数据
			SchoolClass schoolclass;
			schoolclass.classnum = 1;
			schoolclass.teacher.name = "Ren Bin";
			schoolclass.teacher.age = 27;
			schoolclass.teacher.sex = "male";
			schoolclass.teacher.subject = "maths";
			
			//定义学生结构体并加入到stulist结构中
			Student sd;
			sd.name = "Li Ming";
			sd.age = 13;
			sd.sex = "male";
			schoolclass.stulist.push_back(sd);

			sd.name = "Han Meimei";
			sd.age = 13;
			sd.sex = "female";
			schoolclass.stulist.push_back(sd);

			sd.name = "Jim";
			sd.age = 14;
			sd.sex = "male";
			schoolclass.stulist.push_back(sd);
			
			//这个结构可获得结构体序列化之后的长度
			int binlen = schoolclass.get_binary_len();
			//序列化进缓存中
			schoolclass.serialize_to_buffer(buffer,BUFF_SIZE);
		}
		//反序列化流程
		{
			//定义结构体
			SchoolClass schoolclass;
			//只需要一步就可以从缓存反序列化
			schoolclass.parse_from_buffer(buffer,BUFF_SIZE);
			//这个结构可获得结构体序列化之后的长度
			int binlen = schoolclass.get_binary_len();
			printf("Class number is %d\n",schoolclass.classnum.to_int());
			printf("Class teacher named %s teaches %s, and %s age is %d\n"
				,schoolclass.teacher.name.c_str()
				,schoolclass.teacher.subject.c_str()
				,strcmp(schoolclass.teacher.sex.c_str(), "male") == 0 ? "his" : "her"
				,schoolclass.teacher.age.to_int());
			printf("Here comes the students\n");
			for(binproto::binary_obj_list<Student,1>::iterator it = schoolclass.stulist.begin();
				it != schoolclass.stulist.end(); ++it)
			{
				printf("a student named %s, and %s age is %d\n"
					,it->name.c_str()
					,strcmp(it->sex.c_str(), "male") == 0 ? "his" : "her"
					,it->age.to_int());
			}
		}
	}
	//若反序列化出现问题,则会抛出该异常
	catch(const binproto::exception& ex)
	{
		printf("%s.\n",ex.what().c_str());
	}
	return 0;	
}

About

一个顺序的C++序列化反序列化库

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages