news 2026/6/10 16:36:56

从零开始实现一个C++高性能服务器框架----序列化模块

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
从零开始实现一个C++高性能服务器框架----序列化模块

详细内容:日志模块,使用宏实现流式输出,支持同步日志与异步日志、自定义日志格式、日志级别、多日志分离等功能。线程模块,封装pthread相关方法,封装常用的锁包括(信号量,读写锁,自旋锁等)。IO协程调度模块,基于ucontext_t实现非对称协程模型,以线程池的方式实现多线程,多协程协同调度,同时依赖epoll实现了事件监听机制。定时器模块,使用最小堆管理定时器,配合IO协程调度模块可以完成基于协程的定时任务调度。hook模块,将同步的系统调用封装成异步操作(accept, recv, send等),配合IO协程调度能够极大的提升服务器性能。Http模块,封装了sokcet常用方法,支持http协议解析,客户端实现连接池发送请求,服务器端实现servlet模式处理客户端请求,支持单Reator多线程,多Reator多线程模式的服务器。

序列化模块
  • 序列化模块的底层存储是固定大小的块,以链表形式组织。每次写入数据时,将数据写入到链表最后一个块中,如果最后一个块不足以容纳数据,则分配一个新的块并添加到链表结尾,再写入数据。ByteArray会记录当前的操作位置,每次写入数据时,该操作位置按写入大小往后偏移,如果要读取数据,则必须调用setPosition重新设置当前的操作位置。
  • 这样有个好处,不用一开始就开一个很大的空间去存储数据。
1. 主要功能
  • 支持序列化固定长度的有符号/无符号8位、16位、32位、64位整数
  • 支持序列化不固定长度的有符号/无符号32位、64位整数(使用zigzag算法进行压缩)
  • 支持序列化float、double类型
  • 支持序列化string和(length+string)的格式
  • 支持将序列化内容输出到文件
  • 支持大小端转换
2. 功能演示

代码语言:javascript

AI代码解释

std::vector<int32_t> res; johnsonli::ByteArray::ptr ba(new johnsonli::ByteArray(base_len)); for(int i = 0; i<len; ++i) { res.push_back(rand()); } for(auto &it : res) { ba->writeFint32(it); } ba->setPosition(0); // 读之前偏移量要设置为0 for(auto it : res) { int32_t val = ba->readFint32(); DO_ASSERT(it == val); } DO_ASSERT(ba->getReadSize() == 0); LOG_INFO(g_logger) << "writeFint32/readFint32" " (int32_t) len=" << len << " base_len=" << base_len << " size=" << ba->getSize();
3. 模块介绍
3.1 ByteArray
  • 序列化类。封装一个内存块,使用链表将内存块连接,实现动态扩容

代码语言:javascript

AI代码解释

