《通过C#学Proto.Actor模型》之PID

时间:2021-12-17 17:31:31

PID对象是代表Actor对象的进程,是能过Actor.Spawn(props)获取的;它有什么成员呢?既然代理Actor,首先有一个ID,标识自己是谁,Actor在Spawn时可以命名这个ID,否则会自动生成。还有三种向邮箱发消息的方法,Tell(),Request(),RequestAsync(),还有一个发送系统消息(Started,Stoping,Stoped等)方法SendSystemMessage(),还有一个停止的方法Stop()。

 using Proto;
using System;
using System.Threading;
using System.Threading.Tasks; namespace P004_PID
{
class Program
{
static void Main(string[] args)
{
var props = Actor.FromProducer(() => new MyActor());
var pid = Actor.Spawn(props);
while (true)
{
Console.WriteLine("**************************************");
Console.WriteLine("1、单向请求Tell 2、单向请求Request 3、双向请求RequestAsync");
switch (Console.ReadLine())
{
case "":
Console.WriteLine("单向请求开始");
pid.Tell(new Request { Name = "单向请求 Tell", RequestType = "one-way", Time = DateTime.Now });
break;
case "":
Console.WriteLine("单向请求开始");
//无法接回应签,与官网说法不一
pid.Request(new Request { Name = "单向请求 Request", RequestType = "two-way-1", Time = DateTime.Now }, pid); break;
case "":
Console.WriteLine("双向请求开始");
var response = pid.RequestAsync<Response>(new Request { Name = "双向请求 RequestAsync", RequestType = "two-way-2", Time = DateTime.Now }).Result;
Console.WriteLine(response.Time + ":" + response.Name);
break;
}
Thread.Sleep();
}
}
} public class MyActor : IActor
{
public Task ReceiveAsync(IContext context)
{ if (context.Message is Request request)
{
switch (request.RequestType)
{
case "one-way"://context.Sender为null
Console.WriteLine("接收到:" + request.RequestType + "," + request.Time + ":" + request.Name);
break;
case "two-way-1"://context.Sender= context.Self为自己
Console.WriteLine("接收到:" + request.RequestType + "," + request.Time + ":" + request.Name);
context.Respond(new Response() { Time = DateTime.Now, Name = "服务端应答 two-way-1" });
break;
case "two-way-2"://context.Sender!= context.Self为新实例
Console.WriteLine("接收到:" + request.RequestType + "," + request.Time + ":" + request.Name);
context.Respond(new Response() { Time = DateTime.Now, Name = "服务端应答 two-way-2" });
break;
}
}
return Actor.Done;
}
} public class Request
{
public string Name
{ get; set; }
public string RequestType
{ get; set; }
public DateTime Time
{ get; set; }
} public class Response
{
public string Name
{ get; set; }
public DateTime Time
{ get; set; }
}
}

Actor中的Receive接到消息后,Context是中有两个PID对象,一个Self,一个Sender,Tell方法到达后,Sender对象为空;Request到达后,Sender=Self;而RequestAsync则Sender,Self都有对象,但不相同,这是一个区别。再有就是Tell和Request都是单向调用(我实测与官方文档说明有出入),RequestAsync是可以有返回值的,详见代码,如下是运行结果:

《通过C#学Proto.Actor模型》之PID