news 2026/9/3 0:58:42

从零开始实现一个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/9/2 21:34:55

Qwen3-Embedding向量维度灵活设置,适配各种场景

Qwen3-Embedding向量维度灵活设置&#xff0c;适配各种场景 1. 模型简介&#xff1a;Qwen3-Embedding系列的核心优势 Qwen3 Embedding 模型系列是 Qwen 家族中专为文本嵌入和排序任务打造的最新成员。该系列基于强大的 Qwen3 密集基础模型构建&#xff0c;提供从 0.6B 到 8B …

作者头像 李华
网站建设 2026/9/2 21:33:54

C++中的静态变量和静态函数的作用

C中的静态变量和静态函数是一种特殊类型的成员&#xff0c;具有不同于普通成员的特殊性质。静态变量和静态函数都是与类相关的&#xff0c;而不是与类的实例相关的。在本文中&#xff0c;我们将介绍C中的静态变量和静态函数的作用以及使用案例。静态变量在C中&#xff0c;静态变…

作者头像 李华
网站建设 2026/9/2 23:53:13

Obsidian插件汉化终极指南:一键中文界面快速配置完整教程

Obsidian插件汉化终极指南&#xff1a;一键中文界面快速配置完整教程 【免费下载链接】obsidian-i18n 项目地址: https://gitcode.com/gh_mirrors/ob/obsidian-i18n 还在为Obsidian中繁多的英文插件而苦恼吗&#xff1f;想要打造纯粹的中文工作环境却不知从何入手&…

作者头像 李华
网站建设 2026/9/2 23:56:46

Z-Image-Turbo如何降低推理成本?9步生成节省70%算力实战案例

Z-Image-Turbo如何降低推理成本&#xff1f;9步生成节省70%算力实战案例 1. 为什么文生图模型的推理成本是个大问题&#xff1f; 你有没有遇到过这种情况&#xff1a;想用最新的AI画一张高清图&#xff0c;结果等了三分钟&#xff0c;显卡风扇快飞出去了&#xff0c;电费都快…

作者头像 李华
网站建设 2026/9/2 22:28:56

YOLOv9实战案例:野生动物监测系统部署详细流程

YOLOv9实战案例&#xff1a;野生动物监测系统部署详细流程 在自然保护区、生态研究和野外巡护中&#xff0c;如何高效、准确地识别和记录野生动物一直是技术难点。传统的人工巡查方式耗时耗力&#xff0c;而红外相机采集的海量图像数据又难以快速处理。借助AI目标检测技术&…

作者头像 李华
网站建设 2026/9/3 0:52:28

Label Studio完整教程:从零开始掌握数据标注全流程

Label Studio完整教程&#xff1a;从零开始掌握数据标注全流程 【免费下载链接】label-studio 项目地址: https://gitcode.com/gh_mirrors/lab/label-studio 数据标注是AI项目中不可或缺的关键环节&#xff0c;而Label Studio作为业界领先的开源数据标注平台&#xff0…

作者头像 李华