├── .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