├── .gitattributes ├── CMakeLists.txt ├── LICENSE ├── README.md ├── build_cmake.sh ├── examples ├── README.md ├── errorhandle │ └── errorhandle.cpp ├── helloworld │ └── helloworld.cpp ├── messageTypeRegister │ ├── MessageType.h │ └── main.cpp ├── pingpong │ ├── MessageType.h │ └── main.cpp ├── remoteActorMessage │ ├── MessageType.h │ └── main.cpp └── sendByName │ └── SendByName.cpp ├── project ├── codeblock_linux │ ├── MessageTypeRegister │ │ └── messagetyperegister.cbp │ ├── RemoteActorMessage │ │ └── remoteactormessage.cbp │ ├── SendByName │ │ └── sendbyname.cbp │ ├── errorhandle │ │ └── errorhanlde.cbp │ ├── helloworld │ │ └── helloworld.cbp │ └── pingpong │ │ └── pingpong.cbp └── vs2017 │ ├── MessageTypeRegister │ ├── MessageTypeRegister.vcxproj │ ├── MessageTypeRegister.vcxproj.filters │ └── MessageTypeRegister.vcxproj.user │ ├── RemoteActorMessage │ ├── RemoteActorMessage.vcxproj │ ├── RemoteActorMessage.vcxproj.filters │ └── RemoteActorMessage.vcxproj.user │ ├── SendByName │ ├── SendByName.vcxproj │ ├── SendByName.vcxproj.filters │ └── SendByName.vcxproj.user │ ├── errorhandle │ ├── errorhandle.vcxproj │ ├── errorhandle.vcxproj.filters │ └── errorhandle.vcxproj.user │ ├── helloworld │ ├── helloworld.vcxproj │ ├── helloworld.vcxproj.filters │ └── helloworld.vcxproj.user │ ├── orca.sln │ └── pingpong │ ├── pingpong.vcxproj │ ├── pingpong.vcxproj.filters │ └── pingpong.vcxproj.user ├── src └── orca │ ├── base │ ├── SpinLock │ │ ├── SpinLock.cpp │ │ └── SpinLock.h │ ├── condition │ │ ├── Condition.cpp │ │ └── Condition.h │ ├── error │ │ ├── ErrorHandle.h │ │ ├── ErrorInfo.cpp │ │ └── ErrorInfo.h │ ├── queue │ │ └── BlockQueue.h │ └── thread │ │ ├── Thread.cpp │ │ ├── Thread.h │ │ ├── ThreadPool.cpp │ │ └── ThreadPool.h │ ├── core │ ├── Actor.h │ ├── Address.h │ ├── Assert.h │ ├── Define.h │ ├── EndPoint.h │ ├── Framework.h │ ├── Mail.h │ ├── MailBoxThread.cpp │ ├── MailBoxThread.h │ ├── Mailbox.h │ ├── MailboxCenter.h │ ├── MailboxPage.h │ ├── MessagePack.h │ ├── MessageTraits.h │ ├── RemoteMail.h │ └── net │ │ ├── ActorClient.cpp │ │ ├── ActorClient.h │ │ ├── ActorServer.cpp │ │ ├── ActorServer.h │ │ ├── Protocol.h │ │ ├── TcpPacket.cpp │ │ └── TcpPacket.h │ └── orca.h └── uv-cpp ├── .gitattributes ├── 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 └── 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 ├── examples ├── all_example │ ├── Clinet.h │ ├── EchoServer.cpp │ ├── EchoServer.h │ └── main.cpp ├── boost_asio_echo_client │ └── main.cpp ├── boost_asio_echo_server │ └── main.cpp ├── clients_cross_thread │ ├── Client.cpp │ ├── Client.h │ └── main.cpp ├── dns │ └── main.cpp ├── echo_client │ └── main.cpp ├── echo_server │ └── main.cpp ├── helloworld │ └── main.cpp ├── http_client │ └── main.cpp ├── http_server │ └── main.cpp ├── pingpang │ ├── Clinet.h │ ├── EchoServer.cpp │ ├── EchoServer.h │ └── main.cpp ├── radix_tree │ └── main.cpp └── rpc │ ├── RpcClient.cpp │ ├── RpcClient.h │ ├── RpcCtrl.cpp │ ├── RpcCtrl.h │ ├── RpcServer.cpp │ ├── RpcServer.h │ ├── main.cpp │ └── protocol.h ├── 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 │ ├── helloworld │ │ └── helloworld.cbp │ ├── http_client │ │ └── http_client.cbp │ ├── http_server │ │ └── http_server.cbp │ ├── pingpong │ │ └── pingpong.cbp │ ├── radix_tree │ │ └── radix_tree.cbp │ └── rpc │ │ └── rpc.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 │ ├── create_lib │ ├── create_lib.vcxproj │ ├── create_lib.vcxproj.filters │ └── create_lib.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 │ ├── 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 │ └── uv-cpp.sln └── uv ├── Async.cpp ├── CycleBuffer.cpp ├── DnsGet.cpp ├── EventLoop.cpp ├── GlobalConfig.cpp ├── Idle.cpp ├── ListBuffer.cpp ├── LogWriter.cpp ├── Packet.cpp ├── Signal.cpp ├── SocketAddr.cpp ├── TcpAccepter.cpp ├── TcpClient.cpp ├── TcpConnection.cpp ├── TcpServer.cpp ├── Timer.cpp ├── Udp.cpp ├── http ├── HttpClient.cpp ├── HttpCommon.cpp ├── HttpServer.cpp ├── Request.cpp └── Response.cpp └── include ├── Async.h ├── CycleBuffer.h ├── DnsGet.h ├── EventLoop.h ├── GlobalConfig.h ├── Idle.h ├── ListBuffer.h ├── LogWriter.h ├── Packet.h ├── PacketBuffer.h ├── Signal.h ├── SocketAddr.h ├── TcpAccepter.h ├── TcpClient.h ├── TcpConnection.h ├── TcpServer.h ├── Timer.h ├── TimerWheel.h ├── Udp.h ├── http ├── HttpClient.h ├── HttpCommon.h ├── HttpServer.h ├── RadixTree.h ├── Request.h └── Response.h └── uv11.h /.gitattributes: -------------------------------------------------------------------------------- 1 | *.h linguist-language=C++ -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.8) 2 | 3 | PROJECT(orca) 4 | SET(VERSION 1.0.0) 5 | 6 | 7 | SET(LIB_DIRECTORY 8 | ${PROJECT_SOURCE_DIR}/uv-cpp/libuv1.30.0/lib/gcc8.3.0/) 9 | 10 | SET(SOURCE_DIRECTORY 11 | ${PROJECT_SOURCE_DIR}/uv-cpp/uv/ 12 | ${PROJECT_SOURCE_DIR}/uv-cpp/uv/http/ 13 | ${PROJECT_SOURCE_DIR}/src/orca/base/condition/ 14 | ${PROJECT_SOURCE_DIR}/src/orca/base/error/ 15 | ${PROJECT_SOURCE_DIR}/src/orca/base/queue/ 16 | ${PROJECT_SOURCE_DIR}/src/orca/base/SpinLock/ 17 | ${PROJECT_SOURCE_DIR}/src/orca/base/thread/ 18 | ${PROJECT_SOURCE_DIR}/src/orca/core/ 19 | ${PROJECT_SOURCE_DIR}/src/orca/core/net/ 20 | ) 21 | 22 | SET(INCLUDE_DIRECTORY 23 | ${PROJECT_SOURCE_DIR}/uv-cpp/libuv1.30.0/include/ 24 | ${PROJECT_SOURCE_DIR}/ 25 | ) 26 | 27 | SET(OUTPUT_DIR 28 | "${PROJECT_SOURCE_DIR}/orca_lib") 29 | 30 | 31 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -m64 -O2") 32 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -O2") 33 | 34 | 35 | INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORY}) 36 | 37 | FOREACH(SOURCE_DIR ${SOURCE_DIRECTORY}) 38 | AUX_SOURCE_DIRECTORY(${SOURCE_DIR} SOURCES) 39 | ENDFOREACH() 40 | 41 | SET(OUTPUT_SHARED "${PROJECT_NAME}_shared") 42 | SET(OUTPUT_STATIC "${PROJECT_NAME}_static") 43 | 44 | LINK_DIRECTORIES(LIB_DIRECTORY) 45 | TARGET_LINK_LIBRARIES(${PROJECT_SHARED} "libuv.a") 46 | TARGET_LINK_LIBRARIES(${PROJECT_STATIC} "libuv.a") 47 | 48 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/orca_lib") 49 | 50 | ADD_LIBRARY (${OUTPUT_SHARED} SHARED ${SOURCES} ) 51 | ADD_LIBRARY (${OUTPUT_STATIC} STATIC ${SOURCES} ) 52 | 53 | #SET_TARGET_PROPERTIES(${OUTPUT_STATIC} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}" ) 54 | 55 | #SET_TARGET_PROPERTIES(${OUTPUT_SHARED} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}" ) 56 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # orca 2 | A actor framework base on c++11/14. 3 |
一个C++11/14风格的actor库。
4 |
接口上参考过Theron库,实现上避开了Theron相关不合理实现及一些可复现的bug,ping-pong测试性能优于Theron和CAF。
5 | ## Dependent 6 | 网络部分基于libuv,项目中提供libuv1.22.0 gcc5.5.0及visual studio 2017编译的版本,如需其他版本则需要自己编译libuv。 7 |
C++封装了一层libuv:https://github.com/wlgq2/uv-cpp
8 | ## Build 9 | Linux:提供codebolcks工程与cmake编译环境; 10 |
Windows:通过Visual Studios2017编译工程。
11 | ## Testing 12 | 本地消息测试: 13 | 14 | local message | Theron|CAF|orca| 15 | :---------:|:--------:|:--------:|:--------:| 16 | 1K字节(次/秒) | 3707 753.8 |113 343.2|3036 779.6| 17 | 4K字节(次/秒) | 2108 082.8 |116 756.4|3089 924.6| 18 | 8K字节(次/秒) | 1800 637.2 |111 539|3108 350.2| 19 | ``` 20 | 注:Theron本地消息性能远高于CAF,orca本地消息实现zore-copy,所以只在1K字节以下性能略低于Theron。 21 | ``` 22 |
远程消息测试:
23 | 24 | remote message | Theron|CAF|orca| 25 | :---------:|:--------:|:--------:|:--------:| 26 | 1K字节(次/秒) | 250 |5508.8|19148.4| 27 | 2K字节(次/秒) | 250 |5426.4|17813.8| 28 | 4K字节(次/秒) | 250 |5028|16058.2| 29 | 30 | ``` 31 | 注:Theron的网络部分用非阻塞+轮询实现,所以性能很差。 32 | ``` 33 | ## How To Use 34 | * 阅读Wiki: https://github.com/wlgq2/orca/wiki 35 | * 使用简介: https://segmentfault.com/blog/wlgq2 36 | -------------------------------------------------------------------------------- /build_cmake.sh: -------------------------------------------------------------------------------- 1 | rm -rf build_cmake 2 | rm -rf orca_lib 3 | mkdir orca_lib 4 | mkdir build_cmake 5 | cd build_cmake 6 | cmake .. 7 | make 8 | cp liborca_static.a ../orca_lib/ 9 | cp liborca_shared.so ../orca_lib/ 10 | cd .. 11 | #cp -rf src/orca orca_lib/include/orca 12 | #find orca_lib/include -name *.cpp | xargs rm -f 13 | 14 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # tutorial 2 | ** ** 3 | * `Helloworld`:基本发送/接收本地消息操作。 4 | * `SendByName`:通过Actor Name发送消息。 5 | * `MessageTypeRegister`:注册自定义消息类型(需要实现相应接口)。 6 | * `Errorhandle`:注册错误回调处理操作。 7 | * `pingpang`:消息ping-pong操作。 8 | * `RemoteActorMessage`:网络消息通信。 9 | -------------------------------------------------------------------------------- /examples/errorhandle/errorhandle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | REGISTER_MESSAGE_TYPE(std::string); 6 | 7 | void errorHandle(orca::base::ErrorInfo info) 8 | { 9 | std::cout << "error id : " << info.getErrorId() << std::endl; 10 | std::cout << "error message: " << info.getErrorInfo() << std::endl; 11 | } 12 | 13 | int main(int argc, char** args) 14 | { 15 | //actor framework. 16 | orca::Framework framework; 17 | 18 | framework.RegisterErrorHandle(std::bind(&errorHandle,std::placeholders::_1)); 19 | 20 | orca::base::ErrorHandle::Instance()->error(orca::base::ErrorInfo::UndefinedError,"undefine error"); 21 | framework.loop(); 22 | 23 | } -------------------------------------------------------------------------------- /examples/helloworld/helloworld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | REGISTER_MESSAGE_TYPE(std::string); 6 | 7 | class ActorTest :public orca::Actor 8 | { 9 | public: 10 | ActorTest(orca::Framework* framework,std::string name = "") 11 | :Actor(framework, name) 12 | { 13 | registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2)); 14 | } 15 | void handle(orca::MessagePack& pack, orca::Address& from) 16 | { 17 | std::cout << (char*)(pack.enter()) << std::endl; 18 | } 19 | }; 20 | 21 | int main(int argc, char** args) 22 | { 23 | //actor framework. 24 | orca::Framework framework; 25 | //arctor object. 26 | ActorTest actor1(&framework); 27 | ActorTest actor2(&framework); 28 | //message pack. 29 | orca::MessagePack message; 30 | message.create("hello world!"); 31 | 32 | //actor1->actor2 send message. 33 | actor1.send(message,actor2.getAddress()); 34 | framework.loop(); 35 | 36 | } -------------------------------------------------------------------------------- /examples/messageTypeRegister/MessageType.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/examples/messageTypeRegister/MessageType.h -------------------------------------------------------------------------------- /examples/messageTypeRegister/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "MessageType.h" 3 | 4 | class ActorTest :public orca::Actor 5 | { 6 | public: 7 | ActorTest(orca::Framework* framework,std::string name = "") 8 | :Actor(framework, name) 9 | { 10 | registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2)); 11 | } 12 | void handle(orca::MessagePack& pack, orca::Address& from) 13 | { 14 | std::cout << (char*)(pack.enter()) << std::endl; 15 | } 16 | }; 17 | 18 | int main(int argc, char** args) 19 | { 20 | //actor framework. 21 | orca::Framework framework; 22 | //arctor object. 23 | ActorTest actor1(&framework); 24 | ActorTest actor2(&framework); 25 | //message pack. 26 | char data[] = "a message of my customize type"; 27 | orca::MessagePack message; 28 | message.create(data,(int)sizeof(data)); 29 | 30 | //actor1->actor2 send message. 31 | actor1.send(message,actor2.getAddress()); 32 | framework.loop(); 33 | 34 | } -------------------------------------------------------------------------------- /examples/pingpong/MessageType.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/examples/pingpong/MessageType.h -------------------------------------------------------------------------------- /examples/pingpong/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "MessageType.h" 6 | 7 | class ActorTest :public orca::Actor 8 | { 9 | public: 10 | ActorTest(orca::Framework* framework,std::string name = "") 11 | :Actor(framework, name) 12 | { 13 | clearCnt(); 14 | registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2)); 15 | } 16 | void handle(orca::MessagePack& pack, orca::Address& from) 17 | { 18 | cnt_++; 19 | send(pack, from); 20 | } 21 | void clearCnt() 22 | { 23 | cnt_ = 0; 24 | } 25 | uint64_t getCnt() 26 | { 27 | return cnt_; 28 | } 29 | private: 30 | std::atomic cnt_; 31 | }; 32 | 33 | int main(int argc, char** args) 34 | { 35 | //actor framework. 36 | orca::Framework framework; 37 | //arctor object. 38 | ActorTest actor1(&framework); 39 | ActorTest actor2(&framework); 40 | //message pack. 41 | char data[1024] = "1k ping-pang message"; 42 | orca::MessagePack message; 43 | message.create(data,(int)sizeof(data)); 44 | 45 | //actor1->actor2 send message. 46 | actor1.send(message,actor2.getAddress()); 47 | 48 | //new thread count. 49 | std::thread t1([&actor2]() 50 | { 51 | while (true) 52 | { 53 | orca::base::Thread::SleepMSeconds(1000); 54 | std::cout << "count: " << actor2.getCnt() << std::endl; 55 | actor2.clearCnt(); 56 | } 57 | }); 58 | framework.loop(); 59 | 60 | } -------------------------------------------------------------------------------- /examples/remoteActorMessage/MessageType.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/examples/remoteActorMessage/MessageType.h -------------------------------------------------------------------------------- /examples/remoteActorMessage/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/examples/remoteActorMessage/main.cpp -------------------------------------------------------------------------------- /examples/sendByName/SendByName.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | REGISTER_MESSAGE_TYPE(std::string); 6 | 7 | class ActorTest :public orca::Actor 8 | { 9 | public: 10 | ActorTest(orca::Framework* framework,std::string name = "") 11 | :Actor(framework, name) 12 | { 13 | registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2)); 14 | } 15 | void handle(orca::MessagePack& pack, orca::Address& from) 16 | { 17 | std::cout << (char*)(pack.enter()) << std::endl; 18 | } 19 | }; 20 | 21 | int main(int argc, char** args) 22 | { 23 | //actor framework. 24 | orca::Framework framework; 25 | //arctor object. 26 | ActorTest actor1(&framework); 27 | ActorTest actor2(&framework,"actor02"); 28 | //message pack. 29 | orca::MessagePack message; 30 | message.create("a message to actor2"); 31 | 32 | //actor1->actor2 send message. 33 | actor1.send(message,"actor02"); 34 | framework.loop(); 35 | 36 | } -------------------------------------------------------------------------------- /project/vs2017/MessageTypeRegister/MessageTypeRegister.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /project/vs2017/RemoteActorMessage/RemoteActorMessage.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /project/vs2017/SendByName/SendByName.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /project/vs2017/errorhandle/errorhandle.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /project/vs2017/helloworld/helloworld.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /project/vs2017/pingpong/pingpong.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/orca/base/SpinLock/SpinLock.cpp: -------------------------------------------------------------------------------- 1 | #include "SpinLock.h" 2 | 3 | #include 4 | using namespace orca::base; 5 | 6 | SpinLock::SpinLock() 7 | :locked_(Unlocked) 8 | { 9 | } 10 | 11 | void SpinLock::lock() 12 | { 13 | while (true) 14 | { 15 | int old(Unlocked); 16 | if (locked_.compare_exchange_strong(old, Locked)) 17 | { 18 | return; 19 | } 20 | std::this_thread::yield(); 21 | } 22 | } 23 | 24 | void SpinLock::unlock() 25 | { 26 | locked_.store(Unlocked); 27 | } 28 | -------------------------------------------------------------------------------- /src/orca/base/SpinLock/SpinLock.h: -------------------------------------------------------------------------------- 1 | #ifndef SPIN_LOCK_H 2 | #define SPIN_LOCK_H 3 | 4 | #include 5 | namespace orca 6 | { 7 | namespace base 8 | { 9 | 10 | class SpinLock 11 | { 12 | public: 13 | enum 14 | { 15 | Unlocked, 16 | Locked 17 | }; 18 | 19 | SpinLock(); 20 | void lock(); 21 | void unlock(); 22 | private: 23 | std::atomic locked_; 24 | }; 25 | } 26 | } 27 | #endif // ! SPIN_LOCK_H 28 | 29 | -------------------------------------------------------------------------------- /src/orca/base/condition/Condition.cpp: -------------------------------------------------------------------------------- 1 | #include "Condition.h" 2 | 3 | void orca::base::Condition::notifyAll() 4 | { 5 | std::unique_lock lock(mutex_); 6 | condition_.notify_all(); 7 | } 8 | 9 | void orca::base::Condition::notify() 10 | { 11 | std::unique_lock lock(mutex_); 12 | condition_.notify_one(); 13 | } 14 | 15 | void orca::base::Condition::wait(ConditionCallback callback) 16 | { 17 | std::unique_lock lock(mutex_); 18 | condition_.wait(lock, callback); 19 | } 20 | -------------------------------------------------------------------------------- /src/orca/base/condition/Condition.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_CONDITION_H 2 | #define ORCA_CONDITION_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace orca 9 | { 10 | namespace base 11 | { 12 | 13 | class Condition 14 | { 15 | public: 16 | using ConditionCallback = std::function; 17 | 18 | void notifyAll(); 19 | void notify(); 20 | void wait(ConditionCallback callback); 21 | private: 22 | std::mutex mutex_; 23 | std::condition_variable condition_; 24 | }; 25 | } 26 | } 27 | #endif -------------------------------------------------------------------------------- /src/orca/base/error/ErrorHandle.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_BASE_ERROR_HANDLE 2 | #define ORCA_BASE_ERROR_HANDLE 3 | 4 | #include 5 | #include 6 | #include 7 | #include "ErrorInfo.h" 8 | 9 | 10 | namespace orca 11 | { 12 | 13 | namespace base 14 | { 15 | 16 | class ErrorHandle 17 | { 18 | public: 19 | using ErrorHandleFunction = std::function; 20 | 21 | public: 22 | static ErrorHandle* Instance() 23 | { 24 | static ErrorHandle errorHandle; 25 | return &errorHandle; 26 | } 27 | 28 | void bind(ErrorHandleFunction func) 29 | { 30 | handle_ = func; 31 | } 32 | void error(ErrorInfo info) 33 | { 34 | if (handle_) 35 | handle_(info); 36 | else 37 | std::cerr << "error id "<< info.getErrorId() << ":" << info.getErrorInfo() << std::endl; 38 | } 39 | void error(ErrorInfo::ErrorId id, std::string&& info) 40 | { 41 | ErrorInfo errorInfo(id, info); 42 | error(errorInfo); 43 | } 44 | void error(ErrorInfo::ErrorId id, std::string& info) 45 | { 46 | ErrorInfo errorInfo(id, info); 47 | error(errorInfo); 48 | } 49 | private: 50 | ErrorHandleFunction handle_; 51 | 52 | ErrorHandle() :handle_(nullptr){} 53 | 54 | }; 55 | 56 | } 57 | } 58 | #endif // ! ORCA_CORE_ERROR_HANDLE 59 | 60 | -------------------------------------------------------------------------------- /src/orca/base/error/ErrorInfo.cpp: -------------------------------------------------------------------------------- 1 | #include "ErrorInfo.h" 2 | 3 | using namespace orca::base; 4 | 5 | orca::base::ErrorInfo::ErrorInfo(ErrorId id, std::string info) 6 | :id_(id), 7 | info_(info) 8 | { 9 | } 10 | 11 | ErrorInfo::ErrorId orca::base::ErrorInfo::getErrorId() 12 | { 13 | return id_; 14 | } 15 | 16 | std::string& orca::base::ErrorInfo::getErrorInfo() 17 | { 18 | return info_; 19 | } 20 | -------------------------------------------------------------------------------- /src/orca/base/error/ErrorInfo.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_BASE_ERROR_INFO_H 2 | #define ORCA_BASE_ERROR_INFO_H 3 | 4 | #include 5 | 6 | namespace orca 7 | { 8 | namespace base 9 | { 10 | class ErrorInfo 11 | { 12 | public: 13 | enum ErrorId 14 | { 15 | UVWriteFail = -2048, 16 | UVConnectFail, 17 | UVDisconnectFromServer, 18 | UVSigPipe, 19 | 20 | UndefinedError = -1024, 21 | NoFindActorName, 22 | ActorNameTooLong, 23 | NoFindActorAddr, 24 | ReDefineActorName, 25 | MessagePackNull, 26 | PackMessageError, 27 | NoFindRemoteFramework, 28 | 29 | RepeatedRemoteFrameworkID, 30 | }; 31 | ErrorInfo(ErrorId id,std::string info); 32 | 33 | ErrorId getErrorId(); 34 | std::string& getErrorInfo(); 35 | 36 | private: 37 | ErrorId id_; 38 | std::string info_; 39 | }; 40 | } 41 | } 42 | #endif // ! ORCA_BASE_ERROR_INFO_H 43 | 44 | -------------------------------------------------------------------------------- /src/orca/base/queue/BlockQueue.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/src/orca/base/queue/BlockQueue.h -------------------------------------------------------------------------------- /src/orca/base/thread/Thread.cpp: -------------------------------------------------------------------------------- 1 | #include "Thread.h" 2 | 3 | 4 | using namespace std; 5 | using namespace orca::base; 6 | 7 | Thread::Thread() 8 | :Thread(nullptr) 9 | { 10 | 11 | } 12 | 13 | Thread::Thread(std::function callback) 14 | :started_(false), 15 | run_(callback), 16 | thread_(std::bind(&Thread::entryPoint,this)) 17 | { 18 | 19 | } 20 | 21 | Thread::~Thread() 22 | { 23 | 24 | } 25 | 26 | void Thread::start() 27 | { 28 | unique_lock lock(mutex_); 29 | started_ = true; 30 | condition_.notify_one(); 31 | } 32 | 33 | void Thread::join() 34 | { 35 | thread_.join(); 36 | } 37 | void Thread::detach() 38 | { 39 | thread_.detach(); 40 | } 41 | 42 | bool Thread::isRunInThisThread() 43 | { 44 | return (CurrentThreadId() == id_); 45 | } 46 | 47 | std::thread::id Thread::thisThreadId() 48 | { 49 | return id_; 50 | } 51 | 52 | void Thread::registerProcess(std::function func) 53 | { 54 | run_ = func; 55 | } 56 | 57 | void Thread::entryPoint() 58 | { 59 | 60 | id_ = CurrentThreadId(); 61 | { 62 | unique_lock lock(mutex_); 63 | condition_.wait(lock,[this]{return started_;}); 64 | } 65 | if(run_) 66 | run_(); 67 | } 68 | 69 | 70 | bool Thread::isStarted() 71 | { 72 | return started_; 73 | } 74 | 75 | void Thread::SleepMSeconds(unsigned long ms) 76 | { 77 | std::this_thread::sleep_for(std::chrono::milliseconds(ms)); 78 | } 79 | 80 | 81 | std::thread::id Thread::CurrentThreadId() 82 | { 83 | return std::this_thread::get_id(); 84 | } 85 | 86 | void Thread::YieldCurrentThread() 87 | { 88 | std::this_thread::yield(); 89 | } -------------------------------------------------------------------------------- /src/orca/base/thread/Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_THREAD_H 2 | #define ORCA_THREAD_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace orca 11 | { 12 | 13 | namespace base 14 | { 15 | class Thread 16 | { 17 | public: 18 | Thread(); 19 | Thread(std::function callback); 20 | 21 | virtual ~Thread(); 22 | 23 | void start(); 24 | 25 | virtual void stop() {} 26 | 27 | bool isStarted(); 28 | 29 | void join(); 30 | void detach(); 31 | 32 | bool isRunInThisThread(); 33 | 34 | std::thread::id thisThreadId(); 35 | 36 | void registerProcess(std::function func); 37 | 38 | static void YieldCurrentThread(); 39 | static std::thread::id CurrentThreadId(); 40 | static void SleepMSeconds(unsigned long ms); 41 | 42 | protected: 43 | bool started_; 44 | std::mutex mutex_; 45 | std::condition_variable condition_; 46 | 47 | private: 48 | std::function run_; 49 | std::thread::id id_; 50 | std::thread thread_; 51 | 52 | void entryPoint(); 53 | }; 54 | } 55 | } 56 | #endif 57 | -------------------------------------------------------------------------------- /src/orca/base/thread/ThreadPool.cpp: -------------------------------------------------------------------------------- 1 | #include "ThreadPool.h" 2 | 3 | using namespace std; 4 | using namespace orca::base; 5 | 6 | 7 | ThreadPool::ThreadPool() 8 | { 9 | } 10 | 11 | ThreadPool::~ThreadPool() 12 | { 13 | threads_.clear(); 14 | } 15 | 16 | 17 | void ThreadPool::run() 18 | { 19 | while (true) 20 | { 21 | processor_(); 22 | } 23 | } 24 | 25 | 26 | void ThreadPool::start(unsigned int cnt) 27 | { 28 | for (unsigned int i = 0; i < cnt; i++) 29 | { 30 | ThreadPtr ptr = std::make_shared(std::bind(&ThreadPool::run, this)); 31 | threads_.push_back(ptr); 32 | ptr->start(); 33 | } 34 | } 35 | 36 | void orca::base::ThreadPool::join() 37 | { 38 | for (auto thread : threads_) 39 | { 40 | thread->join(); 41 | } 42 | } 43 | 44 | void ThreadPool::registerPorcess(ProcessorType process) 45 | { 46 | processor_ = process; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /src/orca/base/thread/ThreadPool.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_THREAD_POOL_H 2 | #define ORCA_THREAD_POOL_H 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Thread.h" 10 | namespace orca 11 | { 12 | 13 | namespace base 14 | { 15 | 16 | class ThreadPool 17 | { 18 | public: 19 | using ProcessorType = std::function; 20 | ThreadPool(); 21 | virtual ~ThreadPool(); 22 | using ThreadPtr = std::shared_ptr; 23 | 24 | void registerPorcess(ProcessorType process); 25 | 26 | void start(unsigned int cnt); 27 | void join(); 28 | private: 29 | ProcessorType processor_; 30 | std::vector threads_; 31 | 32 | 33 | void run(); 34 | }; 35 | 36 | } 37 | } 38 | #endif // ! ORCA_THREAD_POOL 39 | -------------------------------------------------------------------------------- /src/orca/core/Address.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_ADDRESS_H 2 | #define ORCA_ADDRESS_H 3 | 4 | #include 5 | 6 | namespace orca 7 | { 8 | namespace core 9 | { 10 | 11 | struct Address 12 | { 13 | uint32_t framework; 14 | uint32_t page; 15 | uint32_t index; 16 | }; 17 | } 18 | } 19 | #endif // ! ORCA_ADDRESS_H 20 | 21 | -------------------------------------------------------------------------------- /src/orca/core/Assert.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_ASSERT_H 2 | #define ORCA_ASSERT_H 3 | 4 | #include 5 | #include 6 | 7 | #define ORCA_FAIL() orca::Assert::Fail(__FILE__, __LINE__) 8 | 9 | #define ORCA_FAIL_MSG(msg) orca::Assert::Fail(__FILE__, __LINE__, msg) 10 | 11 | #define ORCA_ASSERT(condition) orca::Assert::IsFail(condition,__FILE__, __LINE__) 12 | 13 | #define ORCA_ASSERT_MSG(condition, msg) orca::Assert::IsFail(condition,__FILE__, __LINE__,msg) 14 | 15 | 16 | namespace orca 17 | { 18 | 19 | class Assert 20 | { 21 | public: 22 | static void IsFail(bool nomal, const char* const file, const unsigned int line, const std::string message = "") 23 | { 24 | if (!nomal) 25 | { 26 | Fail(file,line,message); 27 | } 28 | } 29 | 30 | static void Fail(const char* const file, const unsigned int line, const std::string message = "") 31 | { 32 | std::cerr<<"fail in :"< 5 | 6 | namespace orca 7 | { 8 | 9 | namespace core 10 | { 11 | 12 | template 13 | struct Mail 14 | { 15 | Address from; 16 | Address destination; 17 | std::shared_ptr message; 18 | }; 19 | } 20 | } 21 | #endif // ! ORCA_MAIL_H 22 | 23 | -------------------------------------------------------------------------------- /src/orca/core/MailBoxThread.cpp: -------------------------------------------------------------------------------- 1 | #include "MessagePack.h" 2 | 3 | -------------------------------------------------------------------------------- /src/orca/core/MailBoxThread.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_MAILBOX_THREAD_H 2 | #define ORCA_MAILBOX_THREAD_H 3 | 4 | class MailBoxThread 5 | { 6 | public: 7 | MailBoxThread(); 8 | ~MailBoxThread(); 9 | 10 | private: 11 | 12 | }; 13 | 14 | MailBoxThread::MailBoxThread() 15 | { 16 | } 17 | 18 | MailBoxThread::~MailBoxThread() 19 | { 20 | } 21 | #endif // ! ORCA_MAILBOX_THREAD 22 | 23 | -------------------------------------------------------------------------------- /src/orca/core/Mailbox.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_MAILBOX_H 2 | #define ORCA_MAILBOX_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Mail.h" 8 | #include "MessagePack.h" 9 | #include "Address.h" 10 | 11 | namespace orca 12 | { 13 | 14 | namespace core 15 | { 16 | 17 | template 18 | class Mailbox 19 | { 20 | public: 21 | using MailboxHandler = std::function&, Address&)>; 22 | Mailbox(MailboxHandler handler); 23 | int delivery(MessagePack& message, Address& addr); 24 | int delivery(Mail& mail); 25 | private: 26 | MailboxHandler handler_; 27 | }; 28 | 29 | template 30 | Mailbox::Mailbox(MailboxHandler handler) 31 | :handler_(handler) 32 | { 33 | 34 | } 35 | 36 | template 37 | inline int Mailbox::delivery(MessagePack& message, Address& addr) 38 | { 39 | if (handler_) 40 | { 41 | handler_(message,addr); 42 | return 0; 43 | } 44 | return -1; 45 | } 46 | 47 | template 48 | inline int Mailbox::delivery(Mail& mail) 49 | { 50 | MessagePack pack(mail.message); 51 | return delivery(pack,mail.from); 52 | } 53 | 54 | } 55 | } 56 | #endif // ! MAILBOX_H 57 | 58 | -------------------------------------------------------------------------------- /src/orca/core/MailboxPage.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_MAILBOX_PAGE_H 2 | #define ORCA_MAILBOX_PAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define ORCA_MAILBOX_PAGE_SIZE 1024 9 | 10 | namespace orca 11 | { 12 | namespace core 13 | { 14 | 15 | template 16 | class MailboxPage 17 | { 18 | public: 19 | using MailboxTypePtr = std::shared_ptr; 20 | MailboxPage(); 21 | ~MailboxPage(); 22 | 23 | template 24 | int allocateMailbox(_Types&&... _Args); 25 | void recycleMailbox(int index); 26 | 27 | MailboxTypePtr getMailbox(int index); 28 | 29 | private: 30 | MailboxTypePtr mailboxs_[ORCA_MAILBOX_PAGE_SIZE]; 31 | 32 | std::set idleIndexs_; 33 | }; 34 | 35 | 36 | template 37 | MailboxPage::MailboxPage() 38 | { 39 | for (int i = 0; i < ORCA_MAILBOX_PAGE_SIZE; i++) 40 | { 41 | idleIndexs_.insert(i); 42 | mailboxs_[i]=nullptr; 43 | } 44 | } 45 | 46 | template 47 | MailboxPage::~MailboxPage() 48 | { 49 | idleIndexs_.clear(); 50 | } 51 | 52 | template 53 | template 54 | int MailboxPage::allocateMailbox(_Types&&... _Args) 55 | { 56 | if (idleIndexs_.empty()) 57 | return -1; 58 | auto it = idleIndexs_.begin(); 59 | int index = *it; 60 | idleIndexs_.erase(it); 61 | mailboxs_[index] = std::make_shared(std::forward<_Types>(_Args)...); 62 | return index; 63 | } 64 | 65 | template 66 | void MailboxPage::recycleMailbox(int index) 67 | { 68 | if (index < ORCA_MAILBOX_PAGE_SIZE) 69 | { 70 | idleIndexs_.insert(index); 71 | mailboxs_[index] = nullptr; 72 | } 73 | } 74 | 75 | template 76 | std::shared_ptr MailboxPage::getMailbox(int index) 77 | { 78 | return mailboxs_[index]; 79 | } 80 | 81 | 82 | 83 | 84 | } 85 | } 86 | #endif // ! MAILBOX_PAGE_H 87 | 88 | -------------------------------------------------------------------------------- /src/orca/core/MessagePack.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_MESSAGE_PACK_H 2 | #define ORCA_MESSAGE_PACK_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "MessageTraits.h" 9 | #include "Define.h" 10 | #include "../base/error/ErrorHandle.h" 11 | 12 | namespace orca 13 | { 14 | namespace core 15 | { 16 | 17 | template 18 | class MessagePack 19 | { 20 | public: 21 | MessagePack(MessageType* ptr = nullptr) 22 | :messagePtr_(ptr) 23 | { 24 | } 25 | MessagePack(std::shared_ptr& ptr) 26 | :messagePtr_(ptr) 27 | { 28 | } 29 | template 30 | std::shared_ptr& create(_Types&&... _Args) 31 | { 32 | messagePtr_ = std::make_shared(std::forward<_Types>(_Args)...); 33 | return messagePtr_; 34 | } 35 | 36 | std::shared_ptr get() const 37 | { 38 | return messagePtr_; 39 | } 40 | 41 | const char* enter() 42 | { 43 | auto ptr = MessageTraits::Enter(*messagePtr_); 44 | return ptr; 45 | } 46 | const unsigned long size() 47 | { 48 | return messagePtr_->size(); 49 | } 50 | void set(std::shared_ptr& ptr) 51 | { 52 | messagePtr_ = ptr; 53 | } 54 | private: 55 | std::shared_ptr messagePtr_; 56 | }; 57 | } 58 | } 59 | #endif 60 | -------------------------------------------------------------------------------- /src/orca/core/MessageTraits.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_MESSAGE_TRAITS_H 2 | #define ORCA_MESSAGE_TRAITS_H 3 | 4 | #include 5 | 6 | namespace orca 7 | { 8 | namespace core 9 | { 10 | 11 | template 12 | struct MessageTraits 13 | { 14 | static const char* Enter(T& object) 15 | { 16 | return object.enter(); 17 | } 18 | }; 19 | 20 | template<> 21 | struct MessageTraits 22 | { 23 | static const char* Enter(std::string& object) 24 | { 25 | return (object.c_str()); 26 | } 27 | }; 28 | } 29 | } 30 | #endif 31 | -------------------------------------------------------------------------------- /src/orca/core/net/ActorClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_ACTOR_CLIENT_H 2 | #define ORCA_ACTOR_CLIENT_H 3 | 4 | #include 5 | #include "../../../../uv-cpp/uv/include/uv11.h" 6 | 7 | namespace orca 8 | { 9 | 10 | namespace core 11 | { 12 | class ActorClient : public uv::TcpClient,public std::enable_shared_from_this 13 | { 14 | public: 15 | using OnRegisterRemoteFramework = std::function)>; 16 | 17 | ActorClient(uv::EventLoop* loop,uv::SocketAddr& addr, uint32_t id); 18 | ~ActorClient(); 19 | 20 | void setRegisterRemoteFrameworkCallback(OnRegisterRemoteFramework callback); 21 | void connect(); 22 | void onConnectStatus(uv::TcpClient::ConnectStatus status); 23 | void onMessage(const char* data, ssize_t size); 24 | 25 | static const int ReconectTimeMS; 26 | static const int HeartbeatTimeSec; 27 | 28 | 29 | private: 30 | uv::SocketAddr addr_; 31 | bool isConenected_; 32 | uv::Timer* timer_; 33 | uint32_t localId_; 34 | int remoteId_; 35 | int cnt_; 36 | OnRegisterRemoteFramework onRegisterRemoteFramework_; 37 | 38 | private: 39 | void reconnect(); 40 | void heartbeat(uv::Timer*); 41 | 42 | }; 43 | using ActorClientPtr = std::shared_ptr; 44 | } 45 | } 46 | #endif // ! ORCA_CORE_ACTOR_CLIENT_H 47 | 48 | -------------------------------------------------------------------------------- /src/orca/core/net/ActorServer.cpp: -------------------------------------------------------------------------------- 1 | #include "ActorServer.h" 2 | #include "TcpPacket.h" 3 | #include "Protocol.h" 4 | 5 | #include "../../base/error/ErrorHandle.h" 6 | 7 | using namespace orca::core; 8 | using namespace std; 9 | 10 | const int ActorServer::HeartTimeOutSecend = 120; 11 | 12 | ActorServer::ActorServer(uv::EventLoop* loop, uint32_t id, OnActorMeessageCallback callback) 13 | :uv::TcpServer(loop), 14 | id_(id), 15 | onActorMessageCallback_(callback) 16 | { 17 | setTimeout(HeartTimeOutSecend); 18 | setMessageCallback(std::bind(&ActorServer::onMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3)); 19 | } 20 | 21 | 22 | void orca::core::ActorServer::onMessage(std::shared_ptrconnection, const char* data, ssize_t size) 23 | { 24 | auto packetBuf = connection->getPacketBuffer(); 25 | if (nullptr != packetBuf) 26 | { 27 | packetBuf->append(data, (int)size); 28 | TcpPacket packet; 29 | while (0 == packetBuf->readGeneric(&packet)) 30 | { 31 | switch (packet.messageType_) 32 | { 33 | case Protocol::ReqFrameworkId: 34 | { 35 | uint32_t id = *((uint32_t*)packet.getData()); 36 | onReqFrameworkId(id, connection); 37 | break; 38 | } 39 | case Protocol::ActorMessage: 40 | onActorMessage(packet.getData(), packet.DataSize()); 41 | break; 42 | default: 43 | break; 44 | } 45 | } 46 | } 47 | } 48 | 49 | void orca::core::ActorServer::onReqFrameworkId(uint32_t id, std::shared_ptr connection) 50 | { 51 | 52 | //resp remote framework id. 53 | TcpPacket packet; 54 | packet.messageType_ = Protocol::RespFrameworkId; 55 | packet.packWithType((const char*)(&id_), sizeof(uint32_t)); 56 | connection->write(packet.Buffer().c_str(), packet.PacketSize(), 57 | [this](uv::WriteInfo info) 58 | { 59 | if (0 != info.status) 60 | { 61 | base::ErrorHandle::Instance()->error(base::ErrorInfo::UVWriteFail,uv::EventLoop::GetErrorMessage(info.status)); 62 | } 63 | }); 64 | endPoints_[id] = connection; 65 | } 66 | 67 | void orca::core::ActorServer::onActorMessage(const char* data, int size) 68 | { 69 | if (onActorMessageCallback_) 70 | onActorMessageCallback_(data, size); 71 | } 72 | -------------------------------------------------------------------------------- /src/orca/core/net/ActorServer.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_ACTOR_SERVER_H 2 | #define ORCA_ACTOR_SERVER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "../../../../uv-cpp/uv/include/uv11.h" 8 | 9 | namespace orca 10 | { 11 | 12 | namespace core 13 | { 14 | using OnActorMeessageCallback = std::function; 15 | class ActorServer : public uv::TcpServer 16 | { 17 | public: 18 | ActorServer(uv::EventLoop* loop,uint32_t id, OnActorMeessageCallback callback); 19 | void onMessage(std::shared_ptrconnection, const char* data, ssize_t size); 20 | static const int HeartTimeOutSecend; 21 | 22 | private: 23 | uint32_t id_; 24 | std::map> endPoints_; 25 | 26 | private: 27 | OnActorMeessageCallback onActorMessageCallback_; 28 | 29 | void onReqFrameworkId(uint32_t id,std::shared_ptr connection); 30 | void onActorMessage(const char* data, int size); 31 | }; 32 | } 33 | } 34 | #endif // ! ORCA_ACTOR_SERVER_H 35 | 36 | -------------------------------------------------------------------------------- /src/orca/core/net/Protocol.h: -------------------------------------------------------------------------------- 1 | #ifndef CORE_PROCOTOL_H 2 | #define CORE_PROCOTOL_H 3 | 4 | 5 | namespace orca 6 | { 7 | namespace core 8 | { 9 | class Protocol 10 | { 11 | public: 12 | enum MessageType 13 | { 14 | ReqFrameworkId = 1, 15 | RespFrameworkId, 16 | ActorMessage, 17 | HeartBeatMessage 18 | }; 19 | 20 | }; 21 | } 22 | } 23 | #endif // ! CORE_PROCOTOL_H 24 | 25 | -------------------------------------------------------------------------------- /src/orca/core/net/TcpPacket.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/src/orca/core/net/TcpPacket.cpp -------------------------------------------------------------------------------- /src/orca/core/net/TcpPacket.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2017, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-7-26 7 | 8 | Description: 9 | */ 10 | #ifndef ORCA_TCP_PACKET_H 11 | #define ORCA_TCP_PACKET_H 12 | 13 | #include "../../../../uv-cpp/uv/include/uv11.h" 14 | 15 | namespace orca 16 | { 17 | namespace core 18 | { 19 | class TcpPacket :public uv::Packet 20 | { 21 | public : 22 | void packWithType(const char* data, uint16_t size); 23 | const char* getData(); 24 | static int ReadTcpBuffer(uv::PacketBuffer* packetbuf, void* out); 25 | public: 26 | uint64_t messageType_; 27 | }; 28 | 29 | } 30 | } 31 | #endif // ! ORCA_ACTOR_SERVER_H 32 | 33 | -------------------------------------------------------------------------------- /src/orca/orca.h: -------------------------------------------------------------------------------- 1 | #ifndef ORCA_ORCA_H 2 | #define ORCA_ORCA_H 3 | 4 | #include "core/Define.h" 5 | #include "core/Actor.h" 6 | #include "core/Framework.h" 7 | #include "core/MessagePack.h" 8 | #include "base/error/ErrorHandle.h" 9 | 10 | #endif // ! ORCA_ORCA_H 11 | 12 | -------------------------------------------------------------------------------- /uv-cpp/.gitattributes: -------------------------------------------------------------------------------- 1 | *.h linguist-language=C++ 2 | *.lua linguist-language=C++ -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 |

