├── .gitattributes
├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── README.md
├── README_cn.md
├── doc
├── README.md
├── cn
│ ├── Async.md
│ ├── CycleBuffer.md
│ ├── DnsGet.md
│ ├── EventLoop.md
│ ├── GlobalConfig.md
│ ├── Idle.md
│ ├── ListBuffer.md
│ ├── LogWriter.md
│ ├── Packet.md
│ ├── PacketBuffer.md
│ ├── Signal.md
│ ├── SocketAddr.md
│ ├── TcpAccepter.md
│ ├── TcpClient.md
│ ├── TcpConnection.md
│ ├── TcpServer.md
│ ├── Timer.md
│ ├── TimerWheel.md
│ ├── Udp.md
│ └── http
│ │ ├── HttpClient.md
│ │ ├── HttpServer.md
│ │ ├── RadixTree.md
│ │ ├── Request.md
│ │ └── Response.md
└── en
│ ├── Async.md
│ ├── CycleBuffer.md
│ ├── DnsGet.md
│ ├── EventLoop.md
│ ├── GlobalConfig.md
│ ├── Idle.md
│ ├── ListBuffer.md
│ ├── LogWriter.md
│ ├── Packet.md
│ ├── PacketBuffer.md
│ ├── Signal.md
│ ├── SocketAddr.md
│ ├── TcpAccepter.md
│ ├── TcpClient.md
│ ├── TcpConnection.md
│ ├── TcpServer.md
│ ├── Timer.md
│ ├── TimerWheel.md
│ ├── Udp.md
│ └── http
│ ├── HttpClient.md
│ ├── HttpServer.md
│ ├── RadixTree.md
│ ├── Request.md
│ └── Response.md
├── examples
├── all_example
│ ├── Clinet.hpp
│ ├── EchoServer.cpp
│ ├── EchoServer.hpp
│ └── main.cpp
├── boost_asio_echo_client
│ └── main.cpp
├── boost_asio_echo_server
│ └── main.cpp
├── clients_cross_thread
│ ├── Client.cpp
│ ├── Client.hpp
│ └── main.cpp
├── dns
│ └── main.cpp
├── echo_client
│ └── main.cpp
├── echo_server
│ └── main.cpp
├── helloworld
│ └── main.cpp
├── http_client
│ └── main.cpp
├── http_server
│ └── main.cpp
├── loop_exit
│ └── main.cpp
├── pingpang
│ ├── Clinet.hpp
│ ├── EchoServer.cpp
│ ├── EchoServer.hpp
│ └── main.cpp
├── radix_tree
│ └── main.cpp
├── rpc
│ ├── RpcClient.cpp
│ ├── RpcClient.hpp
│ ├── RpcCtrl.cpp
│ ├── RpcCtrl.hpp
│ ├── RpcServer.cpp
│ ├── RpcServer.hpp
│ ├── main.cpp
│ └── protocol.hpp
└── server_quit
│ └── main.cpp
├── libuv1.30.0
├── include
│ ├── uv.h
│ └── uv
│ │ ├── aix.h
│ │ ├── android-ifaddrs.h
│ │ ├── bsd.h
│ │ ├── darwin.h
│ │ ├── errno.h
│ │ ├── linux.h
│ │ ├── os390.h
│ │ ├── posix.h
│ │ ├── stdint-msvc2008.h
│ │ ├── sunos.h
│ │ ├── threadpool.h
│ │ ├── tree.h
│ │ ├── unix.h
│ │ ├── version.h
│ │ └── win.h
└── lib
│ ├── gcc8.3.0
│ ├── libuv.a
│ ├── libuv.la
│ ├── libuv.so
│ ├── libuv.so.1
│ ├── libuv.so.1.0.0
│ └── pkgconfig
│ │ └── libuv.pc
│ └── win_sdk10.0.10240
│ ├── Debug
│ └── libuv.lib
│ └── Release
│ └── libuv.lib
├── project
├── cmake
│ └── build.sh
├── codeblocks_linux
│ ├── all_example
│ │ └── all_example.cbp
│ ├── clients
│ │ └── clients.cbp
│ ├── create_lib
│ │ └── create_lib.cbp
│ ├── dns
│ │ └── dns.cbp
│ ├── echo_server
│ │ └── echo_server.cbp
│ ├── eventloop_quit
│ │ └── eventloop_quit.cbp
│ ├── helloworld
│ │ └── helloworld.cbp
│ ├── http_client
│ │ └── http_client.cbp
│ ├── http_server
│ │ └── http_server.cbp
│ ├── pingpong
│ │ └── pingpong.cbp
│ ├── radix_tree
│ │ └── radix_tree.cbp
│ ├── rpc
│ │ └── rpc.cbp
│ └── server_quit
│ │ └── server_quit.cbp
└── vs2017
│ ├── DNS
│ ├── DNS.vcxproj
│ ├── DNS.vcxproj.filters
│ └── DNS.vcxproj.user
│ ├── all_example
│ ├── all_example.vcxproj
│ ├── all_example.vcxproj.filters
│ └── all_example.vcxproj.user
│ ├── clients
│ ├── clients.vcxproj
│ ├── clients.vcxproj.filters
│ └── clients.vcxproj.user
│ ├── echo_server
│ ├── echo_server.vcxproj
│ ├── echo_server.vcxproj.filters
│ └── echo_server.vcxproj.user
│ ├── helloworld
│ ├── helloworld.vcxproj
│ ├── helloworld.vcxproj.filters
│ └── helloworld.vcxproj.user
│ ├── http_client
│ ├── http_client.vcxproj
│ ├── http_client.vcxproj.filters
│ └── http_client.vcxproj.user
│ ├── http_server
│ ├── http_server.vcxproj
│ ├── http_server.vcxproj.filters
│ └── http_server.vcxproj.user
│ ├── loop_exit
│ ├── loop_exit.vcxproj
│ ├── loop_exit.vcxproj.filters
│ └── loop_exit.vcxproj.user
│ ├── pingpong
│ ├── pingpong.vcxproj
│ ├── pingpong.vcxproj.filters
│ └── pingpong.vcxproj.user
│ ├── radix_tree
│ ├── radix_tree.vcxproj
│ ├── radix_tree.vcxproj.filters
│ └── radix_tree.vcxproj.user
│ ├── rpc
│ ├── rpc.vcxproj
│ ├── rpc.vcxproj.filters
│ └── rpc.vcxproj.user
│ ├── sever_quit
│ ├── sever_quit.vcxproj
│ ├── sever_quit.vcxproj.filters
│ └── sever_quit.vcxproj.user
│ ├── uv-cpp.sln
│ └── uv_cpp_lib
│ ├── uv_cpp_lib.vcxproj
│ ├── uv_cpp_lib.vcxproj.filters
│ └── uv_cpp_lib.vcxproj.user
└── uv
├── Async.cpp
├── CycleBuffer.cpp
├── DnsGet.cpp
├── EventLoop.cpp
├── GlobalConfig.cpp
├── Idle.cpp
├── ListBuffer.cpp
├── LogWriter.cpp
├── Packet.cpp
├── Signal.cpp
├── SocketAddr.cpp
├── TcpAcceptor.cpp
├── TcpClient.cpp
├── TcpConnection.cpp
├── TcpServer.cpp
├── Timer.cpp
├── Udp.cpp
├── http
├── HttpClient.cpp
├── HttpCommon.cpp
├── HttpServer.cpp
├── Request.cpp
└── Response.cpp
└── include
├── Async.hpp
├── CycleBuffer.hpp
├── DnsGet.hpp
├── EventLoop.hpp
├── GlobalConfig.hpp
├── Idle.hpp
├── ListBuffer.hpp
├── LogWriter.hpp
├── Packet.hpp
├── PacketBuffer.hpp
├── Signal.hpp
├── SocketAddr.hpp
├── TcpAcceptor.hpp
├── TcpClient.hpp
├── TcpConnection.hpp
├── TcpServer.hpp
├── Timer.hpp
├── TimerWheel.hpp
├── Udp.hpp
├── http
├── HttpClient.hpp
├── HttpCommon.hpp
├── HttpServer.hpp
├── RadixTree.hpp
├── Request.hpp
└── Response.hpp
└── uv11.hpp
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.h linguist-language=C++
2 | *.lua linguist-language=C++
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.idea
2 | CMakeLists.txt.user
3 | cmake-build-debug/*
4 |
5 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # CMakeLists.txt
2 | # Copyright (C) orcaer@yeah.net. All rights reserved.
3 | # Last modified: 2019-9-3
4 |
5 | CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
6 |
7 | PROJECT(uv_cpp)
8 |
9 | INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/
10 | ${PROJECT_SOURCE_DIR}/libuv1.30.0/include)
11 |
12 | AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/uv SOURCES)
13 | AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/uv/http SOURCES)
14 |
15 | LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/libuv1.30.0/lib/gcc8.3.0)
16 |
17 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -O2")
18 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2")
19 |
20 |
21 | MESSAGE("building...")
22 |
23 | ADD_LIBRARY (${PROJECT_NAME} STATIC ${SOURCES} )
24 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} "libuv.a" -pthread)
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 莫失莫忘
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/doc/README.md:
--------------------------------------------------------------------------------
1 | API's document, [中文][1] or [English][2].
2 |
3 | [1]: https://github.com/wlgq2/uv-cpp/tree/master/doc/cn
4 | [2]: https://github.com/wlgq2/uv-cpp/tree/master/doc/en
5 |
--------------------------------------------------------------------------------
/doc/cn/Async.md:
--------------------------------------------------------------------------------
1 | # Async
2 | 异步接口,用于跨线程调用函数。
3 |
4 | ```C++
5 | Async(EventLoop* loop)
6 | ```
7 | 构造函数
8 | * EventLoop* loop : EventLoop对象句柄。
9 |
10 | ```C++
11 | virtual ~Async()
12 | ```
13 | 析构函数
14 |
15 | ```C++
16 | void runInThisLoop(DefaultCallback callback)
17 | ```
18 | 注册一个回调函数到eventloop中执行。
19 | * DefaultCallback callback : 注册运行回调函数。
20 |
21 | ```C++
22 | void close(DefaultCallback callback)
23 | ```
24 | 关闭async接口。回调函数被执行时,对象可以被安全释放。
25 | * DefaultCallback callback : 关闭完成后的回调函数。
26 |
27 |
--------------------------------------------------------------------------------
/doc/cn/CycleBuffer.md:
--------------------------------------------------------------------------------
1 | # CycleBuffer : PacketBuffer
2 | 循环数据缓存,PacketBuffer 接口的一种实现。
3 | ```C++
4 | CycleBuffer()
5 | ```
6 | 构造函数。
7 |
8 | ```C++
9 | virtual ~CycleBuffer()
10 | ```
11 | 析构函数。
12 |
13 |
--------------------------------------------------------------------------------
/doc/cn/DnsGet.md:
--------------------------------------------------------------------------------
1 | # DnsGet
2 | DNS查询接口
3 |
4 | ```C++
5 | DNSGet(uv::EventLoop* loop)
6 | ```
7 | 构造函数
8 | * EventLoop* loop : EventLoop对象句柄。
9 |
10 | ```C++
11 | void setOnDNSCallback(OnGetIPAddrCallback callback)
12 | ```
13 |
设置获取DNS回调函数
14 | OnGetIPAddrCallback 函数原型: void (int statue, std::string& ip)
15 |
int status : 状态码,获取成功则为0。
16 |
std::string& ip : 返回域名ip地址
17 |
18 |
19 | ```C++
20 | int GetIP(std::string& hostname, std::string service = "")
21 | ```
22 |
23 | 获取ip地址。
24 | * std::string& hostname : 域名, "www.google.com"
25 | * std::string service : 服务命, "http"、"ftp"、"80"……
26 | * 返回值 : 成功则为0。
27 |
28 | ```C++
29 | int GetIP(std::string&& hostname, std::string service = "");
30 | ```
31 | 同上。
32 |
33 |
--------------------------------------------------------------------------------
/doc/cn/EventLoop.md:
--------------------------------------------------------------------------------
1 | # EventLoop
2 | 事件轮询类,单线程。
3 |
4 | ```C++
5 | EventLoop()
6 | ```
7 | 构造函数。
8 |
9 | ```C++
10 | ~EventLoop()
11 | ```
12 | 析构函数。
13 |
14 | ```C++
15 | static EventLoop* DefaultLoop()
16 | ```
17 | 获取单例Loop。
18 |
19 | ```C++
20 | int run()
21 | ```
22 | 阻塞运行loop。
23 | * 返回值 : 调用uv_stop后,仍有活跃对象则返回非0,否则返回0。
24 |
25 | ```C++
26 | int runNoWait()
27 | ```
28 | 非阻塞运行loop。
29 | * 返回值 : 仍有活跃对象则返回非0,否则返回0。
30 |
31 | ```C++
32 | int stop()()
33 | ```
34 | 异步停止事件轮询。事件轮询会在run()返回后停止,stop()调用后并不会立即停止。
35 | * 返回值 : 非0为失败(事件处于已经停止或未运行状态)。
36 |
37 | ```C++
38 | bool isRunInLoopThread()
39 | ```
40 | 判定当前是否在loop线程中
41 | * 返回 true:当前为loop线程 false: 当前不为loop线程
42 |
43 | ```C++
44 | void runInThisLoop(const DefaultCallback func)
45 | ```
46 | 注册回调函数到loop线程中执行,确保每次注册的函数都能被执行。
47 | * const DefaultCallback func 回调函数
48 |
49 | ```C++
50 | uv_loop_t* handle()
51 | ```
52 | 返回libuv的loop句柄
53 |
54 | ```C++
55 | static const char* GetErrorMessage(int status)
56 | ```
57 |
获取错误信息
58 | * int status 错误码
59 |
60 |
--------------------------------------------------------------------------------
/doc/cn/GlobalConfig.md:
--------------------------------------------------------------------------------
1 | # GlobalConfig
2 | 全局变量及配置。
3 | ```C++
4 | static BufferMode BufferModeStatus
5 | ```
6 | 使用数据缓存模式,NoBuffer、CycleBuffer、ListBuffer。默认为NoBuffer。
7 |
8 | ```C++
9 | static uint64_t CycleBufferSize
10 | ```
11 | 循环缓存数据大小,默认32KB。
12 |
13 | ```C++
14 | static ReadBufferPacketFunc ReadBufferPacket
15 | ```
16 | Packet数据包解析读取函数,默认为Packet::readFromBuffer。
17 |
18 | ```C++
19 | static ReadBufferStringFunc ReadBufferString
20 | ```
21 | String数据包解析读取函数,默认为空。
22 |
23 | ```C++
24 | static ReadBufferVoidFunc ReadBufferVoid
25 | ```
26 | 自定义数据包解析读取函数,默认为空。
27 |
--------------------------------------------------------------------------------
/doc/cn/Idle.md:
--------------------------------------------------------------------------------
1 | # Idle
2 | 空闲事件,在每次EventLoop事件中都被调用。
3 |
4 | ```C++
5 | Idle(EventLoop* loop)
6 | ```
7 | 构造函数
8 | * EventLoop* loop : EventLoop对象句柄。
9 |
10 | ```C++
11 | virtual ~Idle()
12 | ```
13 | 析构函数
14 |
15 | ```C++
16 | void setCallback(DefaultCallback callback)
17 | ```
18 | 注册一个回调函数到eventloop中执行。
19 | * DefaultCallback callback : 注册运行回调函数。
20 |
--------------------------------------------------------------------------------
/doc/cn/ListBuffer.md:
--------------------------------------------------------------------------------
1 | # ListBuffer : PacketBuffer
2 | List buffer ,an implementation of the PacketBuffer interface.
3 | ```C++
4 | ListBuffer()
5 | ```
6 | Constructor.
7 |
8 | ```C++
9 | virtual ~ListBuffer()
10 | ```
11 | Destructor.
12 |
13 |
--------------------------------------------------------------------------------
/doc/cn/LogWriter.md:
--------------------------------------------------------------------------------
1 | # LogWriter
2 | 日志输出接口。
3 |
4 | ```C++
5 | static LogWriter* Instance();
6 | ```
7 | 获取单例日志对象。
8 |
9 | ```C++
10 | static void ToHex(std::string& message, const char* data, unsigned int size);
11 | ```
12 | 字节流转hex文本。
13 | * std::string& message : 输出hex文本。
14 | * const char* data : 输入字符数据。
15 | * unsigned int size : 输入字符数据长度。
16 |
17 | ```C++
18 | static void ToHex(std::string& message, std::string& data);
19 | ```
20 | 字节流转hex文本。
21 | * std::string& message : 输出hex文本。
22 | * std::string& data : 输入字符串数据。
23 |
24 |
25 | ```C++
26 | void registerInterface(WriteLogCallback callback);
27 | ```
28 | 注册日志输出函数。
29 | * WriteLogCallback callback :日志输出函数,未设置则默认使用std::cout。
30 | * WriteLogCallback : void(int level,const std::string& log)
31 |
32 |
33 | ```C++
34 | void setLevel(int level);
35 | ```
36 | 设置日志输出等级阈值。
37 | * int level : 日志等级阈值。
38 |
39 |
40 | ```C++
41 | int getLevel();
42 | ```
43 | 获取日志输出等级阈值。
44 | * 返回值 :当前日志输出等级阈值。
45 |
46 |
47 | ```C++
48 | const std::string& getLevelName(int level);
49 | ```
50 | 获取日志等级对应字符串。
51 | * int level : 日志等级。
52 | * 返回值 : 日志等级字符串,"Debug","Info","Warn","Error","Fatal"。
53 |
54 |
55 | ```C++
56 | void write(Level level, const std::string& data);
57 | ```
58 | 写日志。
59 | * Level level : 日志等级。
60 | * const std::string& data : 日志内容。
61 |
62 |
63 | ```C++
64 | void fatal(const std::string& data);
65 | ```
66 | 写fatal等级日志
67 | * const std::string& data : 日志内容
68 |
69 |
70 | ```C++
71 | void warn(const std::string& data);
72 | ```
73 | 写warn等级日志
74 | * const std::string& data : 日志内容
75 |
76 |
77 | ```C++
78 | void error(const std::string& data);
79 | ```
80 | 写error等级日志
81 | * const std::string& data : 日志内容
82 |
83 |
84 |
85 | ```C++
86 | void info(const std::string& data);
87 | ```
88 | 写info等级日志
89 | * const std::string& data : 日志内容
90 |
91 |
92 | ```C++
93 | void debug(const std::string& data);
94 | ```
95 | 写debug等级日志
96 | * const std::string& data : 日志内容
97 |
98 |
--------------------------------------------------------------------------------
/doc/cn/Packet.md:
--------------------------------------------------------------------------------
1 | # Packet
2 |
数据包格式:
3 | 包头 | 数据长度|数据段|包尾|
4 | :---------:|:--------:|:--------:|:--------:|
5 | 1字节 | 2字节|N字节|1字节|
6 | ```C++
7 | Packet()
8 | ```
9 | 构造函数。
10 |
11 | ```C++
12 | ~Packet()
13 | ```
14 | 析构函数。
15 |
16 | ```C++
17 | void pack(const char* data, uint16_t size)
18 | ```
19 | 数据写入packet类。
20 | * const char* data : 写入数据地址。
21 | * uint16_t size : 写入数据长度。
22 |
23 | ```C++
24 | const char* getData()
25 | ```
26 | 读取数据缓存指针。
27 | * 返回值 : 数据缓存地址。
28 |
29 | ```C++
30 | const uint16_t DataSize()
31 | ```
32 | 读取数据缓存长度。
33 | * 返回值 : 数据缓存长度。
34 |
35 | ```C++
36 | const std::string& Buffer()
37 | ```
38 | 读取包缓存。
39 | * 返回值 : 数据包缓存。
40 |
41 | ```C++
42 | const uint32_t PacketSize()
43 | ```
44 | 读取包缓存长度。
45 | * 返回值 : 数据包缓存长度。
46 |
47 | ```C++
48 | void swap(std::string& str)
49 | ```
50 | 交换缓存。
51 | * std::string& str : 交换缓存string。
52 |
53 | ```C++
54 | static int readFromBuffer(PacketBuffer*, Packet&)
55 | ```
56 | 从PacketBuffer接口中读取包数据。
57 | * PacketBuffer* : PacketBuffer接口。
58 | * Packet& :读取的Packet对象。
59 |
60 | ```C++
61 | template
62 | static void UnpackNum(const uint8_t* data, NumType& num)
63 | ```
64 | 从字节流中解析数字。
65 | * const uint8_t* data : 字节流。
66 | * NumType& num :解析出的数字。
67 |
68 | ```C++
69 | template
70 | static void PackNum(char* data, NumType num)
71 | ```
72 | 把数字打包刀字节流。
73 | * char* data : 字节流。
74 | * NumType num :被打包的数字。
75 |
76 | ```C++
77 | static uint32_t PacketMinSize()
78 | ```
79 | 数据包最小长度(包头+数据长度字段+包尾)
80 | * 返回值 : 数据包最小长度。
81 |
82 |
--------------------------------------------------------------------------------
/doc/cn/PacketBuffer.md:
--------------------------------------------------------------------------------
1 | # PacketBuffer
2 | 包缓存接口。
3 | ```C++
4 | PacketBuffer()
5 | ```
6 | 构造函数。
7 |
8 | ```C++
9 | virtual ~PacketBuffer()
10 | ```
11 | 析构函数。
12 |
13 | ```C++
14 | virtual int append(const char* data, uint64_t size) = 0
15 | ```
16 | 向缓存写入数据接口。
17 | * const char* data : 写入数据地址。
18 | * uint16_t size : 写入数据长度。
19 | * 返回值 : 0 成功,-1失败(缓存不够)。
20 |
21 | ```C++
22 | virtual int readBufferN(std::string& data, uint64_t N) = 0
23 | ```
24 | 从缓存读出N字节数据,读过程不清除数据(性能考虑)。
25 | * std::string& data : 读数据接口。
26 | * uint64_t N : 读数据长度。
27 | * 返回值 : 0 成功,-1失败。
28 |
29 | ```C++
30 | virtual int clearBufferN(uint64_t N) = 0
31 | ```
32 | 删除N字节数据。
33 | * uint64_t N : 删除数据长度。
34 | * 返回值 : 0 成功,-1失败。
35 |
36 | ```C++
37 | virtual int clear() = 0
38 | ```
39 | 删除所有数据。
40 | * 返回值 : 0 成功,-1失败。
41 |
42 | ```C++
43 | irtual uint64_t readSize() = 0
44 | ```
45 | 当前可读数据字节长度。
46 | * 返回值 : 可读字节长度。
47 |
48 | ```C++
49 | int readString(std::string& out)
50 | ```
51 | 从缓存中读取一个字符串数据包。
52 | * std::string& out : 字符串数据包。
53 |
54 | ```C++
55 | int readPacket(Packet& out)
56 | ```
57 | 从缓存中读取一个Packet对象数据包。
58 | * Packet& out : Packet对象数据包。
59 |
60 | ```C++
61 | int readGeneric(void* out)
62 | ```
63 | 从缓存中读取一个自定义对象数据包。
64 | * void* out : 自定义数据对象指针。
65 |
--------------------------------------------------------------------------------
/doc/cn/Signal.md:
--------------------------------------------------------------------------------
1 | # Signal
2 | libuv信号封装。
3 |
4 | ```C++
5 | Signal(EventLoop* loop,int sig, SignalHandle handle = nullptr);
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : event loop句柄。
9 | * int sig : 信号值。
10 | * SignalHandle handle : 信号回调函数。
11 |
12 | ```C++
13 | void close(DefaultCallback callback);
14 | ```
15 | 关闭信号回调。
16 | * DefaultCallback callback : 关闭完成回调函数。
17 |
18 | ```C++
19 | virtual ~Signal();
20 | ```
21 | 析构函数。
22 |
23 |
24 | ```C++
25 | void setHandle(SignalHandle handle);
26 | ```
27 | 设置回调函数。
28 | * SignalHandle handle :信号回调函数。
29 |
30 |
31 | ```C++
32 | static void Ignore(int sig);
33 | ```
34 | 忽略某个信号,仅linux环境下有效。
35 | * int sig : 信号值。
36 |
37 |
--------------------------------------------------------------------------------
/doc/cn/SocketAddr.md:
--------------------------------------------------------------------------------
1 | # SocketAddr
2 | IP地址协议及端口封装。
3 |
4 | ```C++
5 | SocketAddr(const std::string&& ip, unsigned short port, IPV ipv = Ipv4)
6 | ```
7 | 构造函数。
8 | * const std::string&& ip : ip地址。
9 | * unsigned short port : 端口号。
10 | * IPV ipv : ip协议版本,ipv4、ipv6。
11 |
12 | ```C++
13 | SocketAddr(const std::SocketAddr& ip, unsigned short port, IPV ipv = Ipv4)
14 | ```
15 | 构造函数。
16 | * const std::string& ip : ip地址。
17 | * unsigned short port : 端口号。
18 | * IPV ipv : ip协议版本,ipv4、ipv6。
19 |
20 | ```C++
21 | SocketAddr(const sockaddr* addr, IPV ipv = Ipv4)
22 | ```
23 | 构造函数。
24 | * const sockaddr* addr : ip地址信息。
25 | * IPV ipv : ip协议版本,ipv4、ipv6。
26 |
27 | ```C++
28 | const sockaddr* Addr()
29 | ```
30 | 获取ip地址端口sockaddr结构信息。
31 | * 返回值: ip地址信息。
32 |
33 | ```C++
34 | void toStr(std::string& str)
35 | ```
36 | ip地址转字符串,格式"xxx.xxx.xxx.xxx:port"。
37 | * std::string& str : 传入字符串引用,返回IP端口信息到该字符串。
38 |
39 | ```C++
40 | std::string toStr()
41 | ```
42 | ip地址转字符串,格式"xxx.xxx.xxx.xxx:port"。
43 | * 返回值 : 返回IP端口信息到该字符串。
44 |
45 | ```C++
46 | IPV Ipv()
47 | ```
48 | 返回ip版本信息。
49 | * 返回值 : ipv4、ipv6。
50 |
51 | ```C++
52 | static void AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv = Ipv4)
53 | ```
54 | 获取客户端的ip及端口的字符串。
55 | * uv_tcp_t* client : 客户端连接句柄。
56 | * std::string& addrStr : 输入字符串,返回ip地址端口信息。
57 | * IPV ipv : ip协议版本,ipv4、ipv6。
58 |
59 | ```C++
60 | static uint16_t GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv = Ipv4)
61 | ```
62 | 获取ip和端口。
63 | * const sockaddr_storage* : 传入的socket句柄。
64 | * std::string& out : 输入字符串,返回ip地址。
65 | * IPV ipv : ip协议版本,ipv4、ipv6。
66 | * 返回值 : 端口号。
--------------------------------------------------------------------------------
/doc/cn/TcpAccepter.md:
--------------------------------------------------------------------------------
1 | # TcpAcceptor
2 | Tcp监听类,监听接收tcp连接。
3 |
4 | ```C++
5 | TcpAcceptor(EventLoop* loop, bool tcpNoDelay)
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 | * bool tcpNoDelay : Nagle 算法, true : 不延迟 false : 延迟。
10 |
11 | ```C++
12 | virtual TcpAcceptor()
13 | ```
14 | 析构函数。
15 |
16 | ```C++
17 | int bind(SocketAddr& addr)
18 | ```
19 | 绑定ip地址及端口。
20 | * SocketAddr& addr : ip及端口信息。
21 | * 返回值 : 0为成功。
22 |
23 | ```C++
24 | int listen()
25 | ```
26 | 监听地址及端口。
27 | * 返回值 : 0为成功。
28 |
29 | ```C++
30 | bool isListen()
31 | ```
32 | 是否监听状态。
33 | * 返回值 : 是否在监听状态。
34 |
35 | ```C++
36 | bool isTcpNoDelay()
37 | ```
38 | 是否开启no_delay.
39 | * 返回值 : 开启不延时。
40 |
41 | ```C++
42 | EventLoop* Loop()
43 | ```
44 | 返回Event loop句柄。
45 | * 返回值 : EventLoop句柄。
46 |
47 | ```C++
48 | void setNewConnectionCallback( NewConnectionCallback callback)
49 | ```
50 | 设置新连接回调函数。
51 | * NewConnectionCallback callback : 新连接回调函数。
52 | * NewConnectionCallback : void(EventLoop* ,UVTcpPtr)。
--------------------------------------------------------------------------------
/doc/cn/TcpClient.md:
--------------------------------------------------------------------------------
1 | # TcpClient
2 | Tcp客户端类。
3 |
4 | ```C++
5 | TcpClient(EventLoop* loop,bool tcpNoDelay = true);
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 | * bool tcpNoDelay : Nagle 算法, true : 不延迟 false : 延迟。
10 |
11 | ```C++
12 | virtual ~TcpClient();
13 | ```
14 | 析构函数。
15 |
16 | ```C++
17 | bool isTcpNoDelay();
18 | ```
19 | 返回是否关闭nagle算法。
20 | * 返回值 : 是否关闭nagle算法。
21 |
22 | ```C++
23 | void setTcpNoDelay(bool isNoDelay)
24 | ```
25 | 设置是否开启nagle算法。
26 | * bool isNoDelay : 关闭negle算法。
27 |
28 | ```C++
29 | void connect(SocketAddr& addr)
30 | ```
31 | 连接服务器。
32 | * SocketAddr& addr : 服务器地址。
33 |
34 | ```C++
35 | void close(std::function callback)
36 | ```
37 | 关闭客户端。
38 | * std::function callback : 关闭完成回调函数,参数为Client指针,回调被调用可以安全删除客户端对象。
39 |
40 | ```C++
41 | void write(const char* buf, unsigned int size, AfterWriteCallback callback = nullptr)
42 | ```
43 | 写tcp socket缓存。
44 | * const char* buf : 写消息指针。
45 | * unsigned int size : 消息长度。
46 | * AfterWriteCallback callback : 写缓存回调函数,返回成功状态。
47 |
48 | ```C++
49 | void writeInLoop(const char* buf, unsigned int size, AfterWriteCallback callback)
50 | ```
51 | 写tcp socket缓存,在loop线程中执行,可以跨线程调用,线程安全。
52 | * const char* buf : 写消息指针。
53 | * unsigned int size : 消息长度。
54 | * AfterWriteCallback callback : 写缓存回调函数,返回成功状态,回调函数中可以安全删除写消息buffer。
55 |
56 | ```C++
57 | void setConnectStatusCallback(ConnectStatusCallback callback);
58 | ```
59 | 设置连接状态改变回调函数。断开连接、连接成功、连接失败。
60 | * ConnectStatusCallback callback : 连接状态改变回调函数。
61 | * ConnectStatusCallback格式 : void(ConnectStatus)
62 |
63 | ```C++
64 | void setMessageCallback(NewMessageCallback callback)
65 | ```
66 | 设置新消息回调函数。
67 | * NewMessageCallback callback : 新消息回调函数。
68 | * NewMessageCallback格式 : void(const char*,ssize_t)
69 |
70 | ```C++
71 | EventLoop* Loop();
72 | ```
73 | 返回Event loop句柄。
74 | * 返回值 : Event loop句柄。
75 |
76 | ```C++
77 | PacketBufferPtr getCurrentBuf()
78 | ```
79 | 返回数据包缓存接口。
80 | * 返回值 : 返回PacketBufferPtr缓存接口,用于缓存解析消息包数据。
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/doc/cn/TcpConnection.md:
--------------------------------------------------------------------------------
1 | # TcpConnection
2 | Tcp连接类。
3 |
4 | ```C++
5 | TcpConnection(EventLoop* loop,std::string& name,UVTcpPtr client,bool isConnected = true)
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 | * std::string& name : 连接标识,ip+端口。
10 | * UVTcpPtr client : libuv tcp句柄。
11 | * bool isConnected : 连接是否建立。
12 |
13 | ```C++
14 | virtual ~TcpConnection()
15 | ```
16 | 析构函数。
17 |
18 | ```C++
19 | void onSocketClose()
20 | ```
21 | 连接被断开。
22 |
23 | ```C++
24 | void close(std::function callback)
25 | ```
26 | 关闭连接。
27 | * std::function callback : 关闭连接完成后回调函数。
28 |
29 | ```C++
30 | int write(const char* buf,ssize_t size,AfterWriteCallback callback)
31 | ```
32 | 写TCP缓存。
33 | * const char* buf : 发送数据buffer。
34 | * ssize_t size : 数据长度。
35 | * AfterWriteCallback callback : 写完成回调。
36 | * 返回值 : 0为成功。
37 |
38 | ```C++
39 | void writeInLoop(const char* buf,ssize_t size,AfterWriteCallback callback)
40 | ```
41 | 写TCP缓存,强制在Eventloop轮询中写,线程安全。
42 | * const char* buf : 发送数据buffer。
43 | * ssize_t size : 数据长度。
44 | * AfterWriteCallback callback : 写完成回调。
45 |
46 | ```C++
47 | void setWrapper(std::shared_ptr conn)
48 | ```
49 | 设置包装,用于时间轮心跳超时判定。
50 | * std::shared_ptr conn : tcp连接包装。
51 |
52 | ```C++
53 | std::shared_ptr getWrapper()
54 | ```
55 | 获取连接包装,用于时间轮心跳超时判定。
56 | * 返回值 : tcp连接包装。
57 |
58 | ```C++
59 | void setMessageCallback(OnMessageCallback callback)
60 | ```
61 | 设置消息回调。
62 | * OnMessageCallback callback : 新消息回调函数。
63 |
64 | ```C++
65 | void setConnectCloseCallback(OnCloseCallback callback)
66 | ```
67 | 设置连接关闭回调函数。
68 | * OnCloseCallback callback : 连接关闭回调。
69 |
70 | ```C++
71 | void setConnectStatus(bool status)
72 | ```
73 | 设置连接状态。
74 | * bool status : 连接/断开。
75 |
76 | ```C++
77 | bool isConnected()
78 | ```
79 | 获取连接状态。
80 | * 返回值 : 连接/断开。
81 |
82 | ```C++
83 | const std::string& Name()
84 | ```
85 | 获取连接标识。
86 | * 返回值 : 连接标识,ip+端口。
87 |
88 | ```C++
89 | PacketBufferPtr getPacketBuffer()
90 | ```
91 | 获取应用层包缓存接口。
92 | * 返回值 : 应用层包缓存接口。
--------------------------------------------------------------------------------
/doc/cn/Timer.md:
--------------------------------------------------------------------------------
1 | # Timer
2 | 定时器类。
3 |
4 | ```C++
5 | Timer(EventLoop* loop, uint64_t timeout, uint64_t repeat, TimerCallback callback)
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 | * int64_t timeout : 第一次超时时间,单位毫秒。
10 | * uint64_t repeat : 定时器周期,单位毫秒,为零为不重复。
11 | * TimerCallback callback : 定时器回调函数。
12 |
13 | ```C++
14 | virtual ~Timer()
15 | ```
16 | 析构函数。
17 |
18 | ```C++
19 | void start()
20 | ```
21 | 开始定时器。
22 |
23 | ```C++
24 | void close(TimerCloseComplete callback)
25 | ```
26 | 关闭定时器。
27 | * TimerCloseComplete callback : 关闭完成回调函数,可安全析构定时器对象。
28 | * TimerCloseComplete : void(Timer*)。
29 |
30 | ```C++
31 | void setTimerRepeat(uint64_t ms)
32 | ```
33 | 修改定时器周期。
34 | * uint64_t ms : 定时器周,单位毫秒。
--------------------------------------------------------------------------------
/doc/cn/TimerWheel.md:
--------------------------------------------------------------------------------
1 | # TimerWheel template
2 | 时间轮类。
3 |
4 | ```C++
5 | TimerWheel(EventLoop* loop)
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 |
10 | ```C++
11 | TimerWheel(EventLoop* loop,unsigned int timeout)
12 | ```
13 | 构造函数。
14 | * EventLoop* loop : Event loop句柄。
15 | * unsigned int timeout : 超时时间,单位秒。
16 |
17 | ```C++
18 | void setTimeout(unsigned int seconds)
19 | ```
20 | 设置超时时间,单位秒。
21 | * unsigned int timeout : 超时时间,单位秒。
22 |
23 | ```C++
24 | int getTimeout()
25 | ```
26 | 返回超时时间。
27 | * 返回值 : 超时时间,单位秒。
28 |
29 | ```C++
30 | void start()
31 | ```
32 | 开始时间轮。
33 |
34 | ```C++
35 | void insert(std::shared_ptr value)
36 | ```
37 | 插入时间轮对象。
38 | * std::shared_ptr value : 时间轮元素对象。
--------------------------------------------------------------------------------
/doc/cn/Udp.md:
--------------------------------------------------------------------------------
1 | # Udp
2 | Udp类。
3 |
4 | ```C++
5 | Udp(EventLoop* loop)
6 | ```
7 | 构造函数。
8 | * EventLoop* loop : Event loop句柄。
9 |
10 | ```C++
11 | virtual ~Udp()
12 | ```
13 | 析构函数。
14 |
15 | ```C++
16 | int bindAndRead(SocketAddr& addr)
17 | ```
18 | 绑定地址并开始读数据。
19 | * SocketAddr& addr : 绑定地址。
20 | * 返回值 : 0为成功。
21 |
22 | ```C++
23 | int send(SocketAddr& to, const char* buf, unsigned size)
24 | ```
25 | 发送数据。
26 | * SocketAddr& to : 发送地址。
27 | * const char* buf : 发送数据缓存。
28 | * unsigned size : 发送数据长度。
29 | * 返回值 : 0 为成功。
30 |
31 | ```C++
32 | void close(DefaultCallback callback)
33 | ```
34 | 关闭Udp。
35 | * DefaultCallback callback : 关闭完成回调函数。
36 |
37 | ```C++
38 | void setMessageCallback(OnUdpMessageCallback callback)
39 | ```
40 | 设置消息回调函数。
41 | * OnUdpMessageCallback callback : 新消息回调函数。
--------------------------------------------------------------------------------
/doc/cn/http/HttpClient.md:
--------------------------------------------------------------------------------
1 | # HttpClient
2 | Http客户端。
3 |
4 | ```C++
5 | enum ReqResult
6 | ```
7 | Http请求结果。
8 | * Success : 请求成功。
9 | * ConnectFail : 连接服务器失败。
10 | * ParseFail : 解析数据失败。
11 | * Unknow : 未知失败。
12 |
13 | ```C++
14 | HttpClient(EventLoop* loop)
15 | ```
16 | 构造函数
17 | * EventLoop* loop : Event loop句柄。
18 |
19 | ```C++
20 | virtual ~HttpClient()
21 | ```
22 | 析构函数。
23 |
24 | ```C++
25 | void setOnResp(OnRespCallback callback)
26 | ```
27 | 设置请求服务端数据回调函数。
28 | * OnRespCallback callback : 请求回调函数。
29 | * OnRespCallback原型 : void(ReqResult, Response* )
30 |
31 | ```C++
32 | void Req(uv::SocketAddr& addr,Request& req)
33 | ```
34 | 请求一次Http。
35 | * uv::SocketAddr& addr : 服务器地址,可由DNS模块获得。
36 | * Request& req : Http请求数据。
--------------------------------------------------------------------------------
/doc/cn/http/HttpServer.md:
--------------------------------------------------------------------------------
1 | # HttpServer
2 | Http服务器。
3 |
4 | ```C++
5 | HttpServer(EventLoop* loop)
6 | ```
7 | 构造函数
8 | * EventLoop* loop : Event loop句柄。
9 |
10 | ```C++
11 | void Get(std::string path, OnHttpReqCallback callback)
12 | ```
13 | 设置Get请求回调函数。
14 | * std::string path : 请求路径,支持通配符、参数等。
15 | * OnHttpReqCallback callback : 回调函数。
16 |
17 | ```C++
18 | void Post(std::string path, OnHttpReqCallback callback)
19 | ```
20 | 设置Post请求回调函数。
21 | * std::string path : 请求路径,支持通配符、参数等。
22 | * OnHttpReqCallback callback : 回调函数。
23 |
24 | ```C++
25 | void Head(std::string path, OnHttpReqCallback callback)
26 | ```
27 | 设置Head请求回调函数。
28 | * std::string path : 请求路径,支持通配符、参数等。
29 | * OnHttpReqCallback callback : 回调函数。
30 |
31 | ```C++
32 | void Put(std::string path, OnHttpReqCallback callback)
33 | ```
34 | 设置Put请求回调函数。
35 | * std::string path : 请求路径,支持通配符、参数等。
36 | * OnHttpReqCallback callback : 回调函数。
37 |
38 | ```C++
39 | void Delete(std::string path, OnHttpReqCallback callback)
40 | ```
41 | 设置Delete请求回调函数。
42 | * std::string path : 请求路径,支持通配符、参数等。
43 | * OnHttpReqCallback callback : 回调函数。
44 |
45 | ```C++
46 | void Connect(std::string path, OnHttpReqCallback callback)
47 | ```
48 | 设置Connect请求回调函数。
49 | * std::string path : 请求路径,支持通配符、参数等。
50 | * OnHttpReqCallback callback : 回调函数。
51 |
52 | ```C++
53 | void Options(std::string path, OnHttpReqCallback callback)
54 | ```
55 | 设置Options请求回调函数。
56 | * std::string path : 请求路径,支持通配符、参数等。
57 | * OnHttpReqCallback callback : 回调函数。
58 |
59 | ```C++
60 | void Trace(std::string path, OnHttpReqCallback callback)
61 | ```
62 | 设置Trace请求回调函数。
63 | * std::string path : 请求路径,支持通配符、参数等。
64 | * OnHttpReqCallback callback : 回调函数。
65 |
66 | ```C++
67 | void Patch(std::string path, OnHttpReqCallback callback)
68 | ```
69 | 设置Patch请求回调函数。
70 | * std::string path : 请求路径,支持通配符、参数等。
71 | * OnHttpReqCallback callback : 回调函数。
--------------------------------------------------------------------------------
/doc/cn/http/RadixTree.md:
--------------------------------------------------------------------------------
1 | # RadixTree
2 | 压缩前缀树。
3 |
4 | ```C++
5 | RadixTree()
6 | ```
7 | 构造函数
8 |
9 | ```C++
10 | virtual ~RadixTree()
11 | ```
12 | 析构函数
13 |
14 | ```C++
15 | void set(std::string& key, Type value)
16 | ```
17 | 设置key和存储值。
18 | * std::string& key : 键值。
19 | * Type value : 存储值。
20 |
21 | ```C++
22 | void set(std::string&& key, Type value)
23 | ```
24 | 设置key和存储值。
25 | * std::string&& key : 键值右值。
26 | * Type value : 存储值。
27 |
28 | ```C++
29 | bool get(const std::string& key, Type& value)
30 | ```
31 | 获取某键对应值。
32 | * const std::string& key : 键值。
33 | * Type& value : 键对应值。
34 | * 返回值 : true为获取成功,false为键值不存在。
35 |
36 | ```C++
37 | bool get(const std::string&& key, Type& value)
38 | ```
39 | 获取某键对应值。
40 | * const std::string& key : 键值右值。
41 | * Type& value : 键对应值。
42 | * 返回值 : true为获取成功,false为键值不存在。
43 |
44 | ```C++
45 | RadixTreeNodePtr Root()
46 | ```
47 | 返回根节点智能指针,树为空则返回nullptr。
48 | * 返回值 : 根节点指针。
49 |
50 | ```C++
51 | static char WildCard
52 | ```
53 | 键值通配符,默认为'*'。
54 |
--------------------------------------------------------------------------------
/doc/en/Async.md:
--------------------------------------------------------------------------------
1 | # Async
2 | Asynchronous interface, used to across threads.
3 |
4 | ```C++
5 | Async(EventLoop* loop)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : EventLoop's handle.
9 |
10 | ```C++
11 | virtual ~Async()
12 | ```
13 | Destructor.
14 |
15 | ```C++
16 | void runInThisLoop(DefaultCallback callback)
17 | ```
18 | Register a callback function run in eventloop.
19 | * DefaultCallback callback : callback function.
20 |
21 | ```C++
22 | void close(DefaultCallback callback)
23 | ```
24 | Close async object. The async's object can be released safely when the callback function is called,
25 |
26 | * DefaultCallback callback : Is called when the close is complete.
27 |
28 |
--------------------------------------------------------------------------------
/doc/en/CycleBuffer.md:
--------------------------------------------------------------------------------
1 | # CycleBuffer : PacketBuffer
2 | Cycle buffer ,an implementation of the PacketBuffer interface.
3 | ```C++
4 | CycleBuffer()
5 | ```
6 | Constructor.
7 |
8 | ```C++
9 | virtual ~CycleBuffer()
10 | ```
11 | Destructor.
12 |
13 |
--------------------------------------------------------------------------------
/doc/en/DnsGet.md:
--------------------------------------------------------------------------------
1 | # DnsGet
2 | DNS utility.
3 |
4 | ```C++
5 | DNSGet(uv::EventLoop* loop)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : EventLoop's handle.
9 |
10 | ```C++
11 | void setOnDNSCallback(OnGetIPAddrCallback callback)
12 | ```
13 |
Set DNS callback function.
14 | OnGetIPAddrCallback function format : void (int statue, std::string& ip)
15 |
int status : will 0 while successed.
16 | std::string& ip : hostname's ip address
17 |
18 |
19 | ```C++
20 | int GetIP(std::string& hostname, std::string service = "")
21 | ```
22 | Get hostname's ip address.
23 | * std::string& hostname : hostname, like "www.google.com"
24 | * std::string service : service, like "http"、"ftp"、"80"……
25 | * return : return 0 while successed.
26 |
27 | ```C++
28 | int GetIP(std::string&& hostname, std::string service = "");
29 | ```
30 | The same.
31 |
32 |
--------------------------------------------------------------------------------
/doc/en/EventLoop.md:
--------------------------------------------------------------------------------
1 | # EventLoop
2 | Event's loop,single-threaded operation.
3 |
4 | ```C++
5 | EventLoop()
6 | ```
7 | Constructor.
8 |
9 | ```C++
10 | ~EventLoop()
11 | ```
12 | Destructor.
13 |
14 | ```C++
15 | static EventLoop* DefaultLoop()
16 | ```
17 | Get singleton loop.
18 |
19 | ```C++
20 | int run()
21 | ```
22 | Blocked run loop.
23 | * return : Returns non-zero if uv_stop() was called and there are still active handles or requests. Returns zero in all other cases.
24 |
25 | ```C++
26 | int runNoWait()
27 | ```
28 | Non-blocking run loop
29 | * return : Returns zero when done (no active handles or requests left), or non-zero if more callbacks are expected (meaning you should run the event loop again sometime in the future).
30 |
31 | ```C++
32 | int stop()()
33 | ```
34 | async stop event loop. the loop will stop after end of EventLoop::run() call, not EventLoop::stop().
35 | * return : zero if success.
36 |
37 | ```C++
38 | bool isRunInLoopThread()
39 | ```
40 | Is it in the loop thread.
41 | * return true:loop's thread. false: no loop's thread.
42 |
43 | ```C++
44 | void runInThisLoop(const DefaultCallback func)
45 | ```
46 | Register callback function to loop's event, all registered callback functions will be called.
47 |
48 | * const DefaultCallback func : callback function
49 |
50 | ```C++
51 | uv_loop_t* handle()
52 | ```
53 | Get libuv's loop handle.
54 |
55 | ```C++
56 | static const char* GetErrorMessage(int status)
57 | ```
58 |
Get Error Info Message.
59 | * int status : error's code
60 |
61 |
--------------------------------------------------------------------------------
/doc/en/GlobalConfig.md:
--------------------------------------------------------------------------------
1 | # GlobalConfig
2 | Global variable and setting.
3 | ```C++
4 | static BufferMode BufferModeStatus
5 | ```
6 | Data cache mode used,NoBuffer、CycleBuffer、ListBuffer.The default is NoBuffer.
7 |
8 | ```C++
9 | static uint64_t CycleBufferSize
10 | ```
11 | Cycle buffer size,The default is 32KB.
12 |
13 | ```C++
14 | static ReadBufferPacketFunc ReadBufferPacket
15 | ```
16 | Packet data parsing and reading function,The default is Packet::readFromBuffer.
17 |
18 | ```C++
19 | static ReadBufferStringFunc ReadBufferString
20 | ```
21 | String data packet parsing and reading function,The default is Null.
22 |
23 | ```C++
24 | static ReadBufferVoidFunc ReadBufferVoid
25 | ```
26 | Customized data packet parsing and reading function,The default is Null.
27 |
--------------------------------------------------------------------------------
/doc/en/Idle.md:
--------------------------------------------------------------------------------
1 | # Idle
2 |
3 | Idle handles will run the given callback once per loop iteration.
4 |
5 | ```C++
6 | Idle(EventLoop* loop)
7 | ```
8 | Constructor.
9 | * EventLoop* loop : EventLoop's handle.
10 |
11 | ```C++
12 | virtual ~Idle()
13 | ```
14 | Destructor.
15 |
16 | ```C++
17 | void setCallback(DefaultCallback callback)
18 | ```
19 | Set the idle callback function run in eventloop.
20 | * DefaultCallback callback : callback function.
21 |
--------------------------------------------------------------------------------
/doc/en/ListBuffer.md:
--------------------------------------------------------------------------------
1 | # ListBuffer : PacketBuffer
2 | 链表数据缓存,PacketBuffer接口的一种实现。
3 | ```C++
4 | ListBuffer()
5 | ```
6 | 构造函数。
7 |
8 | ```C++
9 | virtual ~ListBuffer()
10 | ```
11 | 析构函数。
12 |
13 |
--------------------------------------------------------------------------------
/doc/en/LogWriter.md:
--------------------------------------------------------------------------------
1 | # LogWriter
2 | Log output interface.
3 |
4 | ```C++
5 | static LogWriter* Instance();
6 | ```
7 | Get single log object.
8 |
9 | ```C++
10 | static void ToHex(std::string& message, const char* data, unsigned int size);
11 | ```
12 | Byte stream to hex text.
13 | * std::string& message : output hex text.
14 | * const char* data : intput char ptr data.
15 | * unsigned int size : intput data's size.
16 |
17 |
18 | ```C++
19 | static void ToHex(std::string& message, std::string& data);
20 | ```
21 | Byte stream to hex text.
22 | * std::string& message : output hex text.
23 | * std::string& data : intput data string.
24 |
25 |
26 | ```C++
27 | void registerInterface(WriteLogCallback callback);
28 | ```
29 | Register log's writer function.
30 | * WriteLogCallback callback :log writer function,Use std::cout by default
31 | * WriteLogCallback : void(int level,const std::string& log)
32 |
33 |
34 | ```C++
35 | void setLevel(int level);
36 | ```
37 | Set log's output level.
38 | * int level : log's level.
39 |
40 |
41 | ```C++
42 | int getLevel();
43 | ```
44 | Get log's output level.
45 | * return :log's level.
46 |
47 |
48 | ```C++
49 | const std::string& getLevelName(int level);
50 | ```
51 | Get log's level name.
52 | * int level : log's level.
53 | * return : log's level,"Debug","Info","Warn","Error","Fatal"。
54 |
55 |
56 | ```C++
57 | void write(Level level, const std::string& data);
58 | ```
59 | Write log.
60 | * Level level : log's level。
61 | * const std::string& data : log's info.。
62 |
63 |
64 | ```C++
65 | void fatal(const std::string& data);
66 | ```
67 | write fatal log.
68 | * const std::string& data : log's info.
69 |
70 |
71 | ```C++
72 | void warn(const std::string& data);
73 | ```
74 | write warn log.
75 | * const std::string& data : log's info.
76 |
77 |
78 | ```C++
79 | void error(const std::string& data);
80 | ```
81 | write error log.
82 | * const std::string& data : log's info.
83 |
84 |
85 |
86 | ```C++
87 | void info(const std::string& data);
88 | ```
89 | write info log.
90 | * const std::string& data : log's info.
91 |
92 |
93 | ```C++
94 | void debug(const std::string& data);
95 | ```
96 | write debug log.
97 | * const std::string& data : log's info.
98 |
--------------------------------------------------------------------------------
/doc/en/Packet.md:
--------------------------------------------------------------------------------
1 | # Packet
2 |
Packet format:
3 | header | size|data|end|
4 | :---------:|:--------:|:--------:|:--------:|
5 | 1 byte | 2 bytes|N bytes|1 byte|
6 | ```C++
7 | Packet()
8 | ```
9 | Constructor.
10 |
11 | ```C++
12 | ~Packet()
13 | ```
14 | Destructor.
15 |
16 | ```C++
17 | void pack(const char* data, uint16_t size)
18 | ```
19 | Write data to packet.
20 | * const char* data : Data's address.
21 | * uint16_t size : Data's length.
22 |
23 | ```C++
24 | const char* getData()
25 | ```
26 | Get data's address.
27 | * return : Data's address.
28 |
29 | ```C++
30 | const uint16_t DataSize()
31 | ```
32 | Get data's length.
33 | * return : data's length.
34 |
35 | ```C++
36 | const std::string& Buffer()
37 | ```
38 | Get packet buffer address.
39 | * return : packet buffer address.
40 |
41 | ```C++
42 | const uint32_t PacketSize()
43 | ```
44 | Get packet buffer length.
45 | * return : Packet buffer length.
46 |
47 | ```C++
48 | void swap(std::string& str)
49 | ```
50 | Swap packet buffer.
51 | * std::string& str : Buffer string.
52 |
53 | ```C++
54 | static int readFromBuffer(PacketBuffer*, Packet&)
55 | ```
56 | Read packet data from PacketBuffer interface.
57 | * PacketBuffer* : PacketBuffer interface.
58 | * Packet& :Packet obj.
59 |
60 | ```C++
61 | template
62 | static void UnpackNum(const uint8_t* data, NumType& num)
63 | ```
64 | Unpack Num from data stream.
65 | * const uint8_t* data : Data stream.
66 | * NumType& num : Num variable.
67 |
68 | ```C++
69 | template
70 | static void PackNum(char* data, NumType num)
71 | ```
72 | Write a num to data stream.
73 | * char* data : Data stream.
74 | * NumType num : Num variable.
75 |
76 | ```C++
77 | static uint32_t PacketMinSize()
78 | ```
79 | Min Packet size.(header+data size+end)
80 | * return : Min Packet size.
81 |
82 |
--------------------------------------------------------------------------------
/doc/en/PacketBuffer.md:
--------------------------------------------------------------------------------
1 | # PacketBuffer
2 | Packet buffer interface.
3 | ```C++
4 | PacketBuffer()
5 | ```
6 | Constructor.
7 |
8 | ```C++
9 | virtual ~PacketBuffer()
10 | ```
11 | Destructor.
12 |
13 | ```C++
14 | virtual int append(const char* data, uint64_t size) = 0
15 | ```
16 | Write data to buffer.
17 | * const char* data : Write data address.
18 | * uint16_t size : Write data size.
19 | * return : 0 success, -1 fail.
20 |
21 | ```C++
22 | virtual int readBufferN(std::string& data, uint64_t N) = 0
23 | ```
24 | Read data from buffer,but not clear data.
25 | * std::string& data : Read data.
26 | * uint64_t N : Read data size.
27 | * return : 0 success,-1 fail.
28 |
29 | ```C++
30 | virtual int clearBufferN(uint64_t N) = 0
31 | ```
32 | Delete N bytes data from buffer.
33 | * uint64_t N : Delete data size.
34 | * return : 0 success,-1 fail.
35 |
36 | ```C++
37 | virtual int clear() = 0
38 | ```
39 | Delete all data from buffer.
40 | * return : 0 success,-1 fail.
41 |
42 | ```C++
43 | irtual uint64_t readSize() = 0
44 | ```
45 | Get current data size in buffer.
46 | * return : Readable data length.
47 |
48 | ```C++
49 | int readString(std::string& out)
50 | ```
51 | Read a string data packet from buffer.
52 | * std::string& out : String data packet.
53 |
54 | ```C++
55 | int readPacket(Packet& out)
56 | ```
57 | Read a data packet from buffer.
58 | * Packet& out : Packet obj.
59 |
60 | ```C++
61 | int readGeneric(void* out)
62 | ```
63 | Read a customized data packet from buffer.
64 | * void* out : Customized data packet pointer.
65 |
--------------------------------------------------------------------------------
/doc/en/Signal.md:
--------------------------------------------------------------------------------
1 | # Signal
2 | libuv signal.
3 |
4 | ```C++
5 | Signal(EventLoop* loop,int sig, SignalHandle handle = nullptr);
6 | ```
7 | Constructor.
8 | * EventLoop* loop : event loop's handle.
9 | * int sig : signal value.
10 | * SignalHandle handle : the signal callback,called when received.
11 |
12 |
13 | ```C++
14 | virtual ~Signal();
15 | ```
16 | Destructor.
17 |
18 |
19 | ```C++
20 | void close(DefaultCallback callback);
21 | ```
22 | Close signal event.
23 | * DefaultCallback callback : callback called after close completion.
24 |
25 |
26 |
27 | ```C++
28 | void setHandle(SignalHandle handle);
29 | ```
30 | Set singal callback.
31 | * SignalHandle handle :the signal callback.
32 |
33 |
34 | ```C++
35 | static void Ignore(int sig);
36 | ```
37 | Ignore a signal, only valid in Linux.
38 | * int sig : signal value.
39 |
40 |
--------------------------------------------------------------------------------
/doc/en/SocketAddr.md:
--------------------------------------------------------------------------------
1 | # SocketAddr
2 | IP address and port.
3 |
4 | ```C++
5 | SocketAddr(const std::string&& ip, unsigned short port, IPV ipv = Ipv4)
6 | ```
7 | Constructor.
8 | * const std::string&& ip : ip address.
9 | * unsigned short port : port num.
10 | * IPV ipv : ip version,ipv4、ipv6.
11 |
12 | ```C++
13 | SocketAddr(const std::SocketAddr& ip, unsigned short port, IPV ipv = Ipv4)
14 | ```
15 | Constructor.
16 | * const std::string& ip : ip address.
17 | * unsigned short port : port num.
18 | * IPV ipv : ip version,ipv4、ipv6.
19 |
20 | ```C++
21 | SocketAddr(const sockaddr* addr, IPV ipv = Ipv4)
22 | ```
23 | Constructor.
24 | * const sockaddr* addr : sockaddr.
25 | * IPV ipv : ip version,ipv4、ipv6.
26 |
27 | ```C++
28 | const sockaddr* Addr()
29 | ```
30 | get ip&&port sockaddr information.
31 | * return: sockaddr.
32 |
33 | ```C++
34 | void toStr(std::string& str)
35 | ```
36 | IP&&port to string,format"xxx.xxx.xxx.xxx:port".
37 | * std::string& str : return IP&&port string.
38 |
39 | ```C++
40 | std::string toStr()
41 | ```
42 | IP&&port to string,format"xxx.xxx.xxx.xxx:port".
43 | * return : IP&&port string.
44 |
45 | ```C++
46 | IPV Ipv()
47 | ```
48 | get IP version.
49 | * return : ipv4、ipv6。
50 |
51 | ```C++
52 | static void AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv = Ipv4)
53 | ```
54 | Get ip&&port string from uv_tcp.
55 | * uv_tcp_t* client : client's handle.
56 | * std::string& addrStr : Return ip&&port string.
57 | * IPV ipv : IP version,ipv4、ipv6.
58 |
59 | ```C++
60 | static uint16_t GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv = Ipv4)
61 | ```
62 | 获取ip和端口。
63 | * const sockaddr_storage* : socket handle.
64 | * std::string& out : Get ip address string.
65 | * IPV ipv : ip version,ipv4、ipv6.
66 | * return : port num.
--------------------------------------------------------------------------------
/doc/en/TcpAccepter.md:
--------------------------------------------------------------------------------
1 | # TcpAcceptor
2 | Tcp's accepter,listen and accept tcp connection.
3 |
4 | ```C++
5 | TcpAcceptor(EventLoop* loop, bool tcpNoDelay);
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 | * bool tcpNoDelay : Nagle’s Algorithm, true : no delay, false : delay.
10 |
11 | ```C++
12 | virtual TcpAcceptor()
13 | ```
14 | Destructor.
15 |
16 | ```C++
17 | int bind(SocketAddr& addr)
18 | ```
19 | Bind socket address.
20 | * SocketAddr& addr : Sokcet address.
21 | * return : Successed if return zero.
22 |
23 | ```C++
24 | int listen()
25 | ```
26 | Listen ip and port.
27 | * return : Successed if return zero.
28 |
29 | ```C++
30 | bool isListen()
31 | ```
32 | Is listen status.
33 | * return : is listen.
34 |
35 | ```C++
36 | bool isTcpNoDelay()
37 | ```
38 | Is no_delay status.
39 | * return : is no_delay.
40 |
41 | ```C++
42 | EventLoop* Loop()
43 | ```
44 | Return event loop's handle.
45 | * return : Event loop's handle.
46 |
47 | ```C++
48 | void setNewConnectionCallback( NewConnectionCallback callback)
49 | ```
50 | Set callback while new tcp's connection.
51 | * NewConnectionCallback callback : callback.
52 | * NewConnectionCallback : void(EventLoop* ,UVTcpPtr)。
53 |
--------------------------------------------------------------------------------
/doc/en/TcpClient.md:
--------------------------------------------------------------------------------
1 | # TcpClient
2 | Tcp's client.
3 |
4 | ```C++
5 | TcpClient(EventLoop* loop,bool tcpNoDelay = true);
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 | * bool tcpNoDelay : Nagle’s Algorithm, true : no delay, false : delay.
10 |
11 | ```C++
12 | virtual ~TcpClient();
13 | ```
14 | Destructor.
15 |
16 | ```C++
17 | bool isTcpNoDelay();
18 | ```
19 | Return enable No-Delay.
20 |
21 | ```C++
22 | void setTcpNoDelay(bool isNoDelay)
23 | ```
24 | set enable No-Delay.
25 | * bool isNoDelay : Enable No-Delay
26 |
27 | ```C++
28 | void connect(SocketAddr& addr)
29 | ```
30 | Connect to server.
31 | * SocketAddr& addr : Server's address.
32 |
33 | ```C++
34 | void close(std::function callback)
35 | ```
36 | Close client.
37 | * std::function callback : Callback function after closing the client.
38 |
39 | ```C++
40 | void write(const char* buf, unsigned int size, AfterWriteCallback callback = nullptr)
41 | ```
42 | Write tcp socket buffer.
43 | * const char* buf : Write data buffer.
44 | * unsigned int size : Data's length.
45 | * AfterWriteCallback callback : Callback function after writing data.
46 |
47 | ```C++
48 | void writeInLoop(const char* buf, unsigned int size, AfterWriteCallback callback)
49 | ```
50 | Write tcp buffer.Force to write in event loop,thread-safe.
51 | * const char* buf : Write data buffer.
52 | * unsigned int size : Data's length.
53 | * AfterWriteCallback callback : Callback function after writing data.
54 |
55 | ```C++
56 | void setConnectStatusCallback(ConnectStatusCallback callback);
57 | ```
58 | Set callback while client's status changed.Such as disconnect,connect success,connect fail.
59 | * ConnectStatusCallback callback : callback function.
60 | * ConnectStatusCallback : void(ConnectStatus)
61 |
62 | ```C++
63 | void setMessageCallback(NewMessageCallback callback)
64 | ```
65 | Set callback when new message.
66 | * NewMessageCallback callback : callback function.
67 | * NewMessageCallback : void(const char*,ssize_t)
68 |
69 | ```C++
70 | EventLoop* Loop();
71 | ```
72 | * return : Event loop's handle.
73 |
74 | ```C++
75 | PacketBufferPtr getCurrentBuf()
76 | ```
77 | Get packet buffer interface.
78 |
79 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/doc/en/TcpConnection.md:
--------------------------------------------------------------------------------
1 | # TcpConnection
2 | Tcp connection class.
3 |
4 | ```C++
5 | TcpConnection(EventLoop* loop,std::string& name,UVTcpPtr client,bool isConnected = true)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 | * std::string& name : Connection's name,ip+port.
10 | * UVTcpPtr client : Libuv tcp handle.
11 | * bool isConnected : Whether the connection is established.
12 |
13 | ```C++
14 | virtual ~TcpConnection()
15 | ```
16 | Destructor.
17 |
18 | ```C++
19 | void onSocketClose()
20 | ```
21 | Disconnected.
22 |
23 | ```C++
24 | void close(std::function callback)
25 | ```
26 | close connection.
27 | * std::function callback : Callback function after closing the connection.
28 |
29 | ```C++
30 | int write(const char* buf,ssize_t size,AfterWriteCallback callback)
31 | ```
32 | Write tcp buffer.
33 | * const char* buf : Write data buffer.
34 | * ssize_t size : Data's length.
35 | * AfterWriteCallback callback : Callback function after writing data.
36 | * return : 0 for success.
37 |
38 | ```C++
39 | void writeInLoop(const char* buf,ssize_t size,AfterWriteCallback callback)
40 | ```
41 | Write tcp buffer.Force to write in event loop,thread-safe.
42 | * const char* buf : Write data buffer.
43 | * ssize_t size : data's length.
44 | * AfterWriteCallback callback : Callback function after writing data.
45 |
46 | ```C++
47 | void setWrapper(std::shared_ptr conn)
48 | ```
49 | Set tcp connection's wrapper.
50 | * std::shared_ptr conn : tcp connection's wrapper.
51 |
52 | ```C++
53 | std::shared_ptr getWrapper()
54 | ```
55 | Get tcp connection's wrapper.
56 | * return : tcp connection wrapper.
57 |
58 | ```C++
59 | void setMessageCallback(OnMessageCallback callback)
60 | ```
61 | Set callback while new message.
62 | * OnMessageCallback callback : callback as new message.
63 |
64 | ```C++
65 | void setConnectCloseCallback(OnCloseCallback callback)
66 | ```
67 | Set callback while connetion closed.
68 | * OnCloseCallback callback : callback.
69 |
70 | ```C++
71 | void setConnectStatus(bool status)
72 | ```
73 | Set connection status.
74 | * bool status : connected/disconnected.
75 |
76 | ```C++
77 | bool isConnected()
78 | ```
79 | Get connection status.
80 | * return : connected/disconnected.
81 |
82 | ```C++
83 | const std::string& Name()
84 | ```
85 | Get connection's name.
86 | * return : connection's name,ip+port.
87 |
88 | ```C++
89 | PacketBufferPtr getPacketBuffer()
90 | ```
91 | Get packet buffer interface.
92 | * return : Packet buffer interface.
--------------------------------------------------------------------------------
/doc/en/Timer.md:
--------------------------------------------------------------------------------
1 | # Timer
2 | uv timer.
3 |
4 | ```C++
5 | Timer(EventLoop* loop, uint64_t timeout, uint64_t repeat, TimerCallback callback)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 | * int64_t timeout : The first timeout of milliseconds.
10 | * uint64_t repeat : Timer timeout period, do't repeat if it is zero.
11 | * TimerCallback callback : Timer callback function.
12 |
13 | ```C++
14 | virtual ~Timer()
15 | ```
16 | Destructor.
17 |
18 | ```C++
19 | void start()
20 | ```
21 | Strat timer.
22 |
23 | ```C++
24 | void close(TimerCloseComplete callback)
25 | ```
26 | Close timer.
27 | * TimerCloseComplete callback : Callback function after close completion, timer object can be safely destroyed in callback.
28 | * TimerCloseComplete : void(Timer*)。
29 |
30 | ```C++
31 | void setTimerRepeat(uint64_t ms)
32 | ```
33 | Modify the timer period.
34 | * uint64_t ms : Timer timeout period of milliseconds.
--------------------------------------------------------------------------------
/doc/en/TimerWheel.md:
--------------------------------------------------------------------------------
1 | # TimerWheel template
2 | Time wheel.
3 |
4 | ```C++
5 | TimerWheel(EventLoop* loop)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 |
10 | ```C++
11 | TimerWheel(EventLoop* loop,unsigned int timeout)
12 | ```
13 | Constructor.
14 | * EventLoop* loop : Event loop's handle.
15 | * unsigned int timeout : Timeout of seconds.
16 |
17 | ```C++
18 | void setTimeout(unsigned int seconds)
19 | ```
20 | Set timeout of seconds.
21 | * unsigned int timeout : Timeout of seconds.
22 |
23 | ```C++
24 | int getTimeout()
25 | ```
26 | Get timeout of seconds.
27 | * Return : Timeout of seconds.
28 |
29 | ```C++
30 | void start()
31 | ```
32 | Start time wheel.
33 |
34 | ```C++
35 | void insert(std::shared_ptr value)
36 | ```
37 | Insert element of time wheel.
38 | * std::shared_ptr value : Elements of the time wheel.
--------------------------------------------------------------------------------
/doc/en/Udp.md:
--------------------------------------------------------------------------------
1 | # Udp
2 | Udp class.
3 |
4 | ```C++
5 | Udp(EventLoop* loop)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop's handle.
9 |
10 | ```C++
11 | virtual ~Udp()
12 | ```
13 | Destructor.
14 |
15 | ```C++
16 | int bindAndRead(SocketAddr& addr)
17 | ```
18 | Bind address and start read data.
19 | * SocketAddr& addr : The bound address.
20 | * return : 0 on success, or an error code < 0 on failure.
21 |
22 | ```C++
23 | int send(SocketAddr& to, const char* buf, unsigned size)
24 | ```
25 | Send data.
26 | * SocketAddr& to : The sending address.
27 | * const char* buf : Sending data buffer.
28 | * unsigned size : Sending data legth.
29 | * return : 0 on success, or an error code < 0 on failure.
30 |
31 | ```C++
32 | void close(DefaultCallback callback)
33 | ```
34 | Close Udp.
35 | * DefaultCallback callback : Callback function after closing.
36 |
37 | ```C++
38 | void setMessageCallback(OnUdpMessageCallback callback)
39 | ```
40 | Set callback for new message.
41 | * OnUdpMessageCallback callback : Callback function for new messages.
--------------------------------------------------------------------------------
/doc/en/http/HttpClient.md:
--------------------------------------------------------------------------------
1 | # HttpClient
2 | Http client.
3 |
4 | ```C++
5 | enum ReqResult
6 | ```
7 | Http request result.
8 | * Success : Successed.
9 | * ConnectFail : Connect to server fail.
10 | * ParseFail : Data analysis failed.
11 | * Unknow : Unknow.
12 |
13 | ```C++
14 | HttpClient(EventLoop* loop)
15 | ```
16 | Constructor.
17 | * EventLoop* loop : Event loop's handle.
18 |
19 | ```C++
20 | virtual ~HttpClient()
21 | ```
22 | Destructor.
23 |
24 | ```C++
25 | void setOnResp(OnRespCallback callback)
26 | ```
27 | Set callback when receive response from the server.
28 | * OnRespCallback callback : Response callback.
29 | * OnRespCallback prototype : void(ReqResult, Response* )
30 |
31 | ```C++
32 | void Req(uv::SocketAddr& addr,Request& req)
33 | ``
34 | Request http.
35 | * uv::SocketAddr& addr : Sever's address.
36 | * Request& req : Http's resquest.
--------------------------------------------------------------------------------
/doc/en/http/HttpServer.md:
--------------------------------------------------------------------------------
1 | # HttpServer
2 | Http's server.
3 |
4 | ```C++
5 | HttpServer(EventLoop* loop)
6 | ```
7 | Constructor.
8 | * EventLoop* loop : Event loop句柄。
9 |
10 | ```C++
11 | void Get(std::string path, OnHttpReqCallback callback)
12 | ```
13 | Set `GET` methon callback.
14 | * std::string path : Routing path,wildcards allowed.
15 | * OnHttpReqCallback callback : Callback function.
16 |
17 | ```C++
18 | void Post(std::string path, OnHttpReqCallback callback)
19 | ```
20 | Set `POST` methon callback.
21 | * std::string path : Routing path,wildcards allowed.
22 | * OnHttpReqCallback callback : Callback function.
23 |
24 | ```C++
25 | void Head(std::string path, OnHttpReqCallback callback)
26 | ```
27 | Set `HEAD` methon callback.
28 | * std::string path : Routing path,wildcards allowed.
29 | * OnHttpReqCallback callback : Callback function.
30 |
31 | ```C++
32 | void Put(std::string path, OnHttpReqCallback callback)
33 | ```
34 | Set `PUT` methon callback.
35 | * std::string path : Routing path,wildcards allowed.
36 | * OnHttpReqCallback callback : Callback function.
37 |
38 | ```C++
39 | void Delete(std::string path, OnHttpReqCallback callback)
40 | ```
41 | Set `DELETE` methon callback.
42 | * std::string path : Routing path,wildcards allowed.
43 | * OnHttpReqCallback callback : Callback function.
44 |
45 | ```C++
46 | void Connect(std::string path, OnHttpReqCallback callback)
47 | ```
48 | Set `CONNECT` methon callback.
49 | * std::string path : Routing path,wildcards allowed.
50 | * OnHttpReqCallback callback : Callback function.
51 |
52 | ```C++
53 | void Options(std::string path, OnHttpReqCallback callback)
54 | ```
55 | Set `OPTIONS` methon callback.
56 | * std::string path : Routing path,wildcards allowed.
57 | * OnHttpReqCallback callback : Callback function.
58 |
59 | ```C++
60 | void Trace(std::string path, OnHttpReqCallback callback)
61 | ```
62 | Set `TRACE` methon callback.
63 | * std::string path : Routing path,wildcards allowed.
64 | * OnHttpReqCallback callback : Callback function.
65 |
66 | ```C++
67 | void Patch(std::string path, OnHttpReqCallback callback)
68 | ```
69 | Set `PATCH` methon callback.
70 | * std::string path : Routing path,wildcards allowed.
71 | * OnHttpReqCallback callback : Callback function.
--------------------------------------------------------------------------------
/doc/en/http/RadixTree.md:
--------------------------------------------------------------------------------
1 | # RadixTree
2 | Compact prefix tree.
3 |
4 | ```C++
5 | RadixTree()
6 | ```
7 | Constructor.
8 |
9 | ```C++
10 | virtual ~RadixTree()
11 | ```
12 | Destructor.
13 |
14 | ```C++
15 | void set(std::string& key, Type value)
16 | ```
17 | Set key and value.
18 | * std::string& key : Key
19 | * Type value : Value
20 |
21 | ```C++
22 | void set(std::string&& key, Type value)
23 | ```
24 | Set key and value with right value.
25 | * std::string& key : Key.
26 | * Type value : Value.
27 |
28 | ```C++
29 | bool get(const std::string& key, Type& value)
30 | ```
31 | Get value of key.
32 | * const std::string& key : Key.
33 | * Type& value : Get value.
34 | * return : true:success, false: key not exist.
35 |
36 | ```C++
37 | bool get(const std::string&& key, Type& value)
38 | ```
39 | Get value of key.
40 | * const std::string& key : Key.
41 | * Type& value : Get value.
42 | * return : true:success, false: key not exist.
43 |
44 | ```C++
45 | RadixTreeNodePtr Root()
46 | ```
47 | Get root node pointer, while empty then return nullptr.
48 | * return : Root node pointer.
49 |
50 | ```C++
51 | static char WildCard
52 | ```
53 | Wildcard,the default is '*'.
54 |
--------------------------------------------------------------------------------
/examples/all_example/Clinet.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2018-4-18
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #ifndef CLIENT_HPP
12 | #define CLIENT_HPP
13 |
14 |
15 | #include
16 | #include "uv/include/uv11.hpp"
17 |
18 |
19 | class Client : public uv::TcpClient
20 | {
21 | public:
22 | Client(uv::EventLoop* loop)
23 | :TcpClient(loop),
24 | sockAddr(nullptr)
25 | {
26 | setConnectStatusCallback(std::bind(&Client::onConnect,this,std::placeholders::_1));
27 | setMessageCallback(std::bind(&Client::newMessage,this,std::placeholders::_1,std::placeholders::_2));
28 | }
29 |
30 | void connectToServer(uv::SocketAddr& addr)
31 | {
32 | sockAddr = std::make_shared(addr);
33 | connect(addr);
34 | }
35 |
36 | void reConnect()
37 | {
38 | uv::Timer* timer = new uv::Timer(loop_, 500, 0, [this](uv::Timer* ptr)
39 | {
40 | connect(*(sockAddr.get()));
41 | ptr->close([](uv::Timer* ptr)
42 | {
43 | delete ptr;
44 | });
45 | });
46 | timer->start();
47 |
48 | }
49 | void onConnect(ConnectStatus status)
50 | {
51 | if(status != ConnectStatus::OnConnectSuccess)
52 | {
53 | //重连
54 | reConnect();
55 | }
56 | else
57 | {
58 | char data[] = "test message";
59 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
60 | {
61 | write(data, (int)sizeof(data));
62 | }
63 | else
64 | {
65 | uv::Packet packet;
66 | packet.pack(data, sizeof(data));
67 | write(packet.Buffer().c_str(), packet.PacketSize());
68 | }
69 | }
70 | }
71 |
72 | void newMessage(const char* buf,ssize_t size)
73 | {
74 | //write(buf, (unsigned)size, nullptr);
75 | }
76 |
77 | private:
78 | std::shared_ptr sockAddr;
79 | };
80 | #endif
81 |
--------------------------------------------------------------------------------
/examples/all_example/EchoServer.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2018-4-18
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #include
12 | #include
13 |
14 | #include "EchoServer.hpp"
15 | #include "uv/include/uv11.hpp"
16 |
17 | using namespace uv;
18 | using namespace std;
19 |
20 | EchoServer::EchoServer(EventLoop* loop)
21 | :TcpServer(loop)
22 | {
23 | setMessageCallback(std::bind(&EchoServer::newMessage,this,placeholders::_1,placeholders::_2,placeholders::_3));
24 | }
25 |
26 | void EchoServer::newMessage(shared_ptr connection,const char* buf,ssize_t size)
27 | {
28 | //不使用buffer
29 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
30 | {
31 | std::cout << "receive data :" << std::string(buf, size) << std::endl;
32 | #if 1 //直接发送
33 | connection->write(buf, size, nullptr);
34 |
35 | #else //调用write in loop接口
36 | //实质会直接调用write,并不需要memcpy。
37 | //writeInLoop需要数据在回调中释放。
38 | char* data = new char[size]();
39 | memcpy(data, buf, size);
40 | connection->writeInLoop(data, size,
41 | [this](WriteInfo& info)
42 | {
43 | //write message error.
44 | if (0 != info.status)
45 | {
46 | cout << "Write error :" << EventLoop::GetErrorMessage(info.status) << endl;
47 | }
48 | delete[] info.buf;
49 | });
50 | #endif
51 | }
52 | else //使用buffer
53 | {
54 | Packet packet;
55 | auto packetbuf = connection->getPacketBuffer();
56 | if (nullptr != packetbuf)
57 | {
58 | packetbuf->append(buf, static_cast(size));
59 | //循环读取buffer
60 | while (0 == packetbuf->readPacket(packet))
61 | {
62 | std::cout << "receive data "<< packet.DataSize()<<":" << packet.getData() << std::endl;
63 | connection->write(packet.Buffer().c_str(), packet.PacketSize(), nullptr);
64 | }
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/examples/all_example/EchoServer.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2018-4-18
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #ifndef ECHOSERVER_HPP
12 | #define ECHOSERVER_HPP
13 |
14 | #include "uv/include/uv11.hpp"
15 |
16 |
17 | class EchoServer :public uv::TcpServer
18 | {
19 | public:
20 | EchoServer(uv::EventLoop* loop);
21 | private :
22 | void newMessage(uv::TcpConnectionPtr connection,const char* buf,ssize_t size);
23 | };
24 |
25 | #endif
26 |
--------------------------------------------------------------------------------
/examples/boost_asio_echo_client/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | #include
7 | #include
8 |
9 | using namespace boost::asio;
10 |
11 | struct SocketStr
12 | {
13 | std::shared_ptr pSocket;
14 | char readBuffer[1024*32];
15 | char writeBuffer[1024*32];
16 | };
17 | using SocketPtr = std::shared_ptr;
18 |
19 | void startRead(SocketPtr ptr);
20 |
21 | void write(SocketPtr ptr,size_t size)
22 | {
23 | ptr->pSocket->async_write_some( buffer(ptr->writeBuffer, size), [ptr](const boost::system::error_code& error,std::size_t size)
24 | {
25 | startRead(ptr);
26 | });
27 | }
28 |
29 | void onRead(SocketPtr ptr,const boost::system::error_code& error,std::size_t size)
30 | {
31 | if(error)
32 | {
33 | return;
34 | }
35 | std::copy(ptr->readBuffer, ptr->readBuffer+size, ptr->writeBuffer);
36 | write(ptr,size);
37 | }
38 |
39 | void startRead(SocketPtr ptr)
40 | {
41 | auto pSocket = ptr->pSocket;
42 | auto buff = ptr->readBuffer;
43 | uint64_t size = sizeof(ptr->readBuffer);
44 | pSocket->async_read_some(buffer(buff,size),std::bind(&onRead,ptr,std::placeholders::_1,std::placeholders::_2));
45 | }
46 |
47 | void startClients(io_service& io,std::vector& sockets,ip::tcp::endpoint& endpoint, uint64_t cnt)
48 | {
49 | for(uint64_t i=0;i();
52 | sockets.push_back(ptr);
53 | ptr->pSocket = std::make_shared(io);
54 | ptr->pSocket->async_connect(endpoint, [ptr](const boost::system::error_code& error)
55 | {
56 | if(error)
57 | {
58 | std::cout<<"connect fail"< sockets;
71 | ip::tcp::endpoint endpoint( ip::address::from_string("127.0.0.1"), 10012);
72 | startClients(io,sockets,endpoint,1);
73 | io.run();
74 | }
75 |
--------------------------------------------------------------------------------
/examples/boost_asio_echo_server/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 |
8 | using namespace boost::asio;
9 |
10 | struct SocketStr
11 | {
12 | std::shared_ptr pSocket;
13 | char readBuffer[1024*32];
14 | char writeBuffer[1024*32];
15 | };
16 | using SocketPtr = std::shared_ptr;
17 |
18 | void startAccept(io_service& io,ip::tcp::acceptor& acceptor);
19 | void startRead(SocketPtr ptr);
20 | void startTimer(deadline_timer& timer);
21 |
22 | std::atomic cnt(0);
23 |
24 | void write(SocketPtr ptr,size_t size)
25 | {
26 | ptr->pSocket->async_write_some( buffer(ptr->writeBuffer, size), [ptr](const boost::system::error_code& error,std::size_t size)
27 | {
28 | startRead(ptr);
29 | });
30 | }
31 |
32 | void onRead(SocketPtr ptr,const boost::system::error_code& error,std::size_t size)
33 | {
34 | if(error)
35 | {
36 | return;
37 | }
38 | cnt += size;
39 | std::copy(ptr->readBuffer, ptr->readBuffer+size, ptr->writeBuffer);
40 | write(ptr,size);
41 | }
42 |
43 | void startRead(SocketPtr ptr)
44 | {
45 | auto pSocket = ptr->pSocket;
46 | auto buff = ptr->readBuffer;
47 | uint64_t size = sizeof(ptr->readBuffer);
48 | pSocket->async_read_some(buffer(buff,size),std::bind(&onRead,ptr,std::placeholders::_1,std::placeholders::_2));
49 | }
50 |
51 | void onNewConnection(io_service& io,ip::tcp::acceptor& acceptor, SocketPtr ptr,boost::system::error_code error)
52 | {
53 | if(error)
54 | {
55 | return;
56 | }
57 | startRead(ptr);
58 | startAccept(io,acceptor);
59 | }
60 |
61 | void startAccept(io_service& io,ip::tcp::acceptor& acceptor)
62 | {
63 | SocketPtr ptr = std::make_shared();
64 | ptr->pSocket = std::make_shared(io);
65 | acceptor.async_accept(*(ptr->pSocket), std::bind(&onNewConnection,std::ref(io),std::ref(acceptor),ptr,std::placeholders::_1));
66 | }
67 |
68 | void onTimer(deadline_timer& timer,const boost::system::error_code &ec)
69 | {
70 | std::cout << "send data:" << (cnt >> 10) << " kbyte/s" << std::endl;
71 | cnt = 0;
72 | startTimer(timer);
73 | }
74 |
75 | void startTimer(deadline_timer& timer)
76 | {
77 | timer.expires_at(timer.expires_at()+ boost::posix_time::milliseconds(1000));
78 | timer.async_wait(std::bind(&onTimer,std::ref(timer),std::placeholders::_1));
79 | }
80 |
81 | int main(int argc, char* argv[])
82 | {
83 | io_service io;
84 | ip::tcp::acceptor acceptor(io, ip::tcp::endpoint(ip::tcp::v4(), 10012));
85 | startAccept(io,acceptor);
86 |
87 | deadline_timer timer(io, boost::posix_time::milliseconds(1000));
88 | startTimer(timer);
89 |
90 | io.run();
91 | }
92 |
--------------------------------------------------------------------------------
/examples/clients_cross_thread/Client.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2018-4-18
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #include "Client.hpp"
12 |
13 |
14 | using namespace uv;
15 | using namespace std;
16 |
17 |
18 | Client::Client(uv::EventLoop* loop)
19 | :TcpClient(loop),
20 | sockAddr(nullptr)
21 | {
22 | setConnectStatusCallback(std::bind(&Client::onConnect,this,std::placeholders::_1));
23 | setMessageCallback(std::bind(&Client::newMessage,this,std::placeholders::_1,std::placeholders::_2));
24 | }
25 |
26 | void Client::connectToServer(uv::SocketAddr& addr)
27 | {
28 | sockAddr = std::make_shared(addr);
29 | connect(addr);
30 | }
31 |
32 | void Client::reConnect()
33 | {
34 | uv::Timer* timer = new uv::Timer(loop_, 500, 0, [this](uv::Timer* ptr)
35 | {
36 | connect(*(sockAddr.get()));
37 | ptr->close([](uv::Timer* ptr)
38 | {
39 | delete ptr;
40 | });
41 | });
42 | timer->start();
43 |
44 | }
45 | void Client::onConnect(ConnectStatus status)
46 | {
47 | if(status != ConnectStatus::OnConnectSuccess)
48 | {
49 | //重连
50 | reConnect();
51 | }
52 | else
53 | {
54 | char data[] = "test message";
55 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
56 | {
57 | write(data, (int)sizeof(data));
58 | }
59 | else
60 | {
61 | uv::Packet packet;
62 | packet.pack(data, sizeof(data));
63 | write(packet.Buffer().c_str(), packet.PacketSize());
64 | }
65 | }
66 | }
67 |
68 | void Client::newMessage(const char* buf,ssize_t size)
69 | {
70 | write(buf, (unsigned)size, nullptr);
71 | }
72 |
--------------------------------------------------------------------------------
/examples/clients_cross_thread/Client.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2018-4-18
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #ifndef CLIENT_HPP
12 | #define CLIENT_HPP
13 |
14 |
15 | #include
16 | #include "uv/include/uv11.hpp"
17 |
18 |
19 | class Client : public uv::TcpClient
20 | {
21 | public:
22 | Client(uv::EventLoop* loop);
23 |
24 | void connectToServer(uv::SocketAddr& addr);
25 | void reConnect();
26 | void onConnect(ConnectStatus status);
27 | void newMessage(const char* buf,ssize_t size);
28 |
29 | private:
30 | std::shared_ptr sockAddr;
31 | };
32 | #endif
33 |
--------------------------------------------------------------------------------
/examples/clients_cross_thread/main.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2019-10-20
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 |
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include "Client.hpp"
19 |
20 |
21 | using namespace uv;
22 |
23 | using ClientPtr = std::shared_ptr;
24 |
25 | struct Clients
26 | {
27 | std::vector clients ;
28 | std::mutex mutex ;
29 | std::condition_variable condition ;
30 | bool inited;
31 | };
32 |
33 | void runClients(int count,SocketAddr& server,Clients& clients)
34 | {
35 | EventLoop loop;
36 | {
37 | std::lock_guard lock(clients.mutex);
38 | for (int i=0;i(&loop);
41 | client->connectToServer(server);
42 | clients.clients.push_back(client);
43 | }
44 | clients.inited = true ;
45 | }
46 | clients.condition.notify_one();
47 | loop.run();
48 | }
49 |
50 |
51 | void runClientsCrossThread(Clients& clients)
52 | {
53 | {
54 | std::unique_lock lock(clients.mutex);
55 | clients.condition.wait(lock, [&clients] { return clients.inited; });
56 | }
57 | EventLoop loop;
58 | char data[] = "test";
59 | uv::Timer timer(&loop,1000,1000,
60 | [&clients,data](uv::Timer*)
61 | {
62 | for(auto ptr : clients.clients)
63 | {
64 | ptr->writeInLoop(data,sizeof(data),nullptr);
65 | }
66 | });
67 | timer.start();
68 | loop.run();
69 | }
70 |
71 | int main(int argc, char** args)
72 | {
73 | SocketAddr addr("127.0.0.1",10005);
74 | struct Clients clients;
75 | clients.inited = false;
76 | //开1000客户端
77 | std::thread t1(std::bind(&runClients,1000,addr,std::ref(clients)));
78 | //跨线程发送消息
79 | std::thread t2(std::bind(&runClientsCrossThread,std::ref(clients)));
80 | t1.join();
81 | t2.join();
82 | }
83 |
--------------------------------------------------------------------------------
/examples/dns/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 |
5 | void onGetIp(int status, std::string& ip)
6 | {
7 | if (status < 0)
8 | {
9 | uv::LogWriter::Instance()->error(uv::EventLoop::GetErrorMessage(status));
10 | return;
11 | }
12 | std::cout << ip << std::endl;
13 | }
14 |
15 | int main(int argc, char** args)
16 | {
17 | uv::EventLoop* loop = new uv::EventLoop();
18 |
19 | uv::DNSGet dnsGet(loop);
20 |
21 | dnsGet.setOnDNSCallback(std::bind(&onGetIp, std::placeholders::_1, std::placeholders::_2));
22 |
23 | int rst = dnsGet.GetIP("www.github.com");
24 |
25 | if (rst != 0)
26 | {
27 | uv::LogWriter::Instance()->error(uv::EventLoop::GetErrorMessage(rst));
28 | }
29 | loop->run();
30 | delete loop;
31 | }
32 |
--------------------------------------------------------------------------------
/examples/echo_client/main.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2020-4-20
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #include
12 | #include
13 |
14 | void startClients(uv::EventLoop* loop,uv::SocketAddr& addr,std::vector& clients,uint64_t cnt)
15 | {
16 | for(uint64_t i=0;i(loop);
19 | client->setConnectStatusCallback(
20 | [client](uv::TcpClient::ConnectStatus status)
21 | {
22 | if (status == uv::TcpClient::ConnectStatus::OnConnectSuccess)
23 | {
24 | char data[4096] = "hello world!";
25 | client->write(data, sizeof(data));
26 | }
27 | else
28 | {
29 | std::cout << "Error : connect to server fail" << std::endl;
30 | }
31 | });
32 | client->setMessageCallback([client](const char* data,ssize_t size)
33 | {
34 | client->write(data,(unsigned)size,nullptr);
35 | });
36 |
37 | client->connect(addr);
38 | clients.push_back(client);
39 | }
40 | }
41 |
42 | int main(int argc, char** args)
43 | {
44 | uv::EventLoop* loop = uv::EventLoop::DefaultLoop();
45 |
46 | uv::SocketAddr serverAddr("127.0.0.1", 10005, uv::SocketAddr::Ipv4);
47 | std::vector clients;
48 | startClients(loop,serverAddr,clients,1);
49 |
50 | loop->run();
51 | }
52 |
--------------------------------------------------------------------------------
/examples/echo_server/main.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
3 |
4 | Author: orcaer@yeah.net
5 |
6 | Last modified: 2019-10-20
7 |
8 | Description: https://github.com/wlgq2/uv-cpp
9 | */
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | using namespace uv;
16 |
17 |
18 | int main(int argc, char** args)
19 | {
20 | EventLoop* loop = EventLoop::DefaultLoop();
21 |
22 | SocketAddr addr("0.0.0.0", 10005, SocketAddr::Ipv4);
23 |
24 | std::atomic dataSize(0);
25 | uv::TcpServer server(loop);
26 | server.setMessageCallback([&dataSize](uv::TcpConnectionPtr ptr,const char* data, ssize_t size)
27 | {
28 | dataSize += size;
29 | ptr->write(data, size, nullptr);
30 | });
31 | //心跳超时
32 | //server.setTimeout(15);
33 | server.bindAndListen(addr);
34 |
35 | uv::Timer timer(loop, 1000, 1000, [&dataSize](uv::Timer* ptr)
36 | {
37 | std::cout << "send data:" << (dataSize >> 10) << " kbyte/s" << std::endl;
38 | dataSize = 0;
39 | });
40 | timer.start();
41 | loop->run();
42 | }
43 |
--------------------------------------------------------------------------------
/examples/helloworld/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 |
5 | int main(int argc, char** args)
6 | {
7 | //event's loop
8 | //uv::EventLoop* loop = new uv::EventLoop();
9 | //or
10 | uv::EventLoop* loop = uv::EventLoop::DefaultLoop();
11 |
12 | uv::SocketAddr serverAddr("127.0.0.1", 10000, uv::SocketAddr::Ipv4);
13 | //Tcp Server
14 | uv::TcpServer server(loop);
15 | server.setMessageCallback(
16 | [](uv::TcpConnectionPtr conn, const char* data , ssize_t size)
17 | {
18 | std::cout << std::string(data, size) << std::endl;
19 | std::string str("hex :");
20 | uv::LogWriter::ToHex(str, data, (unsigned int)size);
21 | std::cout << str << std::endl;
22 | conn->write(data, size,nullptr);
23 | });
24 | server.bindAndListen(serverAddr);
25 |
26 |
27 | //Tcp Client
28 | uv::TcpClient client(loop);
29 | client.setConnectStatusCallback(
30 | [&client](uv::TcpClient::ConnectStatus status)
31 | {
32 | if (status == uv::TcpClient::ConnectStatus::OnConnectSuccess)
33 | {
34 | char data[] = "hello world!";
35 | client.write(data, sizeof(data));
36 | }
37 | else
38 | {
39 | std::cout << "Error : connect to server fail" << std::endl;
40 | }
41 | });
42 | client.connect(serverAddr);
43 |
44 | loop->run();
45 | }
46 |
--------------------------------------------------------------------------------
/examples/http_client/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | void onHttpResp(uv::http::HttpClient* client,int error, uv::http::Response* resp)
6 | {
7 | if (error == uv::http::HttpClient::ConnectFail)
8 | {
9 | //连接服务失败
10 | uv::LogWriter::Instance()->error("connect server fail.");
11 | return;
12 | }
13 | if (error == uv::http::HttpClient::ParseFail)
14 | {
15 | //解析response出错
16 | uv::LogWriter::Instance()->error("parse http response fail.");
17 | return;
18 | }
19 | std::ofstream outfile;
20 | outfile.open("test.html", std::ios_base::out);
21 | outfile << resp->getContent();
22 | outfile.close();
23 | std::cout << "write test.htmt success" << std::endl;
24 | //close connection.
25 | delete client;
26 | }
27 |
28 |
29 | void onGetIp(uv::EventLoop* loop ,int status, std::string& ip)
30 | {
31 | if (status < 0)
32 | {
33 | uv::LogWriter::Instance()->error(uv::EventLoop::GetErrorMessage(status));
34 | return;
35 | }
36 | uv::LogWriter::Instance()->debug(std::string("get ip:")+ ip);
37 | //服务器ip地址
38 | uv::SocketAddr addr(ip, 80);
39 | uv::http::HttpClient* client = new uv::http::HttpClient(loop);
40 | client->setOnResp(std::bind(&onHttpResp, client,
41 | std::placeholders::_1,std::placeholders::_2));
42 |
43 | uv::http::Request req;
44 | req.setPath("/search");
45 | //url参数
46 | req.appendUrlParam("q", "uv-cpp");
47 | //消息头列表
48 | req.appendHead("Host", "cn.bing.com");
49 | req.appendHead("Connection", "keep - alive");
50 | req.appendHead("Accept-Language", "en");
51 |
52 |
53 | //请求http服务
54 | client->Req(addr,req);
55 | }
56 |
57 | int main(int argc, char** args)
58 | {
59 | uv::EventLoop loop;
60 |
61 | //查询DNS。
62 | uv::DNSGet dnsGet(&loop);
63 | dnsGet.setOnDNSCallback(std::bind(&onGetIp, &loop, std::placeholders::_1, std::placeholders::_2));
64 |
65 | int rst = dnsGet.GetIP("cn.bing.com","80");
66 |
67 | if (rst != 0)
68 | {
69 | uv::LogWriter::Instance()->error(uv::EventLoop::GetErrorMessage(rst));
70 | }
71 |
72 | loop.run();
73 | }
74 |
--------------------------------------------------------------------------------
/examples/loop_exit/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | void func(uv::EventLoop* loop)
5 | {
6 | std::cout << "in func thread id:" << std::this_thread::get_id() << std::endl;
7 | loop->runInThisLoop([loop]()
8 | {
9 | std::cout << "stop in loop thread id:" << std::this_thread::get_id() << std::endl;
10 | loop->stop();
11 | });
12 | }
13 |
14 | void loop_exit()
15 | {
16 | uv::EventLoop* loop = new uv::EventLoop();
17 | std::thread t1(std::bind(&func, loop));
18 | loop->run();
19 | loop->runInThisLoop([]()
20 | {
21 | std::cout << "loop is stop" << std::endl;
22 | });
23 | delete loop;
24 | t1.join();
25 | std::cout << "delete loop" << loop << std::endl << std::endl;;
26 | }
27 | int main(int argc, char** args)
28 | {
29 | while (true)
30 | {
31 | std::thread t1(std::bind(&loop_exit));
32 | t1.join();
33 | std::this_thread::sleep_for(std::chrono::milliseconds(1000));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/examples/pingpang/Clinet.hpp:
--------------------------------------------------------------------------------
1 | #ifndef CLIENT_HPP
2 | #define CLIENT_HPP
3 |
4 |
5 | #include
6 | #include
7 |
8 | class Client : public uv::TcpClient
9 | {
10 | public:
11 | Client(uv::EventLoop* loop)
12 | :TcpClient(loop),
13 | sockAddr(nullptr)
14 | {
15 | setConnectStatusCallback(std::bind(&Client::onConnect,this,std::placeholders::_1));
16 | setMessageCallback(std::bind(&Client::newMessage,this,std::placeholders::_1,std::placeholders::_2));
17 | }
18 |
19 | void connectToServer(uv::SocketAddr& addr)
20 | {
21 | sockAddr = std::make_shared(addr);
22 | connect(addr);
23 | }
24 |
25 | void reConnect()
26 | {
27 | uv::Timer* timer = new uv::Timer(Loop(), 1000, 0, [this](uv::Timer* timer)
28 | {
29 | connect(*(sockAddr.get()));
30 | timer->close([](uv::Timer* handle)
31 | {
32 | delete handle;
33 | });
34 | });
35 | timer->start();
36 | }
37 | void sendTestMessage()
38 | {
39 | char data[1024] = "test message";
40 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
41 | {
42 | write(data, (int)sizeof(data));
43 | }
44 | else
45 | {
46 | uv::Packet packet;
47 | packet.pack(data, sizeof(data));
48 | write(packet.Buffer().c_str(), packet.PacketSize());
49 | }
50 | }
51 | void onConnect(TcpClient::ConnectStatus status)
52 | {
53 | if (status != TcpClient::ConnectStatus::OnConnectSuccess)
54 | {
55 | reConnect();
56 | }
57 | else
58 | {
59 | sendTestMessage();
60 | }
61 | }
62 |
63 |
64 | void newMessage(const char* buf,ssize_t size)
65 | {
66 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
67 | {
68 | write(buf, (unsigned int)size);
69 | }
70 | else //使用buffer
71 | {
72 | auto packetbuf = getCurrentBuf();
73 | if (nullptr != packetbuf)
74 | {
75 | packetbuf->append(buf, static_cast(size));
76 | uv::Packet packet;
77 | while (0 == packetbuf->readPacket(packet))
78 | {
79 | write(packet.Buffer().c_str(), (unsigned)packet.PacketSize(), nullptr);
80 | }
81 | }
82 | }
83 | }
84 |
85 | private:
86 | std::shared_ptr sockAddr;
87 | };
88 | #endif
89 |
--------------------------------------------------------------------------------
/examples/pingpang/EchoServer.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "EchoServer.hpp"
5 |
6 |
7 | using namespace uv;
8 | using namespace std;
9 |
10 | EchoServer::EchoServer(EventLoop* loop)
11 | :TcpServer(loop)
12 | {
13 | cnt = 0;
14 | setMessageCallback(std::bind(&EchoServer::newMessage,this,placeholders::_1,placeholders::_2,placeholders::_3));
15 | }
16 |
17 | void EchoServer::newMessage(shared_ptr connection, const char* buf, ssize_t size)
18 | {
19 | cnt++;
20 | //不使用buffer
21 | if (uv::GlobalConfig::BufferModeStatus == uv::GlobalConfig::NoBuffer)
22 | {
23 | connection->write(buf, size, nullptr);
24 | }
25 | else //使用buffer
26 | {
27 | auto packetbuf = connection->getPacketBuffer();
28 | if (nullptr != packetbuf)
29 | {
30 | packetbuf->append(buf, static_cast(size));
31 | //循环读取buffer
32 | Packet packet;
33 | while (0 == packetbuf->readPacket(packet))
34 | {
35 | connection->write(packet.Buffer().c_str(), packet.PacketSize(), nullptr);
36 | }
37 | }
38 | }
39 |
40 | }
41 |
42 | uint64_t EchoServer::Cnt()
43 | {
44 | return cnt;
45 | }
46 |
47 | void EchoServer::clearCnt()
48 | {
49 | cnt =0;
50 | }
51 |
--------------------------------------------------------------------------------
/examples/pingpang/EchoServer.hpp:
--------------------------------------------------------------------------------
1 | #ifndef ECHOSERVER_HPP
2 | #define ECHOSERVER_HPP
3 |
4 | #include
5 | #include
6 |
7 | class EchoServer :public uv::TcpServer
8 | {
9 | public:
10 | EchoServer(uv::EventLoop* loop);
11 |
12 | uint64_t Cnt();
13 | void clearCnt();
14 | private :
15 | std::atomic cnt;
16 | void newMessage(uv::TcpConnectionPtr connection,const char* buf,ssize_t size);
17 | };
18 |
19 | #endif
20 |
--------------------------------------------------------------------------------
/examples/pingpang/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 |
4 | #include "EchoServer.hpp"
5 | #include "Clinet.hpp"
6 |
7 |
8 | using namespace uv;
9 |
10 |
11 |
12 | int main(int argc, char** args)
13 | {
14 | //定义事件分发器类
15 | EventLoop* loop = EventLoop::DefaultLoop();
16 |
17 | uv::GlobalConfig::BufferModeStatus = uv::GlobalConfig::CycleBuffer;
18 | SocketAddr addr1("0.0.0.0", 10002, SocketAddr::Ipv4);
19 |
20 |
21 | EchoServer server(loop);
22 | server.setTimeout(60);
23 | server.bindAndListen(addr1);
24 |
25 | SocketAddr addr2("127.0.0.1", 10002);
26 |
27 | Client client(loop);
28 | client.connectToServer(addr2);
29 |
30 |
31 | Timer timer(loop, 1000, 1000,
32 | [&server](Timer*)
33 | {
34 | std::cout << "cnt:"<run();
41 | delete loop;
42 | }
43 |
--------------------------------------------------------------------------------
/examples/radix_tree/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include