压缩数据减少网络交互量举例

背景:服务器给移动客户端传数据,每份数据有32位id+4bit属性值,有足够理由相信这些id以某种“聚簇”形式存在。比如(为了不写出具体实际场景,举个矬例子)服务器传给客户端某某学校几个班学生的身份证号和班号,这些学生都是老乡,出生时间接近,所以身份证数字差距不太大,班号也是最多16个班。


c++代码,这里都写到一个cpp里面了,本来加密和解密至少应该写在两个cpp,从略。

#include <cstdio>
#include <map>
#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include <arpa/inet.h>
#include <cstring>
#include <stdint.h>

//不是头文件用下无大碍
using namespace std;

const unsigned int encodeMaxBit = 4;
typedef struct{
        //方便htonl处理用uint32_t
        //改变顺序后也是一样大的,先把id放前面
        uint32_t id;
        char grade;
}StuInfo;

inline bool operator<(const StuInfo &lhs,const StuInfo &rhs) noexcept(true) {
        return lhs.id < rhs.id;
}

//用此函数估算buf大小
inline size_t encodeBufLenEstimated(size_t size) noexcept(true) {
        //grade的比特数占用大小+id数+总数
        return ((encodeMaxBit * size) / 8 + 1) + (sizeof(uint32_t) * size + size) + sizeof(uint32_t);
}

//假设stuVec的顺序会改变
//返回实际buf长度
size_t numEncode(vector<StuInfo> &stuVec,char *buf,size_t len) noexcept(false) {
        if(stuVec.size() == 0)
                return 0;
        if(len < encodeBufLenEstimated(stuVec.size()))
                throw invalid_argument("len too short");
        //sort时间复杂度N*log2(N),也假设stuVec不太长
        sort(stuVec.begin(),stuVec.end());

        char *p = buf,cdiff;
        //diff 64位,否则可能会溢出
        int64_t diff;
        int i = 0;
        uint32_t nt;
        //第一个要注意的是网络字节序
        nt = htonl(stuVec.size());
        //第二个要注意uint32_t,和网络字节序都是和平台不相关的
        memcpy(p,&nt,sizeof(uint32_t));
        p += sizeof(uint32_t);
        while(i < stuVec.size()){
                if(i == 0){
                        nt = htonl(stuVec[i].id);
                        memcpy(p,&nt,sizeof(uint32_t));
                        p += sizeof(uint32_t);
                }else{
                        diff = stuVec[i].id - stuVec[i - 1].id;
                        //char取值范围为-128~127,-128用于结尾
                        if(diff <= 127 && diff > -128){
                                cdiff = diff;
                                memcpy(p,&cdiff,1);
                                p += 1;
                        }else{
                                cdiff = -128;
                                memcpy(p,&cdiff,1);
                                p += 1;
                                nt = htonl(stuVec[i].id);
                                memcpy(p,&nt,sizeof(uint32_t));
                                p += sizeof(uint32_t);
                        }
                }
                i++;
        }
        i = 0;
        while(i < stuVec.size()){
                *p = 0xf0;
                *p &= stuVec[i].grade << 4;
                i++;
                if(i < stuVec.size()){
                        *p |= stuVec[i].grade;
                        i++;
                }
                p++;
        }
        return p - buf;
}

//应该返回解析失败信息而非抛异常,to do
//len用来使得不越界,这里先不深究,to do
int numDecode(vector<StuInfo> &res,char *buf,size_t len) noexcept(true) {
        uint32_t nt,last,size;
        memcpy(&nt,buf,sizeof(uint32_t));
        buf += sizeof(uint32_t);
        nt = ntohl(nt);
        res.resize(nt);
        size = nt;
        int i = 0;
        bool shortEnd = true;
        while(i < size){
                if(shortEnd){
                        memcpy(&nt,buf,sizeof(uint32_t));
                        buf += sizeof(uint32_t);
                        nt = ntohl(nt);
                        res[i].id = nt;
                        shortEnd = false;
                        last = nt;
                }else{
                        if(*buf == -128){
                                shortEnd = true;
                                buf++;
                                continue;
                        }
                        last += *buf;
                        buf++;
                        res[i].id = last;
                }
                i++;
        }
        i = 0;
        while(i < nt){
                res[i].grade = (*buf) >> 4;
                i++;
                if(i < nt){
                        res[i].grade = (*buf) & 0x0f;
                        i++;
                }
                buf++;
        }
}

int main(){
        //初始化一个不典型的例子
        vector<StuInfo> stuVec = {{1111,1},{2222,2},{1112,3},{1116,4},{1119,3},{2220,4},{2226,3},{2229,1},{3333,0},{2210,1},{3330,2},{3339,2}},res;
        size_t len = encodeBufLenEstimated(stuVec.size());
        char *buf = new char[len];
        //打印“压缩”后的长度
        printf("%d\n",numEncode(stuVec,buf,len));
        //打印如果用文本传的长度
        printf("%d\n",strlen("1111,1,2222,2,1112,3,1116,4,1119,3,2220,4,2226,3,2229,1,3333,0,2210,1,3330,2,3339,2"));
        numDecode(res,buf,555);
        //验证decode输出
        for_each(res.begin(),res.end(),[](const StuInfo &sv){printf("%d,%d\n",sv.id,sv.grade);});
}

运行结果:

33
83
1111,1
1112,3
1116,4
1119,3
2210,1
2220,4
2222,2
2226,3
2229,1
3330,2
3333,0
3339,2

从前两行看比文本传送小了1.5倍,从后面几行看加解密成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值