i m trying to get a bit deeper into sockets applications , and i'm trying to create a server/Client application with Packets as a Serialized Classes
Example :
[Serializable]
class Msg
{
public String Cmd { get; set; }
public Msg(String M)
{
this.Cmd = M;
}
}
I m trying to make it easy to read and cleaner using interfaces
public interface Packet
{
void Execute();
}
Packets :
[Serializable]
class Msg : Packet
{
public String Cmd { get; set; }
public Msg(String M)
{
this.Cmd = M;
}
public void Execute()
{
Sender.Send(this);
}
}
My question is while Deserializing the Packet is it possible to Deserialize to know the type of the packet for example :
Packet p = (Packet)Data; // data is the packet sent by the Client Desiralized
var type = p.GetType();
then use it like :
if (type== Typeof(Packet.Msg))
{
// handle data
}
is it possible to get packet type across Packet Interface?
EDIT :
cause i noticed in .NET in System.Type reference there is GetType Method Return the Type of the Class so i was a was trying to know if it can returns the type of the class (packet)
Something Like :
public void Send<T>(T packet) where T : IPacket
{
if (!Connected || packet == null) return;
lock (_sendBuffers)
{
using (MemoryStream ms = new MemoryStream())
{
try
{
_parentServer.Serializer.Serialize(ms, packet);
}
catch (Exception)
{
Disconnect();
return;
}
byte[] payload = ms.ToArray();
_sendBuffers.Enqueue(payload);
OnClientWrite(packet, payload.LongLength, payload);
lock (_sendingPacketsLock)
{
if (_sendingPackets) return;
_sendingPackets = true;
}
ThreadPool.QueueUserWorkItem(Send);
}
}
}