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/9 22:02:11

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

详细内容&#xff1a;日志模块&#xff0c;使用宏实现流式输出&#xff0c;支持同步日志与异步日志、自定义日志格式、日志级别、多日志分离等功能。线程模块&#xff0c;封装pthread相关方法&#xff0c;封装常用的锁包括&#xff08;信号量&#xff0c;读写锁&#xff0c;自旋…

作者头像 李华
网站建设 2026/6/10 13:24:43

从零开始实现一个C++高性能服务器框架----守护进程

详细内容&#xff1a;日志模块&#xff0c;使用宏实现流式输出&#xff0c;支持同步日志与异步日志、自定义日志格式、日志级别、多日志分离等功能。线程模块&#xff0c;封装pthread相关方法&#xff0c;封装常用的锁包括&#xff08;信号量&#xff0c;读写锁&#xff0c;自旋…

作者头像 李华
网站建设 2026/6/10 13:23:01

GPU压力测试终极指南:多GPU性能验证与运维实战

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

作者头像 李华
网站建设 2026/6/10 13:24:51

如何解决Live Avatar CUDA OOM?分辨率与帧数调优实战案例

如何解决Live Avatar CUDA OOM&#xff1f;分辨率与帧数调优实战案例 1. Live Avatar阿里联合高校开源的数字人模型 你可能已经听说了&#xff0c;阿里最近联合多所高校推出了一个名为Live Avatar的开源数字人项目。这个模型基于14B参数规模的DiT架构&#xff0c;在文本到视频…

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

【c++入门】缺省参数

1.意义在c语言中&#xff0c;函数的传参要和函数的形参一一对应&#xff0c;相应的它的使用就很死板&#xff0c;所以在c中祖师爷引入了缺省参数这一概念&#xff0c;灵活了函数的使用。2.使用方法我们先看以下代码&#xff1a;代码语言&#xff1a;javascriptAI代码解释void f…

作者头像 李华
网站建设 2026/6/10 10:27:26

ASV波浪模拟器:构建真实海洋环境的Gazebo插件系统

ASV波浪模拟器&#xff1a;构建真实海洋环境的Gazebo插件系统 【免费下载链接】asv_wave_sim This package contains plugins that support the simulation of waves and surface vessels in Gazebo. 项目地址: https://gitcode.com/gh_mirrors/as/asv_wave_sim ASV波浪…

作者头像 李华