24 | ```C++ 25 | int runNoWait() 26 | ``` 27 | 非阻塞运行loop 28 |

29 | ```C++ 30 | bool isRunInLoopThread() 31 | ``` 32 | 判定当前是否在loop线程中 33 | * 返回 true:当前为loop线程 false: 当前不为loop线程 34 |

35 | ```C++ 36 | void runInThisLoop(const DefaultCallback func) 37 | ``` 38 | 注册回调函数到loop线程中执行,确保每次注册的函数都能被执行。 39 | * const DefaultCallback func 回调函数 40 |

41 | ```C++ 42 | uv_loop_t* handle() 43 | ``` 44 | 返回libuv的loop句柄 45 |

46 | ```C++ 47 | static const char* GetErrorMessage(int status) 48 | ``` 49 |
获取错误信息
50 | * int status 错误码 51 | 52 | -------------------------------------------------------------------------------- /uv-cpp/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 |

-------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 |

-------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | * 返回值 : 端口号。 -------------------------------------------------------------------------------- /uv-cpp/doc/cn/TcpAccepter.md: -------------------------------------------------------------------------------- 1 | # TcpAccepter 2 | Tcp监听类,监听接收tcp连接。 3 |

4 | ```C++ 5 | TcpAccepter(EventLoop* loop, bool tcpNoDelay) 6 | ``` 7 | 构造函数。 8 | * EventLoop* loop : Event loop句柄。 9 | * bool tcpNoDelay : Nagle 算法, true : 不延迟 false : 延迟。 10 |