struct Node { Node(size_t s); //构造指定大小的内存块 Node(); ~Node(); char* ptr; //内存块地址指针 Node* next; //下一个内存块地址 size_t size; //内存块大小 };
  • 主要支持以下方法

代码语言:javascript

AI代码解释

void writeFint8 (int8_t value); // 写入固定长度int8_t类型的数据 void writeFuint8 (uint8_t value); // 写入固定长度uint8_t类型的数据 // ... 16、32、64字节 void writeInt32 (int32_t value); // 压缩写入int32_t void writeUint32 (uint32_t value); // 压缩写入uint32_t // 64字节 void writeFloat (float value); void writeDouble(float value); void writeStringF16(const std::string& value); // 写入std::string类型的数据,用uint16_t作为长度类型 void writeStringWithoutLength(const std::string& value); // 不写长度 // ... void readFint8 (int8_t value); // 读取固定长度int8_t类型的数据 void readFuint8 (uint8_t value); // 读取固定长度uint8_t类型的数据 // ... 16、32、64字节 void readInt32 (int32_t value); // 读取压缩的int32_t void readUint32 (uint32_t value); // 读取压缩的uint32_t // 64字节 ...

www.dongchedi.com/article/7597217223819772478
www.dongchedi.com/article/7597215233471889944
www.dongchedi.com/article/7597216873696526910
www.dongchedi.com/article/7597217143041737241
www.dongchedi.com/article/7597214870441935385
www.dongchedi.com/article/7597214599947043353
www.dongchedi.com/article/7597214580846477886
www.dongchedi.com/article/7597216071082738201
www.dongchedi.com/article/7597214433031078424
www.dongchedi.com/article/7597214537498362392
www.dongchedi.com/article/7597215399566361150
www.dongchedi.com/article/7597215658752868888
www.dongchedi.com/article/7597215102077141528
www.dongchedi.com/article/7597214696924004889
www.dongchedi.com/article/7597213042329895448
www.dongchedi.com/article/7597215125493400126
www.dongchedi.com/article/7597212587801018905
www.dongchedi.com/article/7597214580846215742
www.dongchedi.com/article/7597214267869692440
www.dongchedi.com/article/7597213056480969278
www.dongchedi.com/article/7597212812516639257
www.dongchedi.com/article/7597212812516868633
www.dongchedi.com/article/7597213320844182041
www.dongchedi.com/article/7597211160895046206
www.dongchedi.com/article/7597211076186374681
www.dongchedi.com/article/7597212587801477657
www.dongchedi.com/article/7597210839670080062
www.dongchedi.com/article/7597210276412899864
www.dongchedi.com/article/7597211030086926872
www.dongchedi.com/article/7597211160895078974
www.dongchedi.com/article/7597209997126238744
www.dongchedi.com/article/7597209064238039577
www.dongchedi.com/article/7597211030087287320
www.dongchedi.com/article/7597209862904480318
www.dongchedi.com/article/7597209319725253145
www.dongchedi.com/article/7597208525277987353
www.dongchedi.com/article/7597208525278151193
www.dongchedi.com/article/7597210268858958398
www.dongchedi.com/article/7597209475426435609
www.dongchedi.com/article/7597209772429476377
www.dongchedi.com/article/7597201951176213017
www.dongchedi.com/article/7597201687174562366
www.dongchedi.com/article/7597199724889997849
www.dongchedi.com/article/7597199550092657177
www.dongchedi.com/article/7597200248943329816
www.dongchedi.com/article/7597199001863701017
www.dongchedi.com/article/7597198298541834777
www.dongchedi.com/article/7597200591446000190
www.dongchedi.com/article/7597199968348357145
www.dongchedi.com/article/7597199312984162841
www.dongchedi.com/article/7597199429019861566
www.dongchedi.com/article/7597196791863902782
www.dongchedi.com/article/7597197725960110616
www.dongchedi.com/article/7597197533550920217
www.dongchedi.com/article/7597196766895079960
www.dongchedi.com/article/7597197878439756313
www.dongchedi.com/article/7597196909912031768
www.dongchedi.com/article/7597195764053492248
www.dongchedi.com/article/7597196370181030424
www.dongchedi.com/article/7597195961618121241
www.dongchedi.com/article/7597195004385182232
www.dongchedi.com/article/7597196281857442366
www.dongchedi.com/article/7597195368090075673
www.dongchedi.com/article/7597195809683505689
www.dongchedi.com/article/7597194439940932158
www.dongchedi.com/article/7597194164794933822
www.dongchedi.com/article/7597194060553749016
www.dongchedi.com/article/7597195414877995544
www.dongchedi.com/article/7597194911112479256
www.dongchedi.com/article/7597194219174068761
www.dongchedi.com/article/7597192718418756120
www.dongchedi.com/article/7597191743318065689
www.dongchedi.com/article/7597194069471003161
www.dongchedi.com/article/7597193401016500760
www.dongchedi.com/article/7597192147254772286
www.dongchedi.com/article/7597192394672505406
www.dongchedi.com/article/7597190301329080894
www.dongchedi.com/article/7597188998091833881
www.dongchedi.com/article/7597190006675243582
www.dongchedi.com/article/7597189866363372056

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 9:53:10

GalTransl终极教程:AI驱动的Galgame一键汉化完整指南

GalTransl终极教程&#xff1a;AI驱动的Galgame一键汉化完整指南 【免费下载链接】GalTransl 支持GPT-3.5/GPT-4/Newbing/Sakura等大语言模型的Galgame自动化翻译解决方案 Automated translation solution for visual novels supporting GPT-3.5/GPT-4/Newbing/Sakura 项目地…

作者头像 李华
网站建设 2026/6/10 9:54:58

YOLO11模型蒸馏实战:小模型高性能部署方案

YOLO11模型蒸馏实战&#xff1a;小模型高性能部署方案 YOLO11是当前目标检测领域中极具代表性的新一代算法&#xff0c;它在保持高精度的同时进一步优化了推理速度和模型体积。相比前代版本&#xff0c;YOLO11通过更高效的网络结构设计、动态标签分配机制以及增强的特征融合策…

作者头像 李华
网站建设 2026/6/10 9:54:59

GPU压力测试终极指南:全面掌握多显卡性能诊断

GPU压力测试终极指南&#xff1a;全面掌握多显卡性能诊断 【免费下载链接】gpu-burn Multi-GPU CUDA stress test 项目地址: https://gitcode.com/gh_mirrors/gp/gpu-burn GPU Burn是一款基于CUDA架构的专业级多GPU压力测试工具&#xff0c;能够对NVIDIA显卡进行极限性能…

作者头像 李华
网站建设 2026/6/10 8:53:02

监控GPU资源使用:GLM-4.6V-Flash-WEB运维小贴士

监控GPU资源使用&#xff1a;GLM-4.6V-Flash-WEB运维小贴士 在部署像 GLM-4.6V-Flash-WEB 这类高性能多模态模型时&#xff0c;我们往往更关注“能不能跑起来”和“效果好不好”&#xff0c;却容易忽略一个关键问题&#xff1a;系统资源是否健康、稳定、可持续&#xff1f; 尤…

作者头像 李华
网站建设 2026/6/10 10:14:25

3分钟搞定字体合并:打造专属魔兽世界字体方案

3分钟搞定字体合并&#xff1a;打造专属魔兽世界字体方案 【免费下载链接】Warcraft-Font-Merger Warcraft Font Merger&#xff0c;魔兽世界字体合并/补全工具。 项目地址: https://gitcode.com/gh_mirrors/wa/Warcraft-Font-Merger 还在为魔兽世界字体显示不全而烦恼&…

作者头像 李华