MoonNet
MoonNet is a high-performance event driven Linux server network library
Install / Use
/learn @MoonforDream/MoonNetREADME
____ ,--.
,' , `. ,--.'| ___
,-+-,.' _ | ,--,: : | ,--.'|_
,-+-. ; , || ,---. ,---. ,---, ,`--.'`| ' : | | :,'
,--.'|' | ;| ' ,'\ ' ,'\ ,-+-. / | | : : | | : : ' :
| | ,', | ': / / | / / | ,--.'|' | : | \ | : ,---. .;__,' /
| | / | | || . ; ,. : . ; ,. : | | ,"' | | : ' '; | / \ | | |
' | : | : |, ' | |: : ' | |: : | | / | | ' ' ;. ; / / | :__,'| :
; . | ; |--' ' | .; : ' | .; : | | | | | | | | \ | . ' / | ' : |__
| : | | , | : | | : | | | | |/ ' : | ; .' ' ; /| | | '.'|
| : ' |/ \ \ / \ \ / | | |--' | | '`--' ' | / | ; : ;
; | |`-' `----' `----' | |/ ' : | | : | | , /
| ;/ '---' ; |.' \ \ / ---`-'
'---' '---' `----'
目录
- 简介中文版/Chinese
- Overview英文版/Enlish
- 基准测试(Benchmark)
- 使用示例(Example)
- 使用手册(Guide)
- 贡献(Contributing)
- 加入我们(Join us)
- 许可证(License)
- 致谢(Thanks)
简介 (Chinese)
<a name="chinese"></a>
MoonNet 是一个专注于基于 Reactor 的高性能服务端网络库,提供基于主从 Reactor 多线程模型的服务端模块。它利用 Linux 的 epoll 机制,结合多线程和事件驱动设计,提供高效、可扩展的网络通信能力。MoonNet 支持 TCP 和 UDP 协议,并内置信号处理和定时器功能,提供丰富的组件如无锁环形缓冲区、无锁线程池等来构建服务器应用,适用于构建高并发、低延迟的服务器应用。
主要特性
- 高性能:基于
epoll的事件驱动模型,支持大量并发连接。 - 多线程:主从 Reactor 模式,通过线程池管理多个事件循环,充分利用多核 CPU。
- 丰富的事件类型:支持 TCP、UDP、定时器和信号事件。
- 丰富的组件模块:提供丰富的组件去构建服务器应用,你可以灵活的使用MoonNet中的组件去灵活实现项目架构和具体实现
- 易于使用:简洁的接口设计,方便集成到各种项目中。
- 静态/动态负载均衡:提供静态和动态的负载均衡方案
安装
前提条件
- C++11 编译器 或更高版本
- CMake 3.10 或更高版本
- Linux
编译步骤
-
克隆仓库
git clone https://github.com/MoonforDream/MoonNet.git cd MoonNet -
创建构建目录
mkdir build cd build -
运行 CMake
cmake .. -
编译
make -
安装
sudo make install -
引用头文件
#include <moonnet/moonnet.h> -
清理
make clean-all
对比
| 特性 | MoonNet | muduo | libevent | Boost.Asio | libuv | | -------------- | ---------------------- | ---------------------- | ---------------- | ---------------------- | ---------------------- | | 多线程支持 | 内置线程池管理 | 原生支持 | 需自行集成 | 原生支持 | 需自行管理 | | 事件类型 | TCP, UDP, 定时器, 信号 | TCP, UDP, 定时器, 信号 | TCP, UDP, 定时器 | TCP, UDP, 定时器 | TCP, UDP, 定时器, 信号 | | 易用性 | 简洁接口,模块化 | 接口复杂 | 丰富但复杂 | 功能强大但学习曲线陡峭 | 丰富且跨平台 | | 性能 | 高吞吐量,优化负载 | 高吞吐量 | 高性能 | 高性能 | 高性能 | | 跨平台 | Linux | Linux | Linux, Windows | 跨平台 | 跨平台 |
MoonNet 通过简洁的接口设计和内置的多线程管理,提供了与传统高性能网络库相媲美的性能,同时降低了使用复杂度,适合需要高并发处理的服务器应用。
Overview (English)
<a name="en"></a>
MoonNet is a high-performance server-side network library focused on a Reactor-based model, offering server modules based on a master-slave Reactor multi-threaded model. It utilizes Linux's epoll mechanism, combined with multithreading and event-driven design, to provide efficient and scalable network communication capabilities. MoonNet supports TCP and UDP protocols and includes built-in signal handling and timer functions, offering a rich array of components such as lock-free circular buffers and lock-free thread pools for building server applications, suitable for creating high-concurrency, low-latency server applications.
Key Features
- High Performance: Event-driven model based on
epoll, supporting a large number of concurrent connections. - Multithreading: Master-worker Reactor pattern with a thread pool managing multiple event loops, fully utilizing multi-core CPUs.
- Rich Event Types: Supports TCP, UDP, timer, and signal events.
- Rich Component Modules: Provides a rich set of components to build server applications, allowing you to flexibly implement project architecture and specific functionalities.
- Ease of Use: Simple interface design, easy to integrate into various projects.
- Static/Dynamic Load Balancing: Provides static and dynamic load balancing solutions
Installation
Prerequisites
- C++11 Compiler or higher
- CMake 3.10 or higher
- Linux
Build Steps
-
Clone the Repository
git clone https://github.com/MoonforDream/MoonNet.git cd MoonNet -
Create Build Directory
mkdir build cd build -
Run CMake
cmake .. -
Build
make -
Install
sudo make install -
Reference header file
#include <moonnet/moonnet.h> -
Clean
make clean-all
Comparison
| Feature | MoonNet | muduo | libevent | Boost.Asio | libuv | | -------------------------- | -------------------------------- | ----------------------- | -------------------- | --------------------------------- | ----------------------- | | Multithreading Support | Built-in thread pool management | Natively supported | Requires integration | Natively supported | Requires management | | Event Types | TCP, UDP, Timer, Signal | TCP, UDP, Timer, Signal | TCP, UDP, Timer | TCP, UDP, Timer | TCP, UDP, Timer, Signal | | Ease of Use | Simple interface, modularization | Complex interfaces | Rich but complex | Powerful but steep learning curve | Rich and cross-platform | | Performance | High throughput, optimized load | High throughput | High performance | High performance | High performance | | Cross-Platform | Linux | Linux | Linux, Windows | Cross-platform | Cross-platform |
MoonNet offers performance comparable to traditional high-performance network libraries while reducing complexity through its straightforward interface design and built-in multithreading management, making it ideal for server applications requiring high concurrency.
基准测试 (Benchmark)
<a name="benchmark"></a>
测试环境/Test Environment
- 服务器/Server:阿里云 2 核 2G Ubuntu 服务器 / Aliyun 2-core 2G Ubuntu server
- 测试工具/Testing Tool:使用 muduo 的 pingpong 客户端 / Using muduo's pingpong client
- 测试方法/Methodology:在 1000 个并发连接下,运行 10 分钟,分别测试不同消息大小的吞吐量 / Under 1000 concurrent connections, run for 10 minutes, testing throughput with different message sizes
- 测试目标/Test Target:4线程的TCP回声服务器,动态均衡是以2-4线程进行动态调整的,代码见bench目录 / A 4-threaded TCP echo server, code can be found in the bench directory
测试结果/Test Results
吞吐量比较/Throughput
| 并发数/Concurrency | 测试时长/Duration | 消息大小/Message Size | MoonNet(Dynamic) 吞吐量/Throughput | MoonNet(Static) 吞吐量/Throughput | muduo 吞吐量/Throughput | Libevent 吞吐量/Throughput | | ------------------ | ----------------- | --------------------- | ---------------------------------- | --------------------------------- | ----------------------- | -------------------------- | | 1000 | 10 分钟 | 1024 字节 | 119.28mib/s | 102.06 MiB/s | 97.51 MiB/s | 95.22 MiB/s | | 1000 | 10 分钟 | 4096 字节 | 477.82mib/s | 373.76 MiB/s | 355.86 MiB/s | 281.47 MiB/s | | 1000 | 10 分钟 | 8192 字节 | 914.60mib/s | 669.08 MiB/s | 632.54 MiB/s | 561.58 MiB/s |
数据可视化/Data Visualization
为了更直观地展示不同库的吞吐量差异,以下是测试结果的柱状图 / To better illustrate the throughput differences among different libraries, here is a bar chart of the test results:

使用示例(Usage Example)
<a name="example"></a>
以下我会将主要功能用一个服务器示例展现 / I will demonstrate the main functions using an example server as follows:
#include <moonnet/moonnet.h>
#include <signal.h>
#include <iostream>
using namespace moon;
// signalevent callback / 信号事件回调函数
void handle_signal(int signo, server *ser){
std::cout << "Caught signal " << signo << std::endl;
// Close the server and clean up resources / 关闭服务器,清理资源
ser->stop();
std::cout << "TCP Echo Server Termination" << std::endl;
}
void handle_read(bfevent* bev){
std::string str = bev->receive();
// Or get inbuff / 或者获取输入缓冲区
/* buffer* buff = bev->getinbuff();
std::string s = bev->receive(buff->readbytes()); */
bev->sendout(str);
// write(bev->getfd(),str.data()
