1

I'm writing an application that uses protocol buffers C #. I need some example of how to send and receive data via TCP. Especially, how to determine which type of package has arrived. I have classes:

[ProtoContract]
class Package1
{
//fields
} 
[ProtoContract]
class Package2
{
//fields 
}

and methods for writing to stream

private static void SendPackage(Stream stream, Package1 package)
{
   Serializer.Serialize<Package1>(stream, package);
}

private static void SendPackage(Stream stream, Package2 package)
{
   Serializer.Serialize<Package2>(stream, package);
}

Now, how to determine which type of package has arrived?

using (NetworkStream stream = client.GetStream())
{
    switch(packageType)
    {
       //deserialize package
    }
}

P.S sorry for my bad english.

Dmitry
  • 11
  • 4

1 Answers1

1

There is a Serializer.NonGeneric method which you can use to identify the object. See this question and answer How can I send multiple types of objects across Protobuf?

Community
  • 1
  • 1
Kay Tsar
  • 1,428
  • 11
  • 14