欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-13 22:51:33
Proto.Actor中提供了基于tcp/ip的通迅来实现Remote,可以通过其Remot实现对Actor的调用。 ......

proto.actor中提供了基于tcp/ip的通迅来实现remote,可以通过其remot实现对actor的调用。

先来看一个极简单片的远程调用。

码友看码:

引用nuget包

proto.actor

proto.remote

proto.serialization.wire

 

共享库:

 1 namespace p009_lib
 2 {
 3     public class hellorequest
 4     {
 5         public string message
 6         {
 7             get; set;
 8         }
 9     } 
10     public class helloresponse
11     {
12         public string message
13         { get; set; }
14     }
15 }

服务端:

 1 using p009_lib;
 2 using proto;
 3 using proto.remote;
 4 using proto.serialization.wire;
 5 using system;
 6  
 7 using system.threading;
 8 using system.threading.tasks;
 9  
10 namespace p009_server
11 {
12     class program
13     {
14         static void main(string[] args)
15         {
16             console.title = "服务端";
17             console.writeline("回车开始");
18             console.readline();
19             //设置序列化类型并注册
20             var wire = new wireserializer(new[] { typeof(hellorequest), typeof(helloresponse) });
21             serialization.registerserializer(wire, true);
22  
23             var props = actor.fromproducer(() => new helloquestactor());
24             //注册一个为hello类别的          
25             remote.registerknownkind("hello", props);
26             //服务端监控端口5001
27             remote.start("127.0.0.1", 5001);
28             console.writeline("服务端开始……");
29             console.readline();
30         }
31     }
32  
33     class helloquestactor : iactor
34     {
35         public task receiveasync(icontext context)
36         {
37             switch (context.message)
38             {
39                 case hellorequest msg:
40                     console.writeline(msg.message);
41                     context.respond(new helloresponse
42                     {
43                         message = $"回应:我是服务端【{datetime.now}】",
44                     });
45                     break;
46             }
47             return actor.done;
48         }
49     } 
50 }

客户端:

 1 using p009_lib;
 2 using proto;
 3 using proto.remote;
 4 using proto.serialization.wire;
 5 using system;
 6 using system.threading.tasks;
 7  
 8 namespace p009_client
 9 {
10     class program
11     {
12         static void main(string[] args)
13         {
14  
15             console.title = "客户端";
16             console.writeline("回车开始");
17             console.readline();
18             //设置序列化类型并注册
19             var wire = new wireserializer(new[] { typeof(hellorequest), typeof(helloresponse) });
20             serialization.registerserializer(wire, true);
21             //设置自己监控端口5002
22             remote.start("127.0.0.1", 5002);
23             //连接服务端5001
24             var pid = remote.spawnnamedasync("127.0.0.1:5001", "clientactor", "hello", timespan.fromseconds(50)).result.pid;
25             while (true)
26             {
27                 var res = pid.requestasync<helloresponse>(new hellorequest { message = $"请求:我是客户端 【{datetime.now}】" }).result;
28                 console.writeline(res.message);
29                 console.readline();
30             }
31         }
32     }
33 }

代码很简单,看注释就够了。

 

……