項目地址:https://github.com/wlgq2/orcaios
orca的網絡部分基於libuv,並對libuv作了一層C++封裝:https://github.com/wlgq2/libu...
orca的項目中編譯了libuv1.22.0的vs2017及gcc5.50版本,如需使用其餘版本,則需本身編譯相應版本。
對於orca來講,沒有客戶端及服務器的概念。orca中每個Framework都是一個端點(EndPoint)。端點與端點間會創建鏈接、發送識別協議及通訊。orca的每個端點都有一個惟一識別的id做爲通訊網絡中的識別依據。
對於使用來講,在進行用戶消息通訊前須要作如下設置:git
orca::core::FrameworkConfig類爲Framework的配置,原型以下:github
struct FrameworkConfig { FrameworkConfig() { reset(); } void reset() { id = 0; threadCount = 1; endPointAddress = nullptr; } uint32_t id; uint32_t threadCount; std::shared_ptr<struct EndPointAddress> endPointAddress; };
其中id爲framework指定id(用於通訊識別),threadCount爲framework開啓線程數,endPointAddress爲本地綁定地址,定義以下:緩存
struct EndPointAddress { enum IPV { Ipv4 = 0, Ipv6 }; EndPointAddress(std::string ip, uint16_t port, IPV ipv) { this->ip = ip; this->port = port; this->ipv = ipv; } std::string ip; uint16_t port; IPV ipv; };
分別爲ip、端口、ipv4/ipv6設置。
orca經過appendRemoteEndPoint接口添加遠程Framework。
原型爲:服務器
void appendRemoteEndPoint(struct EndPointAddress& addr);
void appendRemoteEndPoint(std::string ip, uint16_t port, EndPointAddress::IPV ipv = EndPointAddress::Ipv4);
orca的網絡消息通訊與本地消息接口同樣,只是在發送時須要指定遠程Farmework的識別ID。須要說明的是,若是在鏈接創建前,調用接口發送網絡消息,orca會把消息緩存到列隊裏等待鏈接創建併發送消息。若是在鏈接鏈接後消息發送失敗,orca會觸發錯誤回調函數。
一個完整的例子:網絡
#include <iostream> #include "MessageType.h" #define USE_END_POINT_1 1 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; #if !USE_END_POINT_1 //remote message return.遠程消息返回。 send(pack, from); #endif } }; int main(int argc, char** args) { //complie endpoint 1 編譯端點1. #if USE_END_POINT_1 //framework configs. orca::FrameworkConfig config; config.id = 1024; config.threadCount = 1; config.endPointAddress = std::make_shared<orca::EndPointAddress>("0.0.0.0",10001, orca::EndPointAddress::Ipv4); orca::Framework framework(config); //append remote framework. framework.appendRemoteEndPoint("127.0.0.1", 10002); ActorTest actor(&framework); //message pack. char data[] = "a message from remote actor"; orca::MessagePack message; message.create(data,sizeof(data)); //actor->actor2(remote) send message. actor.send(message,"actor2",1025); framework.loop(); #else //complie endpoint 2 編譯端點2. //framework configs. orca::FrameworkConfig config; config.id = 1025; config.threadCount = 1; config.endPointAddress = std::make_shared<orca::EndPointAddress>("0.0.0.0", 10002, orca::EndPointAddress::Ipv4); orca::Framework framework(config); //append remote framework. framework.appendRemoteEndPoint("127.0.0.1", 10001); ActorTest actor(&framework,"actor2"); framework.loop(); #endif }