《通过C#学Proto.Actor模型》之PID
程序员文章站
2022-05-18 23:02:16
PID对象是代表Actor对象的进程,是能过Actor.Spawn(props)获取的;它有什么成员呢?既然代理Actor,首先有一个ID,标识自己是谁,Actor在Spawn时可以命名这个ID,否则会自动生成。还有三种向邮箱发消息的方法,Tell(),Request(),RequestAsync(... ......
pid对象是代表actor对象的进程,是能过actor.spawn(props)获取的;它有什么成员呢?既然代理actor,首先有一个id,标识自己是谁,actor在spawn时可以命名这个id,否则会自动生成。还有三种向邮箱发消息的方法,tell(),request(),requestasync(),还有一个发送系统消息(started,stoping,stoped等)方法sendsystemmessage(),还有一个停止的方法stop()。
1 using proto; 2 using system; 3 using system.threading; 4 using system.threading.tasks; 5 6 namespace p004_pid 7 { 8 class program 9 { 10 static void main(string[] args) 11 { 12 var props = actor.fromproducer(() => new myactor()); 13 var pid = actor.spawn(props); 14 while (true) 15 { 16 console.writeline("**************************************"); 17 console.writeline("1、单向请求tell 2、单向请求request 3、双向请求requestasync"); 18 switch (console.readline()) 19 { 20 case "1": 21 console.writeline("单向请求开始"); 22 pid.tell(new request { name = "单向请求 tell", requesttype = "one-way", time = datetime.now }); 23 break; 24 case "2": 25 console.writeline("单向请求开始"); 26 //无法接回应签,与官网说法不一 27 pid.request(new request { name = "单向请求 request", requesttype = "two-way-1", time = datetime.now }, pid); 28 29 break; 30 case "3": 31 console.writeline("双向请求开始"); 32 var response = pid.requestasync<response>(new request { name = "双向请求 requestasync", requesttype = "two-way-2", time = datetime.now }).result; 33 console.writeline(response.time + ":" + response.name); 34 break; 35 } 36 thread.sleep(2000); 37 } 38 } 39 } 40 41 public class myactor : iactor 42 { 43 public task receiveasync(icontext context) 44 { 45 46 if (context.message is request request) 47 { 48 switch (request.requesttype) 49 { 50 case "one-way"://context.sender为null 51 console.writeline("接收到:" + request.requesttype + "," + request.time + ":" + request.name); 52 break; 53 case "two-way-1"://context.sender= context.self为自己 54 console.writeline("接收到:" + request.requesttype + "," + request.time + ":" + request.name); 55 context.respond(new response() { time = datetime.now, name = "服务端应答 two-way-1" }); 56 break; 57 case "two-way-2"://context.sender!= context.self为新实例 58 console.writeline("接收到:" + request.requesttype + "," + request.time + ":" + request.name); 59 context.respond(new response() { time = datetime.now, name = "服务端应答 two-way-2" }); 60 break; 61 } 62 } 63 return actor.done; 64 } 65 } 66 67 public class request 68 { 69 public string name 70 { get; set; } 71 public string requesttype 72 { get; set; } 73 public datetime time 74 { get; set; } 75 } 76 77 public class response 78 { 79 public string name 80 { get; set; } 81 public datetime time 82 { get; set; } 83 } 84 }
actor中的receive接到消息后,context是中有两个pid对象,一个self,一个sender,tell方法到达后,sender对象为空;request到达后,sender=self;而requestasync则sender,self都有对象,但不相同,这是一个区别。再有就是tell和request都是单向调用(我实测与官方文档说明有出入),requestasync是可以有返回值的,详见代码,如下是运行结果:
上一篇: 【leetcode 简单】第十五题 加一
下一篇: 谈论贪心
推荐阅读