11 | ```C++ 12 | virtual ~TcpAccepter() 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 setNewConnectinonCallback( NewConnectionCallback callback) 49 | ``` 50 | 设置新连接回调函数。 51 | * NewConnectionCallback callback : 新连接回调函数。 52 | * NewConnectionCallback : void(EventLoop* ,UVTcpPtr)。 -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | * 返回值 : 应用层包缓存接口。 -------------------------------------------------------------------------------- /uv-cpp/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 : 定时器周,单位毫秒。 -------------------------------------------------------------------------------- /uv-cpp/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 : 时间轮元素对象。 -------------------------------------------------------------------------------- /uv-cpp/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 : 新消息回调函数。 -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 |

24 | ```C++ 25 | int runNoWait() 26 | ``` 27 | Non-blocking run loop 28 |

29 | ```C++ 30 | bool isRunInLoopThread() 31 | ``` 32 | Is it in the loop thread. 33 | * return true:loop's thread. false: no loop's thread. 34 |

35 | ```C++ 36 | void runInThisLoop(const DefaultCallback func) 37 | ``` 38 | Register callback function to loop's event, all registered callback functions will be called. 39 | 40 | * const DefaultCallback func : callback function 41 |

42 | ```C++ 43 | uv_loop_t* handle() 44 | ``` 45 | Get libuv's loop handle. 46 |

