項目地址:https://github.com/wlgq2/orcaios
當Actor對象被構造時,orca會爲其申請一個郵箱,用於消息通訊。當Actor對象被析構後,orca會回收該郵箱並用於其餘新申請Actor對象。orca經過索引來分配、回收郵箱。其中申請郵箱的時間複雜度爲o(1),回收的實現複雜度爲o(logN)。
圖:orca郵箱結構git
orca消息通訊接口很簡單,只須要對相應的Actor發送消息,並註冊該Actor的消息處理函數便可。
發送消息:github
void send(const MessagePack<MessageType>& message,Address& destination);
或者經過Actor的名字發送消息:函數
void send(const MessagePack<MessageType>& message, std::string& name);
註冊消息處理函數接口:oop
void registerHandler(ActorHandle handler);
一個完整的例子:this
#include <iostream> #include <orca/orca.h> REGISTER_MESSAGE_TYPE(std::string); class ActorTest :public orca::Actor { public: ActorTest(orca::Framework* framework,std::string name = "") :Actor(framework, name) { registerHandler(std::bind(&ActorTest::handle,this,std::placeholders::_1,std::placeholders::_2)); } void handle(orca::MessagePack& pack, orca::Address& from) { std::cout << (char*)(pack.enter()) << std::endl; } }; int main(int argc, char** args) { //actor framework. orca::Framework framework; //arctor object. ActorTest actor1(&framework); ActorTest actor2(&framework,"actor02"); //message pack. orca::MessagePack message; message.create("a message to actor2"); //actor1->actor2 send message. actor1.send(message,"actor02"); framework.loop(); }