news 2026/1/23 5:35:02

从零开始实现一个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/1/22 15:38:48

Speech Seaco Paraformer如何刷新状态?系统信息实时监控教程

Speech Seaco Paraformer如何刷新状态&#xff1f;系统信息实时监控教程 1. 欢迎使用&#xff1a;中文语音识别新体验 你是否正在寻找一个高精度、易用性强的中文语音识别工具&#xff1f;Speech Seaco Paraformer 正是为此而生。它基于阿里云 FunASR 技术构建&#xff0c;由…

作者头像 李华
网站建设 2026/1/22 14:58:41

Speech Seaco Paraformer实时录音延迟优化:浏览器与系统设置调整

Speech Seaco Paraformer实时录音延迟优化&#xff1a;浏览器与系统设置调整 1. 引言&#xff1a;为什么实时录音延迟会影响使用体验&#xff1f; 在使用 Speech Seaco Paraformer 进行实时语音识别时&#xff0c;很多用户反馈虽然模型识别准确率高、支持热词定制&#xff0c…

作者头像 李华
网站建设 2026/1/21 3:29:43

Markdown 列表详解

Markdown 列表详解 引言 Markdown 是一种轻量级标记语言,易于阅读和编写。在 Markdown 中,列表是一种常用的元素,用于展示有序或无序列表,使内容层次清晰,易于阅读。本文将详细解析 Markdown 列表的用法,包括有序列表、无序列表、嵌套列表、自定义列表等。 有序列表 …

作者头像 李华
网站建设 2026/1/22 5:54:13

ChampR电竞助手:智能化出装符文管理方案

ChampR电竞助手&#xff1a;智能化出装符文管理方案 【免费下载链接】champ-r &#x1f436; Yet another League of Legends helper 项目地址: https://gitcode.com/gh_mirrors/ch/champ-r 在英雄联盟的竞技世界中&#xff0c;合理的出装搭配和符文选择往往决定了比赛的…

作者头像 李华
网站建设 2026/1/22 18:32:55

对比实测:三种开机启动方法哪个更适合新手?

对比实测&#xff1a;三种开机启动方法哪个更适合新手&#xff1f; 在 Linux 系统中&#xff0c;让脚本随系统启动自动运行是许多用户都会遇到的需求。比如你有一个监控服务、数据同步任务或自定义工具&#xff0c;希望它在每次开机时都能自动执行&#xff0c;而不需要手动干预…

作者头像 李华
网站建设 2026/1/22 9:47:54

Excel驱动智能知识图谱构建终极指南:从零到一的完整实践

Excel驱动智能知识图谱构建终极指南&#xff1a;从零到一的完整实践 【免费下载链接】SmartKG This project accepts excel files as input which contains the description of a Knowledge Graph (Vertexes and Edges) and convert it into an in-memory Graph Store. This pr…

作者头像 李华