47 | ```C++ 48 | static const char* GetErrorMessage(int status) 49 | ``` 50 |
Get Error Info Message.
51 | * int status : error's code 52 | 53 | -------------------------------------------------------------------------------- /uv-cpp/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 |

-------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 |

-------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 |

-------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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. -------------------------------------------------------------------------------- /uv-cpp/doc/en/TcpAccepter.md: -------------------------------------------------------------------------------- 1 | # TcpAccepter 2 | Tcp's accepter,listen and accept tcp connection. 3 |

4 | ```C++ 5 | TcpAccepter(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 ~TcpAccepter() 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 setNewConnectinonCallback( NewConnectionCallback callback) 49 | ``` 50 | Set callback while new tcp's connection. 51 | * NewConnectionCallback callback : callback. 52 | * NewConnectionCallback : void(EventLoop* ,UVTcpPtr)。 53 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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. -------------------------------------------------------------------------------- /uv-cpp/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. -------------------------------------------------------------------------------- /uv-cpp/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. -------------------------------------------------------------------------------- /uv-cpp/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. -------------------------------------------------------------------------------- /uv-cpp/examples/all_example/Clinet.h: -------------------------------------------------------------------------------- 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_H 12 | #define CLIENT_H 13 | 14 | 15 | #include 16 | #include "uv/include/uv11.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/all_example/EchoServer.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 12 | #include 13 | 14 | #include "EchoServer.h" 15 | #include "uv/include/uv11.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/all_example/EchoServer.h: -------------------------------------------------------------------------------- 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 ECHOSERVER_H 12 | #define ECHOSERVER_H 13 | 14 | #include "uv/include/uv11.h" 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 // ECHOSERVER_H 26 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/clients_cross_thread/Client.h: -------------------------------------------------------------------------------- 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_H 12 | #define CLIENT_H 13 | 14 | 15 | #include 16 | #include "uv/include/uv11.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/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.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/examples/echo_server/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 | #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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/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 | -------------------------------------------------------------------------------- /uv-cpp/examples/pingpang/Clinet.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_H 2 | #define CLIENT_H 3 | 4 | 5 | #include 6 | #include "uv/include/uv11.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/pingpang/EchoServer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "EchoServer.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/pingpang/EchoServer.h: -------------------------------------------------------------------------------- 1 | #ifndef ECHOSERVER_H 2 | #define ECHOSERVER_H 3 | 4 | #include "uv/include/uv11.h" 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 // ECHOSERVER_H 20 | -------------------------------------------------------------------------------- /uv-cpp/examples/pingpang/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #include "EchoServer.h" 5 | #include "Clinet.h" 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 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-20 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | #include "RpcClient.h" 11 | 12 | using namespace uv; 13 | using namespace std; 14 | 15 | RpcClient::RpcClient(EventLoop* loop, uv::SocketAddr& addr) 16 | :TcpClient(loop), 17 | addr_(addr), 18 | onResp_(nullptr) 19 | { 20 | setConnectStatusCallback(std::bind(&RpcClient::onConnect,this,placeholders::_1)); 21 | setMessageCallback(std::bind(&RpcClient::onMessage,this,placeholders::_1,placeholders::_2)); 22 | connect(addr_); 23 | } 24 | 25 | RpcClient::~RpcClient() 26 | { 27 | 28 | } 29 | 30 | 31 | void RpcClient::setOnResp(OnRespCallback callback) 32 | { 33 | onResp_ = callback; 34 | } 35 | 36 | void RpcClient::onConnect(ConnectStatus status) 37 | { 38 | if(status == ConnectStatus::OnConnectSuccess) 39 | { 40 | 41 | } 42 | else 43 | { 44 | uv::Timer* timer = new Timer(loop_, 50, 0, 45 | [this](Timer* timer) 46 | { 47 | connect(addr_); 48 | timer->close([this](Timer* timer) 49 | { 50 | delete timer; 51 | }); 52 | }); 53 | timer->start(); 54 | } 55 | } 56 | 57 | void RpcClient::onMessage(const char* data,ssize_t size) 58 | { 59 | if (nullptr == onResp_) 60 | { 61 | return; 62 | } 63 | Resp resp; 64 | if (0 == resp.unpack(data, (int)size)) 65 | { 66 | onResp_(resp); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-20 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | 12 | #ifndef RPC_CLIENT_H 13 | #define RPC_CLIENT_H 14 | 15 | #include 16 | #include "protocol.h" 17 | 18 | using OnRespCallback = std::function; 19 | 20 | class RpcClient : public uv::TcpClient 21 | { 22 | public: 23 | RpcClient(uv::EventLoop* loop,uv::SocketAddr& addr); 24 | virtual ~RpcClient(); 25 | 26 | template 27 | void sendProtocol(Protocol& req); 28 | 29 | void setOnResp(OnRespCallback callback); 30 | private: 31 | void onConnect(ConnectStatus status); 32 | void onMessage(const char* data,ssize_t size); 33 | 34 | private: 35 | uv::SocketAddr addr_; 36 | OnRespCallback onResp_; 37 | }; 38 | 39 | 40 | template 41 | inline void RpcClient::sendProtocol(Protocol& req) 42 | { 43 | int size = req.size(); 44 | char* data = new char[size]; 45 | req.pack(data, size); 46 | std::string str; 47 | uv::LogWriter::ToHex(str, data, size); 48 | writeInLoop(data, size, 49 | [this](uv::WriteInfo& info) 50 | { 51 | delete[] info.buf; 52 | }); 53 | } 54 | 55 | #endif 56 | 57 | 58 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcCtrl.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-20 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include "RpcCtrl.h" 12 | #include 13 | #include "RpcClient.h" 14 | 15 | 16 | using namespace uv; 17 | using namespace std; 18 | 19 | RpcCtrl::RpcCtrl(uv::SocketAddr& addr) 20 | :addr_(addr) 21 | { 22 | std::thread back(std::bind(&RpcCtrl::backRun, this)); 23 | back.detach(); 24 | this_thread::sleep_for(chrono::milliseconds(200)); 25 | } 26 | 27 | RpcCtrl::~RpcCtrl() 28 | { 29 | } 30 | 31 | void RpcCtrl::sum(Req& req, Resp& resp) 32 | { 33 | std::unique_lock lock(mutex_); 34 | ready_ = false; 35 | client_->sendProtocol(req); 36 | condition_.wait(lock, [this]() 37 | { 38 | return ready_; 39 | }); 40 | resp = resp_; 41 | } 42 | 43 | void RpcCtrl::onResp(Resp& resp) 44 | { 45 | std::unique_lock lock(mutex_); 46 | resp_ = resp; 47 | ready_ = true; 48 | condition_.notify_one(); 49 | } 50 | 51 | void RpcCtrl::backRun() 52 | { 53 | uv::EventLoop loop; 54 | client_ = new RpcClient(&loop, addr_); 55 | client_->setOnResp(std::bind(&RpcCtrl::onResp, this, placeholders::_1)); 56 | loop.run(); 57 | //event's loop not run, so can delete. 58 | delete client_; 59 | } 60 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcCtrl.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-20 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | 12 | #ifndef RPC_CTRL_H 13 | #define RPC_CTRL_H 14 | 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "RpcClient.h" 21 | 22 | 23 | 24 | class RpcCtrl 25 | { 26 | public: 27 | RpcCtrl(uv::SocketAddr& addr); 28 | virtual ~RpcCtrl(); 29 | 30 | void sum(Req& req,Resp& resp); 31 | void onResp(Resp& resp); 32 | 33 | private: 34 | void backRun(); 35 | 36 | private: 37 | RpcClient* client_; 38 | uv::SocketAddr addr_; 39 | Resp resp_; 40 | std::mutex mutex_; 41 | std::condition_variable condition_; 42 | bool ready_; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcServer.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 | #include "RpcServer.h" 12 | 13 | using namespace uv; 14 | using namespace std; 15 | 16 | RpcServer::RpcServer(EventLoop* loop) 17 | :TcpServer(loop) 18 | { 19 | setMessageCallback(std::bind(&RpcServer::onMessage,this,placeholders::_1,placeholders::_2,placeholders::_3)); 20 | } 21 | 22 | RpcServer::~RpcServer() 23 | { 24 | 25 | } 26 | 27 | void RpcServer::onMessage(uv::TcpConnectionPtr connection,const char* buf,ssize_t size) 28 | { 29 | Req req; 30 | if (0 == req.unpack(buf, (int)size)) 31 | { 32 | Resp resp; 33 | sum(req, resp); 34 | sendProtocol(connection, resp); 35 | } 36 | } 37 | 38 | void RpcServer::sum(Req& req, Resp& resp) 39 | { 40 | resp.param = 0; 41 | for (auto param : req.params) 42 | { 43 | resp.param += param; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/RpcServer.h: -------------------------------------------------------------------------------- 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 | #ifndef RPC_SERVER_H 12 | #define RPC_SERVER_H 13 | 14 | 15 | #include 16 | #include "protocol.h" 17 | 18 | class RpcServer : public uv::TcpServer 19 | { 20 | public: 21 | RpcServer(uv::EventLoop* loop); 22 | ~RpcServer(); 23 | 24 | template 25 | void sendProtocol(uv::TcpConnectionPtr connection,Protocol& req); 26 | private: 27 | void onMessage(uv::TcpConnectionPtr connection,const char* buf,ssize_t size); 28 | 29 | void sum(Req& req, Resp& resp); 30 | }; 31 | 32 | #endif 33 | 34 | template 35 | inline void RpcServer::sendProtocol(uv::TcpConnectionPtr connection, Protocol & req) 36 | { 37 | int size = req.size(); 38 | char* data = new char[size]; 39 | req.pack(data, size); 40 | connection->writeInLoop(data, size, 41 | [this](uv::WriteInfo& info) 42 | { 43 | delete[] info.buf; 44 | }); 45 | } 46 | -------------------------------------------------------------------------------- /uv-cpp/examples/rpc/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-20 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "RpcServer.h" 16 | #include "RpcCtrl.h" 17 | 18 | using namespace std; 19 | 20 | void runRpcServer(uv::SocketAddr& addr) 21 | { 22 | thread back([&addr]() 23 | { 24 | uv::EventLoop loop; 25 | RpcServer server(&loop); 26 | server.bindAndListen(addr); 27 | loop.run(); 28 | }); 29 | back.detach(); 30 | this_thread::sleep_for(chrono::milliseconds(200)); 31 | } 32 | 33 | int main(int argc, char** args) 34 | { 35 | //rpc server 36 | uv::SocketAddr addr("127.0.0.1", 10000); 37 | runRpcServer(addr); 38 | 39 | //rpc contrl 40 | RpcCtrl rpc(addr); 41 | Req req; 42 | for (auto i = 1; i <= 100; i++) 43 | { 44 | req.params.push_back(i); 45 | } 46 | 47 | Resp resp; 48 | rpc.sum(req, resp); 49 | std::cout << "1~100 sum :"< 16 | 17 | struct Req 18 | { 19 | std::vector params; 20 | 21 | int size() 22 | { 23 | return (int)params.size() * 4; 24 | } 25 | int pack(char* data, int size) 26 | { 27 | if (size < this->size()) 28 | { 29 | return -1; 30 | } 31 | 32 | for (int32_t param : params) 33 | { 34 | std::copy((char*)(¶m), (char*)(¶m + 1), data); 35 | data += sizeof(param); 36 | } 37 | return 0; 38 | } 39 | int unpack(const char* data, int size) 40 | { 41 | if (size % 4 != 0) 42 | { 43 | return -1; 44 | } 45 | 46 | params.clear(); 47 | for (auto i = 0; i < size; i+=4) 48 | { 49 | int32_t num; 50 | num = *((int32_t*)(data + i)); 51 | params.push_back(num); 52 | } 53 | return 0; 54 | } 55 | }; 56 | 57 | struct Resp 58 | { 59 | int64_t param; 60 | int size() 61 | { 62 | return sizeof(int64_t); 63 | } 64 | int pack(char* data, int size) 65 | { 66 | if (size < this->size()) 67 | { 68 | return -1; 69 | } 70 | std::copy((char*)(¶m), (char*)(¶m + 1), data); 71 | return 0; 72 | } 73 | 74 | int unpack(const char* data, int size) 75 | { 76 | if (size < this->size()) 77 | { 78 | return -1; 79 | } 80 | param = *((int64_t*)data); 81 | return 0; 82 | } 83 | }; 84 | 85 | #endif -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/aix.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_AIX_H 23 | #define UV_AIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | int fs_fd; \ 27 | 28 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 29 | uv__io_t event_watcher; \ 30 | char *dir_filename; \ 31 | 32 | #endif /* UV_AIX_H */ 33 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/android-ifaddrs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1995, 1999 3 | * Berkeley Software Design, Inc. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 12 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 14 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 15 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 16 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 17 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 18 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 19 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 20 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 21 | * SUCH DAMAGE. 22 | * 23 | * BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp 24 | */ 25 | 26 | #ifndef _IFADDRS_H_ 27 | #define _IFADDRS_H_ 28 | 29 | struct ifaddrs { 30 | struct ifaddrs *ifa_next; 31 | char *ifa_name; 32 | unsigned int ifa_flags; 33 | struct sockaddr *ifa_addr; 34 | struct sockaddr *ifa_netmask; 35 | struct sockaddr *ifa_dstaddr; 36 | void *ifa_data; 37 | }; 38 | 39 | /* 40 | * This may have been defined in . Note that if is 41 | * to be included it must be included before this header file. 42 | */ 43 | #ifndef ifa_broadaddr 44 | #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 45 | #endif 46 | 47 | #include 48 | 49 | __BEGIN_DECLS 50 | extern int getifaddrs(struct ifaddrs **ifap); 51 | extern void freeifaddrs(struct ifaddrs *ifa); 52 | __END_DECLS 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/bsd.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_BSD_H 23 | #define UV_BSD_H 24 | 25 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 26 | uv__io_t event_watcher; \ 27 | 28 | #define UV_IO_PRIVATE_PLATFORM_FIELDS \ 29 | int rcount; \ 30 | int wcount; \ 31 | 32 | #define UV_HAVE_KQUEUE 1 33 | 34 | #endif /* UV_BSD_H */ 35 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/linux.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_LINUX_H 23 | #define UV_LINUX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | uv__io_t inotify_read_watcher; \ 27 | void* inotify_watchers; \ 28 | int inotify_fd; \ 29 | 30 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 31 | void* watchers[2]; \ 32 | int wd; \ 33 | 34 | #endif /* UV_LINUX_H */ 35 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/os390.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_MVS_H 23 | #define UV_MVS_H 24 | 25 | #define UV_PLATFORM_SEM_T long 26 | 27 | #define UV_PLATFORM_LOOP_FIELDS \ 28 | void* ep; \ 29 | 30 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 31 | char rfis_rftok[8]; \ 32 | 33 | #endif /* UV_MVS_H */ 34 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/posix.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_POSIX_H 23 | #define UV_POSIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | struct pollfd* poll_fds; \ 27 | size_t poll_fds_used; \ 28 | size_t poll_fds_size; \ 29 | unsigned char poll_fds_iterating; \ 30 | 31 | #endif /* UV_POSIX_H */ 32 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/sunos.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_SUNOS_H 23 | #define UV_SUNOS_H 24 | 25 | #include 26 | #include 27 | 28 | /* For the sake of convenience and reduced #ifdef-ery in src/unix/sunos.c, 29 | * add the fs_event fields even when this version of SunOS doesn't support 30 | * file watching. 31 | */ 32 | #define UV_PLATFORM_LOOP_FIELDS \ 33 | uv__io_t fs_event_watcher; \ 34 | int fs_fd; \ 35 | 36 | #if defined(PORT_SOURCE_FILE) 37 | 38 | # define UV_PLATFORM_FS_EVENT_FIELDS \ 39 | file_obj_t fo; \ 40 | int fd; \ 41 | 42 | #endif /* defined(PORT_SOURCE_FILE) */ 43 | 44 | #endif /* UV_SUNOS_H */ 45 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/threadpool.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | /* 23 | * This file is private to libuv. It provides common functionality to both 24 | * Windows and Unix backends. 25 | */ 26 | 27 | #ifndef UV_THREADPOOL_H_ 28 | #define UV_THREADPOOL_H_ 29 | 30 | struct uv__work { 31 | void (*work)(struct uv__work *w); 32 | void (*done)(struct uv__work *w, int status); 33 | struct uv_loop_s* loop; 34 | void* wq[2]; 35 | }; 36 | 37 | #endif /* UV_THREADPOOL_H_ */ 38 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/include/uv/version.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_VERSION_H 23 | #define UV_VERSION_H 24 | 25 | /* 26 | * Versions with the same major number are ABI stable. API is allowed to 27 | * evolve between minor releases, but only in a backwards compatible way. 28 | * Make sure you update the -soname directives in configure.ac 29 | * and uv.gyp whenever you bump UV_VERSION_MAJOR or UV_VERSION_MINOR (but 30 | * not UV_VERSION_PATCH.) 31 | */ 32 | 33 | #define UV_VERSION_MAJOR 1 34 | #define UV_VERSION_MINOR 30 35 | #define UV_VERSION_PATCH 0 36 | #define UV_VERSION_IS_RELEASE 1 37 | #define UV_VERSION_SUFFIX "" 38 | 39 | #define UV_VERSION_HEX ((UV_VERSION_MAJOR << 16) | \ 40 | (UV_VERSION_MINOR << 8) | \ 41 | (UV_VERSION_PATCH)) 42 | 43 | #endif /* UV_VERSION_H */ 44 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.a -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.la: -------------------------------------------------------------------------------- 1 | # libuv.la - a libtool library file 2 | # Generated by libtool (GNU libtool) 2.4.6 Debian-2.4.6-9 3 | # 4 | # Please DO NOT delete this file! 5 | # It is necessary for linking the library. 6 | 7 | # The name that we can dlopen(3). 8 | dlname='libuv.so.1' 9 | 10 | # Names of this library. 11 | library_names='libuv.so.1.0.0 libuv.so.1 libuv.so' 12 | 13 | # The name of the static archive. 14 | old_library='libuv.a' 15 | 16 | # Linker flags that cannot go in dependency_libs. 17 | inherited_linker_flags='' 18 | 19 | # Libraries that this one depends upon. 20 | dependency_libs=' -lrt -lpthread -lnsl -ldl' 21 | 22 | # Names of additional weak libraries provided by this library 23 | weak_library_names='' 24 | 25 | # Version information for libuv. 26 | current=1 27 | age=0 28 | revision=0 29 | 30 | # Is this an already installed library? 31 | installed=yes 32 | 33 | # Should we warn about portability when linking against -modules? 34 | shouldnotlink=no 35 | 36 | # Files to dlopen/dlpreopen 37 | dlopen='' 38 | dlpreopen='' 39 | 40 | # Directory that this library needs to be installed in: 41 | libdir='/mnt/hgfs/share/uv130/lib' 42 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so.1 -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so.1.0.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/gcc8.3.0/libuv.so.1.0.0 -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/gcc8.3.0/pkgconfig/libuv.pc: -------------------------------------------------------------------------------- 1 | prefix=/mnt/hgfs/share/uv130 2 | exec_prefix=${prefix} 3 | libdir=${exec_prefix}/lib 4 | includedir=${prefix}/include 5 | 6 | Name: libuv 7 | Version: 1.30.0 8 | Description: multi-platform support library with a focus on asynchronous I/O. 9 | URL: http://libuv.org/ 10 | 11 | Libs: -L${libdir} -luv -lrt -lpthread -lnsl -ldl 12 | Cflags: -I${includedir} 13 | -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/win_sdk10.0.10240/Debug/libuv.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/win_sdk10.0.10240/Debug/libuv.lib -------------------------------------------------------------------------------- /uv-cpp/libuv1.30.0/lib/win_sdk10.0.10240/Release/libuv.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wlgq2/orca/1994598c238ec4ea29bcef27890fc6c34b42dfa7/uv-cpp/libuv1.30.0/lib/win_sdk10.0.10240/Release/libuv.lib -------------------------------------------------------------------------------- /uv-cpp/project/cmake/build.sh: -------------------------------------------------------------------------------- 1 | cmake ../../ 2 | make 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/clients/clients.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/create_lib/create_lib.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 72 | 73 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/dns/dns.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/echo_server/echo_server.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/helloworld/helloworld.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/http_client/http_client.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/http_server/http_server.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/codeblocks_linux/radix_tree/radix_tree.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 69 | 70 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/DNS/DNS.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/all_example/all_example.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/clients/clients.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/create_lib/create_lib.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/echo_server/echo_server.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/helloworld/helloworld.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/http_client/http_client.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/http_server/http_server.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/pingpong/pingpong.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/radix_tree/radix_tree.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/project/vs2017/rpc/rpc.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /uv-cpp/uv/Async.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-2-21 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | #include "include/Async.h" 11 | 12 | using namespace uv; 13 | 14 | Async::Async(EventLoop * loop) 15 | : handle_(new uv_async_t) 16 | { 17 | ::uv_async_init(loop->handle(), handle_, Async::Callback); 18 | handle_->data = static_cast(this); 19 | } 20 | 21 | Async::~Async() 22 | { 23 | 24 | } 25 | 26 | void Async::runInThisLoop(DefaultCallback callback) 27 | { 28 | { 29 | std::lock_guard lock(mutex_); 30 | callbacks_.push(callback); 31 | } 32 | ::uv_async_send(handle_); 33 | } 34 | 35 | void uv::Async::process() 36 | { 37 | std::lock_guard lock(mutex_); 38 | while (!callbacks_.empty()) 39 | { 40 | auto func = callbacks_.front(); 41 | func(); 42 | callbacks_.pop(); 43 | } 44 | } 45 | 46 | void uv::Async::close(DefaultCallback callback) 47 | { 48 | if (::uv_is_closing((uv_handle_t*)handle_) == 0) 49 | { 50 | ::uv_close((uv_handle_t*)handle_, [](uv_handle_t* handle) 51 | { 52 | delete (uv_async_t*)handle; 53 | }); 54 | } 55 | } 56 | 57 | void Async::Callback(uv_async_t* handle) 58 | { 59 | auto async = static_cast(handle->data); 60 | async->process(); 61 | } -------------------------------------------------------------------------------- /uv-cpp/uv/DnsGet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-29 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | #include "include/DnsGet.h" 11 | #include "include/LogWriter.h" 12 | 13 | using namespace uv; 14 | 15 | DNSGet::DNSGet(uv::EventLoop* loop) 16 | :loop_(loop), 17 | callback_(nullptr) 18 | { 19 | handle_.data = (void*) this; 20 | } 21 | 22 | void DNSGet::setOnDNSCallback(OnGetIPAddrCallback callback) 23 | { 24 | callback_ = callback; 25 | } 26 | 27 | int DNSGet::GetIP(std::string& hostname, std::string service ) 28 | { 29 | return uv_getaddrinfo(loop_->handle(), &handle_, DNSGet::onDNSGet, hostname.c_str(), service.c_str(), NULL); 30 | } 31 | 32 | int DNSGet::GetIP(std::string&& hostname, std::string service) 33 | { 34 | return GetIP(hostname, service); 35 | } 36 | 37 | void DNSGet::OnCallback(int status, addrinfo* res) 38 | { 39 | if (nullptr != callback_) 40 | { 41 | std::string ip(""); 42 | if (status < 0) { 43 | callback_(status, ip); 44 | return; 45 | } 46 | ip.resize(32); 47 | #if _MSC_VER 48 | if (res->ai_protocol == IPPROTO_IPV4 || res->ai_protocol == IPPROTO_TCP || res->ai_protocol == IPPROTO_IP) 49 | 50 | #else 51 | if (res->ai_protocol == IPPROTO_TCP || res->ai_protocol == IPPROTO_IP) 52 | #endif 53 | { 54 | uv_ip4_name((struct sockaddr_in*) res->ai_addr, (char*)ip.c_str(), ip.size()); 55 | 56 | } 57 | else if (res->ai_protocol == IPPROTO_IPV6) 58 | { 59 | uv_ip6_name((struct sockaddr_in6*) res->ai_addr, (char*)ip.c_str(), ip.size()); 60 | } 61 | callback_(status, ip); 62 | } 63 | } 64 | 65 | void DNSGet::onDNSGet(uv_getaddrinfo_t* addrInfo, int status, addrinfo* res) 66 | { 67 | auto ptr = (DNSGet*)(addrInfo->data); 68 | if (ptr) 69 | { 70 | ptr->OnCallback(status,res); 71 | uv_freeaddrinfo(res); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /uv-cpp/uv/EventLoop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-6-17 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include "include/EventLoop.h" 12 | #include "include/TcpConnection.h" 13 | #include "include/Async.h" 14 | 15 | using namespace uv; 16 | 17 | EventLoop::EventLoop() 18 | :EventLoop(EventLoop::Mode::New) 19 | { 20 | } 21 | 22 | EventLoop::EventLoop(EventLoop::Mode mode) 23 | : isRun_(false), 24 | loop_(nullptr), 25 | async_(nullptr) 26 | { 27 | if (mode == EventLoop::Mode::New) 28 | { 29 | loop_ = new uv_loop_t(); 30 | ::uv_loop_init(loop_); 31 | } 32 | else 33 | { 34 | loop_ = uv_default_loop(); 35 | } 36 | async_ = new Async(this); 37 | } 38 | 39 | EventLoop::~EventLoop() 40 | { 41 | if (loop_ != uv_default_loop()) 42 | { 43 | uv_loop_close(loop_); 44 | delete loop_; 45 | delete async_; 46 | } 47 | } 48 | 49 | EventLoop* uv::EventLoop::DefaultLoop() 50 | { 51 | static EventLoop defaultLoop(EventLoop::Mode::Default); 52 | return &defaultLoop; 53 | } 54 | 55 | uv_loop_t* EventLoop::handle() 56 | { 57 | return loop_; 58 | } 59 | 60 | int EventLoop::run() 61 | { 62 | loopThreadId_ = std::this_thread::get_id(); 63 | isRun_ = true; 64 | return ::uv_run(loop_, UV_RUN_DEFAULT); 65 | } 66 | 67 | int uv::EventLoop::runNoWait() 68 | { 69 | loopThreadId_ = std::this_thread::get_id(); 70 | isRun_ = true; 71 | return ::uv_run(loop_, UV_RUN_NOWAIT); 72 | } 73 | 74 | 75 | bool EventLoop::isRunInLoopThread() 76 | { 77 | if (isRun_) 78 | { 79 | return std::this_thread::get_id() == loopThreadId_; 80 | } 81 | //EventLoop未运行. 82 | return false; 83 | } 84 | 85 | void uv::EventLoop::runInThisLoop(const DefaultCallback func) 86 | { 87 | if (nullptr == func) 88 | return; 89 | 90 | if (isRunInLoopThread()) 91 | { 92 | func(); 93 | return; 94 | } 95 | async_->runInThisLoop(func); 96 | } 97 | 98 | const char* EventLoop::GetErrorMessage(int status) 99 | { 100 | if (WriteInfo::Disconnected == status) 101 | { 102 | static char info[] = "the connection is disconnected"; 103 | return info; 104 | } 105 | return uv_strerror(status); 106 | } -------------------------------------------------------------------------------- /uv-cpp/uv/GlobalConfig.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-10-25 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include 12 | 13 | #include "include/GlobalConfig.h" 14 | #include "include/Packet.h" 15 | 16 | using namespace uv; 17 | using namespace std; 18 | //默认不使用buffer 19 | GlobalConfig::BufferMode GlobalConfig::BufferModeStatus = GlobalConfig::BufferMode::NoBuffer; 20 | 21 | //默认循环buffer容量32Kb。 22 | uint64_t GlobalConfig::CycleBufferSize = 1024 << 5; 23 | 24 | //默认包解析函数 25 | ReadBufferStringFunc GlobalConfig::ReadBufferString = nullptr; 26 | ReadBufferPacketFunc GlobalConfig::ReadBufferPacket = std::bind(&Packet::readFromBuffer, placeholders::_1, placeholders::_2);; 27 | ReadBufferVoidFunc GlobalConfig::ReadBufferVoid = nullptr; 28 | -------------------------------------------------------------------------------- /uv-cpp/uv/Idle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-10 7 | 8 | Description: uv-cpp 9 | */ 10 | 11 | #include "include/Idle.h" 12 | 13 | using namespace uv; 14 | 15 | Idle::Idle(EventLoop* loop) 16 | :idle_(new uv_idle_t()) 17 | { 18 | idle_->data = this; 19 | uv_idle_init(loop->handle(), idle_); 20 | uv_idle_start(idle_, idleCallback); 21 | } 22 | 23 | Idle::~Idle() 24 | { 25 | uv_idle_stop(idle_); 26 | delete idle_; 27 | } 28 | 29 | void Idle::onCallback() 30 | { 31 | if (callback_) 32 | callback_(); 33 | } 34 | 35 | void Idle::setCallback(DefaultCallback callback) 36 | { 37 | callback_ = callback; 38 | } 39 | 40 | void Idle::idleCallback(uv_idle_t* handle) 41 | { 42 | Idle* ptr = static_cast(handle->data); 43 | ptr->onCallback(); 44 | } 45 | -------------------------------------------------------------------------------- /uv-cpp/uv/ListBuffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2018-10-24 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include "include/ListBuffer.h" 12 | 13 | 14 | #include 15 | using namespace uv; 16 | using namespace std; 17 | 18 | ListBuffer::ListBuffer() 19 | { 20 | 21 | } 22 | 23 | ListBuffer::~ListBuffer() 24 | { 25 | 26 | } 27 | 28 | int ListBuffer::append(const char* data, uint64_t size) 29 | { 30 | for (uint64_t i = 0; i < size; i++) 31 | buffer_.push_back(data[i]); 32 | return 0; 33 | } 34 | 35 | 36 | int uv::ListBuffer::readBufferN(std::string& data, uint64_t N) 37 | { 38 | if (N > (uint64_t)buffer_.size()) 39 | return -1; 40 | auto it = buffer_.begin(); 41 | for (uint64_t i = 0; i < N; i++) 42 | { 43 | data.push_back(*it); 44 | it++; 45 | } 46 | return 0; 47 | } 48 | 49 | uint64_t uv::ListBuffer::readSize() 50 | { 51 | return (uint64_t)buffer_.size(); 52 | } 53 | 54 | int uv::ListBuffer::clearBufferN(uint64_t N) 55 | { 56 | for (uint64_t i = 0; i < N; i++) 57 | { 58 | if (buffer_.empty()) 59 | break; 60 | buffer_.pop_front(); 61 | } 62 | return 0; 63 | } 64 | 65 | int uv::ListBuffer::clear() 66 | { 67 | buffer_.clear(); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /uv-cpp/uv/Signal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2018-8-23 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include "include/Signal.h" 12 | #include "include/LogWriter.h" 13 | #include "include/EventLoop.h" 14 | 15 | using namespace uv; 16 | using namespace std; 17 | 18 | Signal::Signal(EventLoop* loop, int sig, SignalHandle handle) 19 | :signal_(new uv_signal_t), 20 | handle_(handle), 21 | closeCallback_(nullptr) 22 | { 23 | ::uv_signal_init(loop->handle(), signal_); 24 | signal_->data = static_cast(this); 25 | ::uv_signal_start(signal_, &Signal::onSignal, sig); 26 | } 27 | 28 | void uv::Signal::close(DefaultCallback callback) 29 | { 30 | closeCallback_ = callback; 31 | if (uv_is_closing((uv_handle_t*)signal_) == 0) 32 | { 33 | ::uv_close((uv_handle_t*)signal_, 34 | [](uv_handle_t* handle) 35 | { 36 | auto ptr = static_cast(handle->data); 37 | ptr->closeComplete(); 38 | delete handle; 39 | }); 40 | } 41 | else 42 | { 43 | closeCallback_(); 44 | } 45 | } 46 | 47 | Signal::~Signal() 48 | { 49 | } 50 | 51 | void Signal::setHandle(SignalHandle handle) 52 | { 53 | handle_ = handle; 54 | } 55 | 56 | bool Signal::handle(int signum) 57 | { 58 | if (handle_) 59 | { 60 | handle_(signum); 61 | return true; 62 | } 63 | return false; 64 | } 65 | 66 | void uv::Signal::closeComplete() 67 | { 68 | if (closeCallback_) 69 | closeCallback_(); 70 | } 71 | 72 | void uv::Signal::Ignore(int sig) 73 | { 74 | #ifdef __linux__ 75 | signal(sig, SIG_IGN); 76 | #endif 77 | } 78 | 79 | void Signal::onSignal(uv_signal_t* handle, int signum) 80 | { 81 | auto ptr = static_cast (handle->data); 82 | if (!ptr->handle(signum)) 83 | { 84 | uv::LogWriter::Instance()->warn( std::string("non defined signal handle :")+std::to_string(signum)); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /uv-cpp/uv/TcpAccepter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-12-31 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #include "include/TcpAccepter.h" 12 | #include "include/LogWriter.h" 13 | 14 | using namespace std; 15 | using namespace uv; 16 | 17 | TcpAccepter::TcpAccepter(EventLoop* loop, bool tcpNoDelay) 18 | :listened_(false), 19 | tcpNoDelay_(tcpNoDelay), 20 | loop_(loop), 21 | callback_(nullptr) 22 | { 23 | ::uv_tcp_init(loop_->handle(), &server_); 24 | if (tcpNoDelay_) 25 | ::uv_tcp_nodelay(&server_, 1); 26 | server_.data = (void* )this; 27 | } 28 | 29 | 30 | 31 | TcpAccepter:: ~TcpAccepter() 32 | { 33 | 34 | } 35 | 36 | EventLoop* TcpAccepter::Loop() 37 | { 38 | return loop_; 39 | } 40 | 41 | void TcpAccepter::onNewConnect(UVTcpPtr client) 42 | { 43 | if(nullptr !=callback_) 44 | { 45 | callback_(loop_,client); 46 | } 47 | } 48 | 49 | int uv::TcpAccepter::bind(SocketAddr& addr) 50 | { 51 | return ::uv_tcp_bind(&server_, addr.Addr(), 0); 52 | } 53 | 54 | int TcpAccepter::listen() 55 | { 56 | auto rst = ::uv_listen((uv_stream_t*) &server_, 128, 57 | [](uv_stream_t *server, int status) 58 | { 59 | if (status < 0) 60 | { 61 | uv::LogWriter::Instance()->error (std::string("New connection error :")+ EventLoop::GetErrorMessage(status)); 62 | return; 63 | } 64 | TcpAccepter* accept = static_cast(server->data); 65 | UVTcpPtr client = make_shared(); 66 | ::uv_tcp_init(accept->Loop()->handle(), client.get()); 67 | if (accept->isTcpNoDelay()) 68 | ::uv_tcp_nodelay(client.get(), 1); 69 | 70 | if ( 0 == ::uv_accept(server, (uv_stream_t*) client.get())) 71 | { 72 | accept->onNewConnect(client); 73 | } 74 | else 75 | { 76 | ::uv_close((uv_handle_t*) client.get(), NULL); 77 | } 78 | }); 79 | if (rst == 0) 80 | { 81 | listened_ = true; 82 | } 83 | return rst; 84 | } 85 | 86 | 87 | bool TcpAccepter::isListen() 88 | { 89 | return listened_; 90 | } 91 | 92 | bool uv::TcpAccepter::isTcpNoDelay() 93 | { 94 | return tcpNoDelay_; 95 | } 96 | 97 | void TcpAccepter::setNewConnectinonCallback(NewConnectionCallback callback) 98 | { 99 | callback_ = callback; 100 | } 101 | -------------------------------------------------------------------------------- /uv-cpp/uv/Timer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-10 7 | 8 | Description: uv-cpp 9 | */ 10 | 11 | #include "include/Timer.h" 12 | 13 | using namespace uv; 14 | 15 | Timer::Timer(EventLoop * loop, uint64_t timeout, uint64_t repeat, TimerCallback callback) 16 | :started_(false), 17 | handle_(new uv_timer_t), 18 | timeout_(timeout), 19 | repeat_(repeat), 20 | callback_(callback), 21 | closeComplete_(nullptr) 22 | { 23 | handle_->data = static_cast(this); 24 | ::uv_timer_init(loop->handle(), handle_); 25 | } 26 | 27 | Timer::~Timer() 28 | { 29 | } 30 | 31 | void Timer::start() 32 | { 33 | if (!started_) 34 | { 35 | started_ = true; 36 | ::uv_timer_start(handle_, Timer::process, timeout_, repeat_); 37 | } 38 | } 39 | 40 | void Timer::close(TimerCloseComplete callback) 41 | { 42 | closeComplete_ = callback; 43 | if (uv_is_active((uv_handle_t*)handle_)) 44 | { 45 | uv_timer_stop(handle_); 46 | } 47 | if (uv_is_closing((uv_handle_t*)handle_) == 0) 48 | { 49 | ::uv_close((uv_handle_t*)handle_, 50 | [](uv_handle_t* handle) 51 | { 52 | auto ptr = static_cast(handle->data); 53 | ptr->closeComplete(); 54 | delete handle; 55 | }); 56 | } 57 | else 58 | { 59 | closeComplete(); 60 | } 61 | } 62 | 63 | 64 | void Timer::setTimerRepeat(uint64_t ms) 65 | { 66 | repeat_ = ms; 67 | ::uv_timer_set_repeat(handle_, ms); 68 | } 69 | 70 | 71 | void Timer::onTimeOut() 72 | { 73 | if (callback_) 74 | { 75 | callback_(this); 76 | } 77 | } 78 | 79 | void Timer::closeComplete() 80 | { 81 | if (closeComplete_) 82 | closeComplete_(this); 83 | } 84 | 85 | void Timer::process(uv_timer_t * handle) 86 | { 87 | auto ptr = static_cast(handle->data); 88 | ptr->onTimeOut(); 89 | } 90 | 91 | 92 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/Async.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-2-21 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_ASYNC_H 12 | #define UV_ASYNC_H 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "EventLoop.h" 20 | 21 | 22 | namespace uv 23 | { 24 | 25 | class Async : public std::enable_shared_from_this 26 | { 27 | public: 28 | 29 | 30 | Async(EventLoop* loop); 31 | virtual ~Async(); 32 | 33 | void runInThisLoop(DefaultCallback callback); 34 | 35 | 36 | void close(DefaultCallback callback); 37 | private: 38 | std::mutex mutex_; 39 | uv_async_t* handle_; 40 | std::queue callbacks_; 41 | 42 | void process(); 43 | static void Callback(uv_async_t* handle); 44 | 45 | }; 46 | 47 | 48 | using AsyncPtr = std::shared_ptr; 49 | 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/CycleBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2018-10-24 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | #ifndef UV_CYCLE_BUFFER_H 11 | #define UV_CYCLE_BUFFER_H 12 | 13 | #include "PacketBuffer.h" 14 | 15 | //ArrayBuffer(cycle) 16 | //--------------------------------------- 17 | // Null | byte | byte | Null 18 | //--------------------------------------- 19 | // ↑ ↑ 20 | // read position write position 21 | 22 | // ↓can not write 23 | //--------------------------------------- 24 | // byte | byte | byte | byte 25 | //--------------------------------------- 26 | // ↑ ↑ 27 | // write position read position 28 | 29 | //not thread safe. 30 | 31 | namespace uv 32 | { 33 | 34 | struct SizeInfo 35 | { 36 | uint64_t size; 37 | uint64_t part1; 38 | uint64_t part2; 39 | }; 40 | 41 | class CycleBuffer :public PacketBuffer 42 | { 43 | public: 44 | CycleBuffer(); 45 | ~CycleBuffer(); 46 | 47 | //写字节时必须距离读字节一个字节,否则无法区分缓存满/空。 48 | int append(const char* data, uint64_t size) override; 49 | int readBufferN(std::string& data, uint64_t N) override; 50 | int clearBufferN(uint64_t N) override; 51 | int clear() override; 52 | uint64_t readSize() override; 53 | 54 | private: 55 | uint64_t usableSize(); 56 | void usableSizeInfo(SizeInfo& info); 57 | void readSizeInfo(SizeInfo& info); 58 | 59 | int addWriteIndex(uint64_t size); 60 | int addReadIndex(uint64_t size); 61 | 62 | private: 63 | uint8_t* buffer_; 64 | uint64_t writeIndex_; 65 | uint64_t readIndex_; 66 | 67 | }; 68 | 69 | } 70 | #endif // ! UV_PACKET_H 71 | 72 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/DnsGet.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-29 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_DNS_GET_H 12 | #define UV_DNS_GET_H 13 | 14 | #include 15 | 16 | #include "EventLoop.h" 17 | #include "SocketAddr.h" 18 | 19 | namespace uv 20 | { 21 | 22 | 23 | class DNSGet 24 | { 25 | public: 26 | using OnGetIPAddrCallback = std::function; 27 | 28 | DNSGet(uv::EventLoop* loop); 29 | 30 | void setOnDNSCallback(OnGetIPAddrCallback callback); 31 | 32 | int GetIP(std::string& hostname, std::string service = ""); 33 | int GetIP(std::string&& hostname, std::string service = ""); 34 | 35 | 36 | private: 37 | EventLoop* loop_; 38 | OnGetIPAddrCallback callback_; 39 | uv_getaddrinfo_t handle_; 40 | 41 | void OnCallback(int status, addrinfo* res); 42 | static void onDNSGet(uv_getaddrinfo_t* addrInfo, int status, struct addrinfo* res); 43 | 44 | }; 45 | 46 | } 47 | #endif 48 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/EventLoop.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-6-17 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_EVENT_LOOP_H 12 | #define UV_EVENT_LOOP_H 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace uv 20 | { 21 | using DefaultCallback = std::function; 22 | 23 | class Async; 24 | class EventLoop 25 | { 26 | public: 27 | enum Mode 28 | { 29 | Default, 30 | New 31 | }; 32 | 33 | EventLoop(); 34 | ~EventLoop(); 35 | 36 | static EventLoop* DefaultLoop(); 37 | 38 | int run(); 39 | int runNoWait(); 40 | bool isRunInLoopThread(); 41 | void runInThisLoop(const DefaultCallback func); 42 | uv_loop_t* handle(); 43 | 44 | static const char* GetErrorMessage(int status); 45 | 46 | private: 47 | EventLoop(Mode mode); 48 | 49 | std::thread::id loopThreadId_; 50 | std::atomic isRun_; 51 | uv_loop_t* loop_; 52 | Async* async_; 53 | }; 54 | 55 | using EventLoopPtr = std::shared_ptr; 56 | } 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/GlobalConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-10-25 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_GLOBAL_CONFIG_H 12 | #define UV_GLOBAL_CONFIG_H 13 | 14 | #include 15 | #include 16 | 17 | namespace uv 18 | { 19 | class PacketBuffer; 20 | class Packet; 21 | using ReadBufferStringFunc = std::function; 22 | using ReadBufferPacketFunc = std::function; 23 | using ReadBufferVoidFunc = std::function; 24 | 25 | class GlobalConfig 26 | { 27 | public: 28 | enum BufferMode 29 | { 30 | NoBuffer, 31 | CycleBuffer, 32 | ListBuffer 33 | }; 34 | static BufferMode BufferModeStatus; 35 | static uint64_t CycleBufferSize; 36 | 37 | 38 | static ReadBufferStringFunc ReadBufferString; 39 | static ReadBufferPacketFunc ReadBufferPacket; 40 | static ReadBufferVoidFunc ReadBufferVoid; 41 | }; 42 | } 43 | #endif 44 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/Idle.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-11 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_IDLE_H 12 | #define UV_IDLE_H 13 | 14 | #include "EventLoop.h" 15 | 16 | namespace uv 17 | { 18 | 19 | class Idle 20 | { 21 | public: 22 | Idle(EventLoop* loop); 23 | virtual ~Idle(); 24 | 25 | 26 | void setCallback(DefaultCallback callback); 27 | 28 | private: 29 | uv_idle_t* idle_; 30 | 31 | DefaultCallback callback_; 32 | 33 | private: 34 | void onCallback(); 35 | static void idleCallback(uv_idle_t *handle); 36 | }; 37 | 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/ListBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-10-24 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_LIST_BUFFER_H 12 | #define UV_LIST_BUFFER_H 13 | 14 | 15 | #include 16 | 17 | #include "PacketBuffer.h" 18 | 19 | 20 | //ListBuffer 21 | //--------------------------------------- 22 | // Null | Packet | Packet | Null 23 | //--------------------------------------- 24 | // ↑ ↑ 25 | // read position write position 26 | 27 | //not thread safe. 28 | 29 | namespace uv 30 | { 31 | 32 | class ListBuffer : public PacketBuffer 33 | { 34 | public: 35 | ListBuffer(); 36 | ~ListBuffer(); 37 | 38 | int append(const char* data, uint64_t size) override; 39 | int readBufferN(std::string& data, uint64_t N) override; 40 | uint64_t readSize() override; 41 | int clearBufferN(uint64_t N) override; 42 | int clear() override; 43 | 44 | private: 45 | std::list buffer_; 46 | 47 | }; 48 | 49 | } 50 | 51 | #endif // AGILNET_NET_BUFFER 52 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/LogWriter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-10-9 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_LOG_INTERFACE_H 12 | #define UV_LOG_INTERFACE_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #define USED_STD_OUT 1 19 | 20 | namespace uv 21 | { 22 | 23 | class LogWriter 24 | { 25 | public: 26 | using WriteLogCallback = std::function; 27 | 28 | enum Level{ 29 | Debug = 0, 30 | Info, 31 | Warn, 32 | Error, 33 | Fatal, 34 | LevelSize 35 | }; 36 | static LogWriter* Instance(); 37 | static void ToHex(std::string& message, const char* data, unsigned int size); 38 | static void ToHex(std::string& message, std::string& data); 39 | 40 | public: 41 | void registerInterface(WriteLogCallback callback); 42 | 43 | void write(Level level, const std::string& data); 44 | void write(Level level, const std::string&& data); 45 | 46 | void fatal(const std::string& data); 47 | void fatal(const std::string&& data); 48 | void warn(const std::string& data); 49 | void warn(const std::string&& data); 50 | void error(const std::string& data); 51 | void error(const std::string&& data); 52 | void info(const std::string& data); 53 | void info(const std::string&& data); 54 | void debug(const std::string& data); 55 | void debug(const std::string&& data); 56 | 57 | void setLevel(int level); 58 | int getLevel(); 59 | const std::string& getLevelName(int level); 60 | 61 | private: 62 | LogWriter(); 63 | 64 | WriteLogCallback callback_; 65 | int level_; 66 | std::vector levelStr_; 67 | std::string nullLevel_; 68 | }; 69 | } 70 | #endif // ! UV_LOG_INTERFACE_H 71 | 72 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/PacketBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-10-24 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_PACKET_BUFFER_H 12 | #define UV_PACKET_BUFFER_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #include "LogWriter.h" 19 | #include "GlobalConfig.h" 20 | 21 | namespace uv 22 | { 23 | class Packet; 24 | class PacketBuffer 25 | { 26 | 27 | public: 28 | PacketBuffer() {} 29 | virtual ~PacketBuffer(){} 30 | 31 | virtual int append(const char* data, uint64_t size) = 0; 32 | virtual int readBufferN(std::string& data, uint64_t N) = 0; 33 | virtual int clearBufferN(uint64_t N) = 0; 34 | virtual int clear() = 0; 35 | virtual uint64_t readSize() = 0; 36 | 37 | int readString(std::string& out) 38 | { 39 | if (nullptr != GlobalConfig::ReadBufferString) 40 | return GlobalConfig::ReadBufferString(this, out); 41 | uv::LogWriter::Instance()->error("not defined packet parse func."); 42 | return -1; 43 | } 44 | 45 | int readPacket(Packet& out) 46 | { 47 | if (nullptr != GlobalConfig::ReadBufferPacket) 48 | return GlobalConfig::ReadBufferPacket(this, out); 49 | uv::LogWriter::Instance()->error("not defined packet parse func."); 50 | return -1; 51 | } 52 | 53 | int readGeneric(void* out) 54 | { 55 | if (nullptr != GlobalConfig::ReadBufferVoid) 56 | return GlobalConfig::ReadBufferVoid(this, out); 57 | uv::LogWriter::Instance()->error("not defined packet parse func."); 58 | return -1; 59 | } 60 | }; 61 | 62 | using PacketBufferPtr = std::shared_ptr; 63 | } 64 | 65 | #endif // ! UV_PACKET_BUFFER_H 66 | 67 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/Signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2018-8-23 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_SIGNAL_H 12 | #define UV_SIGNAL_H 13 | 14 | 15 | #include 16 | #include 17 | 18 | #include "EventLoop.h" 19 | 20 | namespace uv 21 | { 22 | 23 | using SignalHandle = std::function; 24 | 25 | class Signal 26 | { 27 | public: 28 | Signal(EventLoop* loop,int sig, SignalHandle handle = nullptr); 29 | void close(DefaultCallback callback); 30 | virtual ~Signal(); 31 | 32 | void setHandle(SignalHandle handle); 33 | static void Ignore(int sig); 34 | private: 35 | uv_signal_t* signal_; 36 | SignalHandle handle_; 37 | DefaultCallback closeCallback_; 38 | 39 | bool handle(int signum); 40 | void closeComplete(); 41 | static void onSignal(uv_signal_t* handle, int signum); 42 | }; 43 | 44 | } 45 | #endif 46 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/SocketAddr.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-12-31 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_SOCKET_ADDR_H 12 | #define UV_SOCKET_ADDR_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | namespace uv 19 | { 20 | using UVTcpPtr = std::shared_ptr; 21 | 22 | class SocketAddr 23 | { 24 | public: 25 | enum IPV 26 | { 27 | Ipv4 = 0, 28 | Ipv6 29 | }; 30 | 31 | SocketAddr(const std::string&& ip, unsigned short port, IPV ipv = Ipv4); 32 | SocketAddr(const std::string& ip, unsigned short port, IPV ipv = Ipv4); 33 | SocketAddr(const sockaddr* addr, IPV ipv = Ipv4); 34 | 35 | const sockaddr* Addr(); 36 | void toStr(std::string& str); 37 | std::string toStr(); 38 | 39 | IPV Ipv(); 40 | 41 | static void AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv = Ipv4); 42 | static uint16_t GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv = Ipv4); 43 | 44 | private: 45 | std::string ip_; 46 | unsigned short port_; 47 | const IPV ipv_; 48 | sockaddr_in ipv4_; 49 | sockaddr_in6 ipv6_; 50 | }; 51 | 52 | } 53 | #endif 54 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/TcpAccepter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-12-31 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_TCP_ACCEPTER_H 12 | #define UV_TCP_ACCEPTER_H 13 | 14 | 15 | #include 16 | 17 | #include "EventLoop.h" 18 | #include "SocketAddr.h" 19 | 20 | namespace uv 21 | { 22 | 23 | using NewConnectionCallback = std::function ; 24 | 25 | class TcpAccepter 26 | { 27 | public: 28 | TcpAccepter(EventLoop* loop, bool tcpNoDelay); 29 | 30 | virtual ~TcpAccepter(); 31 | 32 | int bind(SocketAddr& addr); 33 | int listen(); 34 | bool isListen(); 35 | bool isTcpNoDelay(); 36 | void setNewConnectinonCallback( NewConnectionCallback callback); 37 | 38 | EventLoop* Loop(); 39 | 40 | 41 | private: 42 | bool listened_; 43 | bool tcpNoDelay_; 44 | EventLoop* loop_; 45 | NewConnectionCallback callback_; 46 | 47 | uv_tcp_t server_; 48 | 49 | void onNewConnect(UVTcpPtr client); 50 | }; 51 | 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/TcpClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-12-31 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_TCP_CLIENT_H 12 | #define UV_TCP_CLIENT_H 13 | 14 | #include 15 | #include 16 | 17 | #include "TcpConnection.h" 18 | #include "SocketAddr.h" 19 | 20 | namespace uv 21 | { 22 | using NewMessageCallback = std::function ; 23 | 24 | class TcpClient 25 | { 26 | public: 27 | enum ConnectStatus 28 | { 29 | OnConnectSuccess, 30 | OnConnnectFail, 31 | OnConnnectClose 32 | }; 33 | using ConnectStatusCallback = std::function; 34 | public: 35 | TcpClient(EventLoop* loop,bool tcpNoDelay = true); 36 | virtual ~TcpClient(); 37 | 38 | bool isTcpNoDelay(); 39 | void setTcpNoDelay(bool isNoDelay); 40 | void connect(SocketAddr& addr); 41 | void close(std::function callback); 42 | 43 | void write(const char* buf, unsigned int size, AfterWriteCallback callback = nullptr); 44 | void writeInLoop(const char* buf, unsigned int size, AfterWriteCallback callback); 45 | 46 | void setConnectStatusCallback(ConnectStatusCallback callback); 47 | void setMessageCallback(NewMessageCallback callback); 48 | 49 | EventLoop* Loop(); 50 | PacketBufferPtr getCurrentBuf(); 51 | protected: 52 | EventLoop* loop_; 53 | 54 | void onConnect(bool successed); 55 | void onConnectClose(std::string& name); 56 | void onMessage(TcpConnectionPtr connection, const char* buf, ssize_t size); 57 | void afterConnectFail(); 58 | private: 59 | UVTcpPtr socket_; 60 | uv_connect_t* connect_; 61 | SocketAddr::IPV ipv; 62 | bool tcpNoDelay_; 63 | 64 | ConnectStatusCallback connectCallback_; 65 | NewMessageCallback onMessageCallback_; 66 | 67 | TcpConnectionPtr connection_; 68 | void update(); 69 | void runConnectCallback(TcpClient::ConnectStatus successed); 70 | void onClose(std::string& name); 71 | }; 72 | 73 | using TcpClientPtr = std::shared_ptr; 74 | } 75 | #endif 76 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/TcpServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-12-31 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_TCP_SERVER_H 12 | #define UV_TCP_SERVER_H 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "TcpAccepter.h" 20 | #include "TcpConnection.h" 21 | #include "TimerWheel.h" 22 | 23 | namespace uv 24 | { 25 | 26 | using OnConnectionStatusCallback = std::function )> ; 27 | 28 | //no thread safe. 29 | class TcpServer 30 | { 31 | public: 32 | static void SetBufferMode(uv::GlobalConfig::BufferMode mode); 33 | public: 34 | TcpServer(EventLoop* loop, bool tcpNoDelay = true); 35 | virtual ~TcpServer(); 36 | int bindAndListen(SocketAddr& addr); 37 | 38 | 39 | TcpConnectionPtr getConnnection(const std::string& name); 40 | void closeConnection(const std::string& name); 41 | 42 | void setNewConnectCallback(OnConnectionStatusCallback callback); 43 | void setConnectCloseCallback(OnConnectionStatusCallback callback); 44 | 45 | void setMessageCallback(OnMessageCallback callback); 46 | 47 | void write(TcpConnectionPtr connection,const char* buf,unsigned int size, AfterWriteCallback callback = nullptr); 48 | void write(std::string& name,const char* buf,unsigned int size, AfterWriteCallback callback =nullptr); 49 | void writeInLoop(TcpConnectionPtr connection,const char* buf,unsigned int size,AfterWriteCallback callback); 50 | void writeInLoop(std::string& name,const char* buf,unsigned int size,AfterWriteCallback callback); 51 | 52 | void setTimeout(unsigned int); 53 | private: 54 | void onAccept(EventLoop* loop, UVTcpPtr client); 55 | 56 | void addConnnection(std::string& name, TcpConnectionPtr connection); 57 | void removeConnnection(std::string& name); 58 | void onMessage(TcpConnectionPtr connection, const char* buf, ssize_t size); 59 | protected: 60 | EventLoop* loop_; 61 | private: 62 | bool tcpNoDelay_; 63 | SocketAddr::IPV ipv_; 64 | std::shared_ptr accetper_; 65 | std::map connnections_; 66 | 67 | 68 | OnMessageCallback onMessageCallback_; 69 | OnConnectionStatusCallback onNewConnectCallback_; 70 | OnConnectionStatusCallback onConnectCloseCallback_; 71 | TimerWheel timerWheel_; 72 | }; 73 | 74 | 75 | } 76 | #endif 77 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2019-9-11 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | #ifndef UV_TIMER_H 11 | #define UV_TIMER_H 12 | 13 | #include 14 | #include 15 | #include "EventLoop.h" 16 | namespace uv 17 | { 18 | 19 | class Timer 20 | { 21 | public: 22 | using TimerCallback = std::function; 23 | using TimerCloseComplete = std::function; 24 | 25 | Timer(EventLoop* loop, uint64_t timeout, uint64_t repeat, TimerCallback callback); 26 | virtual ~Timer(); 27 | 28 | void start(); 29 | void close(TimerCloseComplete callback); 30 | void setTimerRepeat(uint64_t ms); 31 | 32 | private: 33 | bool started_; 34 | uv_timer_t* handle_; 35 | uint64_t timeout_; 36 | uint64_t repeat_; 37 | TimerCallback callback_; 38 | 39 | TimerCloseComplete closeComplete_; 40 | 41 | private: 42 | void onTimeOut(); 43 | void closeComplete(); 44 | 45 | static void process(uv_timer_t* handle); 46 | 47 | }; 48 | 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/TimerWheel.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-7-21 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_TIMER_WHEEL_H 12 | #define UV_TIMER_WHEEL_H 13 | 14 | #include 15 | #include 16 | #include 17 | #include "Timer.h" 18 | #include "TcpConnection.h" 19 | 20 | namespace uv 21 | { 22 | 23 | //algorithm complexity o(1). 24 | 25 | template 26 | class TimerWheel 27 | { 28 | public: 29 | TimerWheel(EventLoop* loop); 30 | TimerWheel(EventLoop* loop,unsigned int timeout); 31 | void setTimeout(unsigned int seconds); 32 | int getTimeout(); 33 | void start(); 34 | void insert(std::shared_ptr value); 35 | 36 | private: 37 | unsigned int index_; 38 | unsigned int timeoutSec_; 39 | Timer timer_; 40 | 41 | std::vector>> wheel_; 42 | 43 | void wheelCallback(); 44 | 45 | }; 46 | 47 | template 48 | inline TimerWheel::TimerWheel(EventLoop* loop) 49 | :TimerWheel(loop, 0) 50 | { 51 | 52 | } 53 | 54 | template 55 | inline TimerWheel::TimerWheel(EventLoop* loop, unsigned int timeout) 56 | :index_(0), 57 | timeoutSec_(timeout), 58 | timer_(loop, 1000, 1000, std::bind(&TimerWheel::wheelCallback, this)) 59 | { 60 | 61 | } 62 | 63 | template 64 | inline void TimerWheel::setTimeout(unsigned int seconds) 65 | { 66 | timeoutSec_ = seconds; 67 | } 68 | 69 | template 70 | inline void TimerWheel::start() 71 | { 72 | if (timeoutSec_) 73 | { 74 | wheel_.resize(timeoutSec_); 75 | timer_.start(); 76 | } 77 | } 78 | 79 | template 80 | inline void TimerWheel::insert(std::shared_ptr value) 81 | { 82 | if (timeoutSec_ > 0 && nullptr != value) 83 | { 84 | wheel_[index_].insert(value); 85 | } 86 | } 87 | 88 | template 89 | inline int TimerWheel::getTimeout() 90 | { 91 | return timeoutSec_; 92 | } 93 | 94 | template 95 | inline void TimerWheel::wheelCallback() 96 | { 97 | if (!timeoutSec_) 98 | return; 99 | if (++index_ == timeoutSec_) 100 | { 101 | index_ = 0; 102 | } 103 | wheel_[index_].clear(); 104 | } 105 | } 106 | #endif 107 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/Udp.h: -------------------------------------------------------------------------------- 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 | #ifndef UV_UDP_H 12 | #define UV_UDP_H 13 | 14 | #include 15 | 16 | #include "EventLoop.h" 17 | #include "SocketAddr.h" 18 | 19 | namespace uv 20 | { 21 | 22 | 23 | class Udp 24 | { 25 | public: 26 | using OnUdpMessageCallback = std::function; 27 | 28 | Udp(EventLoop* loop); 29 | virtual ~Udp(); 30 | 31 | int bindAndRead(SocketAddr& addr); 32 | int send(SocketAddr& to, const char* buf, unsigned size); 33 | void close(DefaultCallback callback); 34 | void setMessageCallback(OnUdpMessageCallback callback); 35 | 36 | private: 37 | void onCloseCompleted(); 38 | void onMessage(const sockaddr* from, const char* data, unsigned size); 39 | 40 | static void onMesageReceive(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags); 41 | private: 42 | SocketAddr::IPV ipv_; 43 | uv_udp_t* handle_; 44 | DefaultCallback onClose_; 45 | 46 | OnUdpMessageCallback onMessageCallback_; 47 | }; 48 | 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/http/HttpClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-30 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_HTTP_CLIENT_H 12 | #define UV_HTTP_CLIENT_H 13 | 14 | #include "../TcpClient.h" 15 | #include "Request.h" 16 | #include "Response.h" 17 | 18 | namespace uv 19 | { 20 | namespace http 21 | { 22 | 23 | class HttpClient 24 | { 25 | public: 26 | using OnRespCallback = std::function; 27 | enum ReqResult 28 | { 29 | Success = 0, 30 | ConnectFail = 1, 31 | ParseFail = 2, 32 | Unknow = 3, 33 | }; 34 | 35 | public: 36 | HttpClient(EventLoop* loop); 37 | virtual ~HttpClient(); 38 | 39 | void Req(uv::SocketAddr& addr,Request& req); 40 | 41 | void setOnResp(OnRespCallback callback); 42 | 43 | private: 44 | TcpClient* client_; 45 | OnRespCallback callback_; 46 | Request req_; 47 | std::string buffer_; 48 | 49 | private: 50 | void onResp(int rst, Response* resp); 51 | void onConnectStatus(TcpClient::ConnectStatus status); 52 | void onMessage(const char* data, ssize_t size); 53 | bool isConnected; 54 | }; 55 | 56 | } 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/http/HttpCommon.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-30 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_HTTP_COMMON_H 12 | #define UV_HTTP_COMMON_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | namespace uv 19 | { 20 | namespace http 21 | { 22 | enum ParseResult 23 | { 24 | Success = 0, //解析成功 25 | Fail,//解析失败 26 | Error,//解析出错 27 | }; 28 | 29 | enum HttpVersion 30 | { 31 | Unknown, 32 | Http1_0, 33 | Http1_1, 34 | }; 35 | 36 | enum Methon 37 | { 38 | Get = 0, 39 | Post, 40 | Head, 41 | Put, 42 | Delete, 43 | Connect, 44 | Options, 45 | Trace, 46 | Patch, 47 | Invalid, 48 | }; 49 | 50 | const char Crlf[2] = {'\r','\n'}; 51 | 52 | extern std::string HttpVersionToStr(HttpVersion version); 53 | extern HttpVersion GetHttpVersion(std::string& str); 54 | extern int SplitHttpOfCRLF(std::string& str, std::vector& out, int defaultSize = 64); 55 | extern int SplitStrOfSpace(std::string& str, std::vector& out, int defaultSize = 4); 56 | extern uint64_t GetCommomStringLength(std::string& str1, std::string& str2); 57 | extern int AppendHead(std::string& str,std::map& heads); 58 | } 59 | } 60 | #endif 61 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/http/HttpServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-4-5 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_HTTP_SERVER_H 12 | #define UV_HTTP_SERVER_H 13 | 14 | #include "../TcpServer.h" 15 | #include "RadixTree.h" 16 | #include "Request.h" 17 | #include "Response.h" 18 | 19 | namespace uv 20 | { 21 | namespace http 22 | { 23 | 24 | class HttpServer : public uv::TcpServer 25 | { 26 | public: 27 | using OnHttpReqCallback = std::function; 28 | 29 | public: 30 | HttpServer(EventLoop* loop); 31 | void Get(std::string path, OnHttpReqCallback callback); 32 | void Post(std::string path, OnHttpReqCallback callback); 33 | void Head(std::string path, OnHttpReqCallback callback); 34 | void Put(std::string path, OnHttpReqCallback callback); 35 | void Delete(std::string path, OnHttpReqCallback callback); 36 | void Connect(std::string path, OnHttpReqCallback callback); 37 | void Options(std::string path, OnHttpReqCallback callback); 38 | void Trace(std::string path, OnHttpReqCallback callback); 39 | void Patch(std::string path, OnHttpReqCallback callback); 40 | 41 | private: 42 | RadixTree route_[Methon::Invalid]; 43 | 44 | void onMesage(TcpConnectionPtr conn, const char* data, ssize_t size); 45 | 46 | 47 | }; 48 | 49 | } 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/http/Request.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-30 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_HTTP_REQUEST_H 12 | #define UV_HTTP_REQUEST_H 13 | 14 | #include 15 | #include "HttpCommon.h" 16 | #include "../GlobalConfig.h" 17 | 18 | namespace uv 19 | { 20 | namespace http 21 | { 22 | 23 | class Request 24 | { 25 | public: 26 | Request(); 27 | Request(HttpVersion version, Methon methon); 28 | 29 | void swapContent(std::string& str); 30 | void swapContent(std::string&& str); 31 | std::string& getContent(); 32 | void appendHead(std::string& key, std::string& value); 33 | void appendHead(std::string&& key, std::string&& value); 34 | std::string getHead(std::string& key); 35 | std::string getHead(std::string&& key); 36 | void appendUrlParam(std::string& key, std::string& value); 37 | void appendUrlParam(std::string&& key, std::string&& value); 38 | std::string getUrlParam(std::string& key); 39 | std::string getUrlParam(std::string&& key); 40 | 41 | void setVersion(HttpVersion version); 42 | void setMethon(Methon methon); 43 | void setPath(std::string&& path); 44 | void setPath(std::string& path); 45 | std::string& getValue(); 46 | std::string& getPath(); 47 | HttpVersion getVersion(); 48 | Methon getMethon(); 49 | 50 | int pack(std::string& data); 51 | ParseResult unpack(std::string& data); 52 | ParseResult unpackAndCompleted(std::string& data); 53 | 54 | static std::string MethonToStr(Methon methon); 55 | static Methon StrToMethon(std::string& str); 56 | private: 57 | HttpVersion version_; 58 | Methon methon_; 59 | std::string path_; 60 | std::string value_; 61 | std::map urlParms_; 62 | std::map heads_; 63 | std::string content_; 64 | 65 | void packPathParam(std::string& path); 66 | int unpackUrl(std::string& str); 67 | int unpackPath(std::string& str); 68 | }; 69 | 70 | } 71 | } 72 | #endif 73 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/http/Response.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2020, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-3-30 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_HTTP_RESPONSE_H 12 | #define UV_HTTP_RESPONSE_H 13 | 14 | #include 15 | #include "HttpCommon.h" 16 | 17 | namespace uv 18 | { 19 | namespace http 20 | { 21 | 22 | class Response 23 | { 24 | public: 25 | enum StatusCode 26 | { 27 | OK =200, //客户端请求成功 28 | BadRequest = 400, //客户端请求有语法错误,不能被服务器所理解 29 | Unauthorized = 401,//请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用 30 | Forbidden = 403 , //服务器收到请求,但是拒绝提供服务 31 | NotFound = 404 , //请求资源不存在,eg:输入了错误的URL 32 | InternalServerError = 500 , //服务器发生不可预期的错误 33 | ServerUnavailable = 503, //服务器当前不能处理客户端的请求,一段时间后可能恢复正常 34 | }; 35 | 36 | public: 37 | Response(); 38 | Response(HttpVersion version, StatusCode code); 39 | 40 | void setVersion(HttpVersion version); 41 | void setStatus(StatusCode code,std::string info); 42 | HttpVersion getVersion(); 43 | StatusCode getStatusCode(); 44 | std::string& getStatusInfo(); 45 | 46 | void appendHead(std::string& key, std::string& value); 47 | void appendHead(std::string&& key, std::string&& value); 48 | 49 | std::string getHead(std::string& key); 50 | void swapContent(std::string& body); 51 | void swapContent(std::string&& body); 52 | std::string& getContent(); 53 | 54 | int pack(std::string& data); 55 | int unpack(std::string& data); 56 | ParseResult unpackAndCompleted(std::string& data); 57 | ParseResult isCompletedChunked(); 58 | 59 | private: 60 | HttpVersion version_; 61 | StatusCode statusCode_; 62 | std::string statusInfo_; 63 | std::map heads_; 64 | std::string content_; 65 | 66 | int parseStatus(std::string& str); 67 | }; 68 | 69 | } 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /uv-cpp/uv/include/uv11.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright © 2017-2019, orcaer@yeah.net All rights reserved. 3 | 4 | Author: orcaer@yeah.net 5 | 6 | Last modified: 2020-4-13 7 | 8 | Description: https://github.com/wlgq2/uv-cpp 9 | */ 10 | 11 | #ifndef UV_UV11_H 12 | #define UV_UV11_H 13 | 14 | #define UV_CPP_VERSION "1.4.8" 15 | 16 | #include "Async.h" 17 | #include "Signal.h" 18 | #include "TcpServer.h" 19 | #include "TcpClient.h" 20 | #include "LogWriter.h" 21 | #include "Packet.h" 22 | #include "Udp.h" 23 | #include "Idle.h" 24 | #include "GlobalConfig.h" 25 | #include "DnsGet.h" 26 | #include "http/HttpClient.h" 27 | #include "http/HttpServer.h" 28 | 29 | #endif 30 | --------------------------------------------------------------------------------