我的开源项目:一种TLV编解码器的实现
2015-12-03 22:13
309 查看
TLV,全称是“type-length-value”,与“XML”、“Json”这些“字节流”格式不同,它是一种“二机制流”格式,不易阅读,但更省带宽,更容易压缩,常用于通信协议中的变长部分。
维基百科的定义如下:https://en.wikipedia.org/wiki/Type-length-value
一个TLV对象示例如下:
多个TLV对象可以连接起来,组成一个大的buffer:
一个TLV对象内部也可以嵌套另一个TLV对象:
我希望能提供一套API接口(C/C++/Java/其他语言),可以方便地完成TLV格式的编码和解码,项目地址如下所示:
https://github.com/Jhuster/TLV
其中,C++版本已经实现(更新:C/Java/Android版本也均已实现),介绍如下:
1. TLV编码的接口
//put one TLV box bool PutBoolValue(int type,bool value); bool PutCharValue(int type,char value); bool PutShortValue(int type,short value); bool PutIntValue(int type,int value); bool PutLongValue(int type,long value); bool PutLongLongValue(int type,long long value); bool PutFloatValue(int type,float value); bool PutDoubleValue(int type,double value); bool PutStringValue(int type,char *value); bool PutStringValue(int type,const std::string &value); bool PutBytesValue(int type,unsigned char *value,int length); bool PutObjectValue(int type,const TlvBox& value); //do encode bool Serialize(); //access encoded buffer and length unsigned char * GetSerializedBuffer() const; int GetSerializedBytes() const;
2. TLV解码的接口
//do decode bool Parse(const unsigned char *buffer,int buffersize); //get one TLV box bool GetBoolValue(int type,bool &value) const; bool GetCharValue(int type,char &value) const; bool GetShortValue(int type,short &value) const; bool GetIntValue(int type,int &value) const; bool GetLongValue(int type,long &value) const; bool GetLongLongValue(int type,long long &value) const; bool GetFloatValue(int type,float &value) const; bool GetDoubleValue(int type,double &value) const; bool GetStringValue(int type,char *value,int &length) const; bool GetStringValue(int type,std::string &value) const; bool GetBytesValue(int type,unsigned char *value,int &length) const; bool GetBytesValuePtr(int type,unsigned char **value,int &length) const; bool GetObjectValue(int type,TlvBox& value) const;
3. 具体用法
请参考代码目录下的 Test.cpp,有任何疑问欢迎来信lujun.hust@gmail.com交流,提交issue,也欢迎为本项目贡献代码,谢谢。
欢迎大家关注我的新浪微博 @卢_俊 获取最新的文章和资讯。
相关文章推荐
- 我的开源项目:一种TLV编解码器的实现
- 【凡尘工作室】 微信平台架构和实现(github开源项目)
- IM 开源项目 群组服务 缓存设计实现之 群组属性
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- Androidpn 一个实现 android 推送服务的开源项目
- 集成开源系统实现自动化构建、代码质量评估、项目信息统计
- 带你学开源项目:OkHttp--自己动手实现okhttp
- 用开源项目cropper实现对图片中任意部分进行裁剪
- (开源项目)SPPanBlog4JFinal 博客系统jfinal实现版本
- VC++ & MFC实现的优秀的开源项目
- android两种基本联网方式与一种第三方开源项目的使用
- 开源项目实现下载,断点续传..
- 这个开源项目展示了如何实现一个横跨各种Android平台的音乐播放器,包括手机,平板,汽车,手表,电视等。Google官方推出,跨平台开发必看项目。
- sqlite3对数据库加密用c语言开源项目wxSQLite3实现
- android开发(17) 扫描二维码 - 调用第三方APK来实现二维码扫描,参考自 开源项目zxing
- 开源项目实现多线程下载 (xutils)
- 文本建模、文本分类相关开源项目推荐(Pytorch实现)
- 基于开源SuperSocket实现客户端和服务端通信项目实战
- 如何制作捐赠二维码,实现开源项目或免费应用的捐赠收款(支付宝篇)
- 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例