I’m now going to take a look at what’s required to implement a remote Proto.Actor and a “client” to interact with it. I’m going to stick to the standard Greet class, as shown in my previous post, but we’re going to generate the Greet class from a .proto (protocol buffers) definition file.
So before we get into the client/server code, let’s create the Greet.proto file, here’s it is
syntax = "proto3"; package messages; option csharp_namespace = "Messages"; message Greet { string Who = 1; }
The Greet message will generate a Greet type with a property name Who. So once you’ve created this file, you’ll need to use protoc.exe with the following command line
protoc.exe –csharp_out=. Greet.proto
This will generate the Greet.cs file, which I will not bother to show here.
Next up, we’ll reuse our GreetActor class from the previous post, recreated below
public class GreetingActor : IActor { public Task ReceiveAsync(IContext context) { var greet = context.Message as Greet; if (greet != null) { Console.WriteLine("[Thread {0}] Greeting {1}", Thread.CurrentThread.ManagedThreadId, greet.Who); } return Actor.Done; } }
As we know, strictly speaking Akka.net and also Proto.Actor do not adhere to a client/server architecture but remote objects are created in a peer to peer architecture. For the sake of this post we will create two separate projects, one named Server the other Client, just for simplicity.
Creating our server
- Create a Console application, named something like ProtoActorServer
- Using Nuget add the Proto.Actor and Proto.Remote packages
- Add or link to the Greet.cs file generated by protoc
- Add or link to the GreetActor.cs file from above
- In Program.cs add the following using clauses
using Messages; using Proto; using Proto.Remote;
- In the Main method add the following code
Serialization.RegisterFileDescriptor(GreetReflection.Descriptor); RemotingSystem.Start("127.0.0.1", 12000); var props = Actor.FromProducer(() => new GreetingActor()); Actor.SpawnNamed(props, "remote"); // just to keep the app. open Console.ReadLine();
Creating our client
As we know, this is going to basically be very similar to the server is set-up, but there are differences in the code, but I’ll still list all the steps for completeness
- Create a Console application, named something like ProtoActorClient
- Using Nuget add the Proto.Actor and Proto.Remote packages
- Add or link to the Greet.cs file generated by protoc
- Add or link to the GreetActor.cs file from above
- In Program.cs add the following using clauses
using Messages; using Proto; using Proto.Remote;
- In the Main method add the following code
Serialization.RegisterFileDescriptor(GreetReflection.Descriptor); RemotingSystem.Start("127.0.0.1", 12001); var remote = new PID("127.0.0.1:12000", "remote"); remote.Tell(new Greet { Who = "Hello World" });; // just to keep the app. open Console.ReadLine();
If you now run the ProtoActorServer and then the ProtoActorClient you should see the Greeting Hello World message displayed on the server console via it’s GreetingActor.
Disclaimer
At the time of writing this I’ve not yet seen documentation to suggest I’m doing everything 100% by the Proto.Actor book, so to speak, I’ve based this code on previous use of protobuf and Akka.net as well as a couple of examples from the Proto.Actor git repos. If I find I’m doing anything incorrectly, I will update this page.