-
Notifications
You must be signed in to change notification settings - Fork 27
Example application (non RPC)
c80k edited this page Oct 5, 2019
·
2 revisions
Assuming that you've installed the Cap'n Proto tool set and the C# code generator back end (as described in README.md), save the following schema definition into a file named AddressBook.capnp
:
@0xebf92c49b1687ddb;
struct Person {
id @0 :UInt32;
name @1 :Text;
email @2 :Text;
phones @3 :List(PhoneNumber);
struct PhoneNumber {
number @0 :Text;
type @1 :Type;
enum Type {
mobile @0;
home @1;
work @2;
}
}
employment :union {
unemployed @4 :Void;
employer @5 :Text;
school @6 :Text;
selfEmployed @7 :Void;
# We assume that a person is only one of these.
}
}
struct AddressBook {
people @0 :List(Person);
}
Open a command line with current directory set to the location of AddressBook.capnp
. Type:
capnp compile -ocsharp .\AddressBook.capnp
If everything worked as expected, you will find a generated AddressBook.capnp.cs
.
Create a new .NET Console application (either .NET Framework >= 4.7.2 or .NET Core >= 2.1) with VisualStudio. Open the NuGet package manager console and type:
Install-Package Capnp.Net.Runtime
Add AddressBook.capnp.cs
from the previous step to your project. The following Program.cs
snippet demonstrates how to create a domain class instance, serialize it to a stream, and how to deserialize it again:
using System;
using System.IO;
using Capnp;
using CapnpGen;
namespace MyAddressBook
{
class Program
{
static void Main(string[] args)
{
var addressBook = new AddressBook()
{
People = new Person[]
{
new Person()
{
Id = 1234,
Name = "Mark Harthaw",
Email = "mark.harthaw@soylent.corp",
Phones = new Person.PhoneNumber[]
{
new Person.PhoneNumber() { TheType = Person.PhoneNumber.Type.work, Number = "12345678" },
new Person.PhoneNumber() { TheType = Person.PhoneNumber.Type.mobile, Number = "87654321" }
},
Employment = new Person.employment()
{
Employer = "Soylent Corp"
}
},
new Person()
{
Id = 4321,
Name = "John Doe",
Employment = new Person.employment()
{
which = Person.employment.WHICH.SelfEmployed
}
}
}
};
var msg = MessageBuilder.Create();
var root = msg.BuildRoot<AddressBook.WRITER>();
addressBook.serialize(root);
var mems = new MemoryStream();
var pump = new FramePump(mems);
pump.Send(msg.Frame);
mems.Seek(0, SeekOrigin.Begin);
var frame = Framing.ReadSegments(mems);
var deserializer = DeserializerState.CreateRoot(frame);
var reader = new AddressBook.READER(deserializer);
foreach (var person in reader.People)
{
Console.WriteLine($"Id: {person.Id}");
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"E-mail: {person.Email}");
if (person.Phones?.Count > 0)
{
foreach (var phone in person.Phones)
{
Console.WriteLine($"{phone.TheType}: {phone.Number}");
}
}
Console.WriteLine($"Employment: {person.Employment.which}");
switch (person.Employment.which)
{
case Person.employment.WHICH.Employer:
Console.WriteLine($"Employer: {person.Employment.Employer}");
break;
case Person.employment.WHICH.School:
Console.WriteLine($"School: {person.Employment.School}");
break;
}
}
Console.ReadLine();
}
}
}
Enjoy!