CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

时间:2024-01-10 08:34:50

e). 消息延迟发送(和前面没太大区别直接上代码)

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf; behavior fun(event_based_actor* self){
return {
[self](const string& str){
aout(self)<<str<<endl;
auto t2 = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
cout<<"dalay time :"<<t2<<endl;
self->quit();
}
};
} void fun1(event_based_actor* self, actor buddy){
self->delayed_send(buddy, std::chrono::seconds(), "hi!");
} int main(){
auto actor1 = spawn(fun);
auto t1 = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
cout<<"before delayed_send :"<<t1<<endl;
auto actor2 = spawn(fun1,actor1); caf::await_all_actors_done();
shutdown();
return ;
}

结果为

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

f). 消息前转(消息转发)forward.

贴上代码

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf; behavior fun2(event_based_actor* self){
return {
[self](const string& str){
aout(self)<<"C get message return to A"<<endl;
aout(self)<<"C's address is :"<<self->address()<<endl;
return "hello, A";
self->quit();
}
};
} behavior fun1(event_based_actor* self, const actor &buddy){
return {
[=](const string& str){
aout(self)<<"B get message forward to C"<<endl;
self->forward_to(buddy);
self->quit();
}
};
} void fun(event_based_actor* self, const actor &buddy){
self->sync_send(buddy,"hi!").then(
[=](const string& str) {
aout(self)<<str<<endl;
aout(self)<<"A think last_sender is :"<<self->last_sender()<<endl;
}
);
aout(self)<<"A send to B!"<<endl;
} int main(){
auto actorC = spawn(fun2);
auto actorB = spawn(fun1,actorC);
auto actorA = spawn(fun,actorB);
caf::await_all_actors_done();
shutdown();
return ;
}

结果为

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

使用了消息优先级,去caf代码里看了发现只有两种级别就是(hige 和normal)

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)然后在用户手册上给出的示例代码是

using a_atom = atom_constant<atom("a")>;
using b_atom = atom_constant<atom("b")>; behavior testee(event_based_actor* self) {
// send 'b' with normal priority
self->send(self, b_atom::value);
// send 'a' with high priority
self->send(message_priority::high, self, a_atom::value);
// terminate after receiving a 'b'
return {
[=](b_atom) {
aout(self) << "received 'b' => quit" << endl;
self->quit();
},
[=](a_atom) {
aout(self) << "received 'a'" << endl;
},
};
} int main() {
// will print "received 'b' => quit"
spawn(testee);
await_all_actors_done();
// will print "received 'a'" and then "received 'b' => quit"
spawn<priority_aware>(testee);
await_all_actors_done();
shutdown();
}

结果是

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)很好理解,因为第一个actor是不认识具有优先级的message的,只受到b消息,第二个就先收到a再收到b,这里有个细节就是当B本来因该是先收到的,但却变成了后收到,和发送的次序无关了。我甚至在两个发送语句之间加usleep(1000000) 都会先收到A后再输出B,这个Actor 感觉自己知道要收到priority 消息。

我还是不罢休,把代码改了改变成

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

结果为

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

我彻底服了CAF 太牛了把,当Actor不识别优先级时,它会把所有消息当成normal(正常的),当它知道时,就会把所有的优先级消息都接受完了再接受normal的。后来我改写了一些奇怪的语句,这CAF还是要我挺佩服得。

到这里消息都讲完了,之后开始CAF序列化之路。。。