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

Props是配置Actor和实例化Actor,那实例化后,就应该访问了,Props.Actor提供了Actor.Spawn(),Actor.SpawnPrefix(),Actor.SpawnNamed()三个方法,来获取Actor实例,需要注意的是,这些方法返回的并不是真正的Actor对象,而是一个ProgressID,一个代表Actor对象的进程ID,缩写PID。

 上代码:

 1 using Proto;
 2 using System;
 3 using System.Threading;
 4 using System.Threading.Tasks;
 5 
 6 namespace P003_SpawningActors
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             var props = Actor.FromProducer(() => new MyActor());
13 
14             //产生一个自定义名称的PID
15             var pid1 = Actor.Spawn(props);
16             pid1.Tell(new MyEntity { ID = 1 });
17             Thread.Sleep(1000);
18             Console.WriteLine("------------------------------------------");
19             //产生一个有gsw前缀,跟自动生成的名称的PID
20             var pid2 = Actor.SpawnPrefix(props, "gsw");
21             pid2.Tell(new MyEntity { ID = 2 });
22             Thread.Sleep(1000);
23             Console.WriteLine("------------------------------------------");
24             //产生一个名称为gswpid的PID
25             var pid3 = Actor.SpawnNamed(props, "gswpid");
26             pid3.Tell(new MyEntity { ID = 3 });
27             Console.ReadLine();
28         }
29     }
30 
31     public class MyActor : IActor
32     {
33         public Task ReceiveAsync(IContext context)
34         {
35             if (context.Message is MyEntity myEntity)
36             {
37                 Console.WriteLine($"父 SelfID={context.Self.Id}   myEntity.ID={myEntity.ID}");           
38 
39                 var cldProps = Actor.FromProducer(() => new MyChildActor());
40                 //第一个子Actor
41                 var pidCld1 = context.Spawn(cldProps);
42                 pidCld1.Tell(new MyChildEntity { Message = "1 message,myEntity.ID=" + myEntity.ID });
43                 //第二个子Actor
44                 var pidCld2 = context.SpawnPrefix(cldProps, "gswCld");
45                 pidCld2.Tell(new MyChildEntity { Message = "2 message,myEntity.ID=" + myEntity.ID });
46                 //第三个子Actor
47                 var pidCld3 = context.SpawnNamed(cldProps, "gswCldPid");
48                 pidCld3.Tell(new MyChildEntity { ID = 3, Message = "3 message,myEntity.ID=" + myEntity.ID });
49             }
50             return Actor.Done;
51         }
52     }
53     public class MyChildActor : IActor
54     {
55         public Task ReceiveAsync(IContext context)
56         {
57             if (context.Message is MyChildEntity myChildEntity)
58             {              
59                 Console.WriteLine($"子    SelfID={context.Self.Id}     Message={myChildEntity.Message}");          
60             }
61             return Actor.Done;
62         }
63     }
64     public class MyEntity
65     {
66         public int ID { get; set; }
67     }
68     public class MyChildEntity
69     {
70         public string Message { get; set; }
71         public int ID { get; set; }
72     }
73 }

这个例子很简单,说明了三个Spawn的使用方式和Self.Id的特征,包括产生子Actor后,子Actor的Self.Id会带有父ID,结果如下:

《通过C#学Proto.Actor模型》之Spawning_第1张图片

你可能感兴趣的:(《通过C#学Proto.Actor模型》之Spawning)