{"id":15047356,"url":"https://github.com/xukeawsl/asiomp","last_synced_at":"2026-01-02T07:05:42.476Z","repository":{"id":240402515,"uuid":"800486593","full_name":"xukeawsl/asiomp","owner":"xukeawsl","description":"asiomp can help you to build multi-process tcp server based on c++ asio","archived":false,"fork":false,"pushed_at":"2024-05-22T13:29:35.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-22T13:35:20.027Z","etag":null,"topics":["asio","cpp11","multi-process","tcp-server"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xukeawsl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-14T12:30:13.000Z","updated_at":"2024-05-22T13:35:21.736Z","dependencies_parsed_at":"2024-05-22T13:35:19.931Z","dependency_job_id":null,"html_url":"https://github.com/xukeawsl/asiomp","commit_stats":null,"previous_names":["xukeawsl/asiomp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fasiomp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fasiomp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fasiomp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fasiomp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xukeawsl","download_url":"https://codeload.github.com/xukeawsl/asiomp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243495495,"owners_count":20299923,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asio","cpp11","multi-process","tcp-server"],"created_at":"2024-09-24T20:57:05.361Z","updated_at":"2026-01-02T07:05:42.447Z","avatar_url":"https://github.com/xukeawsl.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# asiomp 简介\n\n[![License](https://img.shields.io/npm/l/mithril.svg)](https://github.com/xukeawsl/asiomp/blob/master/LICENSE)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/23ca8b88cf7f4fe2bc3a4e870d5f795f)](https://app.codacy.com/gh/xukeawsl/asiomp/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade)\n[![Build status](https://ci.appveyor.com/api/projects/status/5epf5t4t8c1wqax4?svg=true)](https://ci.appveyor.com/project/xukeawsl/asiomp)\n\nasiomp 能够帮助你构建基于 c++ asio 的多进程 tcp 服务器\n\n## 前提\n\n* 需要支持 C++11 的编译器\n\n* 只支持 Linux 平台 (使用 `fork` 系统调用)\n\n## 特性\n\n* 高可用: 采用类似 `nginx` 的 `master-worker` 工作模式, 当 worker 进程挂掉后可以自动拉起来, 保证正常提供服务, 支持守护进程模式工作\n\n* 高并发: 使用多进程能够很好的利用多核能力, 提高整体处理能力, 推荐可以在工作进程使用 asio 协程, 进一步提高 IO 处理的效率\n\n* 日志: 采用 `spdlog` 第三方日志库, 其中每个进程都有各自对应的日志, 已经设置好了默认的日志记录器, 直接使用即可\n\n## 建议\n\n* 为了更好的发挥多进程的优势, 建议在会话可以独立工作的场景下使用, 例如在项目 [coro_socks](https://github.com/xukeawsl/coro_socks) 中使用多进程加协程实现了一个高性能的 socks5 代理服务器\n\n* 如果会话间需要通信交互等, 建议使用 asio 封装的 unix 域套接字或者 tcp、udp 套接字, 可以很好地融入 asio 的上下文, 并且不需要引入锁\n\n* 如果需要实现类似聊天室这种需要频繁转发消息的功能, 建议使用单进程模式即可, 如果要使用多进程, 则需要进程间通信来向所有连接分发消息, 示例 [chat](https://github.com/xukeawsl/asiomp/tree/master/example/chat) 中使用 unix 域套接字实现了一个简单的聊天室以供参考\n\n## 使用\n\n### 1. 作为子模块引入项目\n\n```bash\n# 对于一个新项目\ngit init\nmkdir third-party\n\n# 添加子模块\ngit submodule add https://github.com/xukeawsl/asiomp.git third-party/asiomp\n\n# 递归拉取所有子模块代码\ngit submodule update --init --recursive\n```\n\n### 2. 在 CMakeLists 中添加子目录并链接动态库\n\n```cmake\nadd_subdirectory(third-party/asiomp)\n\ntarget_link_libraries(${PROJECT_NAME} PUBLIC asiomp pthread spdlog::spdlog)\n```\n\n### 3. 添加业务代码\n\n* 继承 `session` 类并实现其 `start` 方法\n\n```cpp\n#include \"asiomp.h\"\n\nclass your_session : public session {\npublic:\n    explicit your_session(asio::ip::tcp::socket socket) : socket_(std::move(socket)) {\n        // 其它初始化操作\n    }\n\n    void start() override {\n        // 注意如果使用 asio 的异步操作, 需要将智能指针提升\n        // 为 std::shared_ptr\u003cyour_session\u003e 才行, 可以使用 session 中\n        // 定义的 getDerivedSharedPtr\u003cyour_session\u003e 模板方法\n        auto self = getDeriveSharedPtr\u003cyour_session\u003e();\n        self-\u003ehandle_session();\n\n        // 如果不使用异步方法直接调用即可\n        // this-\u003ehandle_session();\n    }\n\nprivate:\n    void handle_session() {\n        // 会话处理\n    }\n\nprivate:\n    asio::ip::tcp::socket socket_;\n};\n```\n\n* 将自己实现的会话类注册到会话工厂中并为其设置一个名称, 使用 `asiomp_server` 的静态模板方法即可\n\n```cpp\n#include \"asiomp.h\"\n#include \"your_session.h\"\n\n/* asiomp 参数解释\n   param1: argv 用于修改运行后的进程名称, 可以使用 cmake -DPROC_NAME=your_proc_name 设置\n   param2: 服务器监听的 ip 地址, 支持 IPv6\n   param3: 服务器监听端口\n   param4: worker 进程个数, 如果为 0 则取 CPU 核心数(单进程模式无这个参数)\n   param5: 是否作为守护进程运行\n   param6: 会话名称(如果对应会话没有注册, 则使用默认会话)\n*/\nint main(int argc, char *argv[]) {\n    asiomp_server::register_session\u003cyour_session\u003e(\"your_session\");\n    asiomp_server(argv, \"127.0.0.1\", 80, 2, false, \"your_session\").run();\n    return 0;\n}\n```\n\n### 4. 构建运行\n\n```bash\nmkdir build \u0026\u0026 cd build\n\n# 默认是 Debug 模式\ncmake -DCMAKE_BUILD_TYPE=Release ..\n\n# 可以在编译时调整日志级别, 如果没有设置则 Release 构建使用 Info 级别\n# Debug 构建使用 Debug 级别\ncmake -DLOG_LEVEL=Trace ..\n\n# 可以在构建时调整可执行文件运行后的名称, 默认是 asiomp\ncmake -DPROC_NAME=echo_server ..\n\n# 编译二进制可执行文件\ncmake --build .\n\n# 假如你的可执行文件名是 echo\n./echo\n```\n\n### 5. 安装\n\n```cmake\n# 安装到默认路径\ncmake --install .\n\n# 安装到指定目录开头的路径\ncmake --install . --prefix /usr/local/test\n\n# 安装完之后注意一下头文件路径和库文件路径是否能在环境变量中找到, 否则需要再添加一下环境变量\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fasiomp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxukeawsl%2Fasiomp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fasiomp/lists"}