I am having an issue which i am not sure how to solve efficiently. I made a server which can handle multiple clients through TCP sockets. These clients send data (multiple different objects!) to the server. The server stores the objects, and then re-sends it to all the interested clients.
Now, the client receives a Binary Serialized ''object'' and it has no clue whatsoever, what class this object is made from. A way to find out, is to just deseralize with all possible classes that i know it would send as a client. and check if it succeeded or not. However, one can imagine that when i have like 50 different classes to deserialize with later on, this would be VERY inefficient when i get a lot of messages over my socket.
Serialization happens in this code, which i run by a thread and currently only sends dummy data:
public static void NotifyService()
{
Stream stream = client.GetStream();
IFormatter formatter = new BinaryFormatter();
while (true)
{
try
{
Class1 klas = new Class1();
Class2 klasen = new Class2();
formatter.Serialize(stream, klas);
formatter.Serialize(stream, klasen);
}
catch (Exception e)
{
Console.WriteLine("Notifier got Exception, closing notify thread: " + e);
break;
}
}
stream.Close();
}
The question is now: What would be the most efficient way for the client to deserialize my object properly?
(There are other ways to do the sending an receiving stuff as is, using XML and other stuff, but i wanted to do objects with Binary serialization) Keep in mind that i am very new to the sockets/serialization in general.