1

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);
            }
        }
    }
cruny
  • 13
  • 5
  • 1
    How about using a text based protocol like xml or json, so that you can easily define the type in it by simple inspection. Maybe something like this: http://stackoverflow.com/a/21510978/932418 – L.B Oct 28 '16 at 18:06
  • my question was about using interfaces its not like i need a solution , i need to understand using it that way – cruny Oct 28 '16 at 18:09
  • OK, I am waiting your new questions after you've solved your problem with interfaces which are always problems when serializing/deserializing of objects :) – L.B Oct 28 '16 at 18:11
  • I guess my question wasn't too good , i' ll edit it – cruny Oct 28 '16 at 18:15
  • cruny, The idea is simple. if share a common assembly between client & server, send an info about the type you serialized and the receiver can control the deserialization process to return the correct type (also assuming that type has a default constructor) then is possible. Otherwise receiving end have to guess.... Now show your serialization/deserialization code so that we can help you.... – L.B Oct 28 '16 at 18:22
  • BTW: Don't forget you send *data* (using serialization) to the server not *code*.... – L.B Oct 28 '16 at 18:25
  • @L.B i found this Method on a project in github something like that i didnt understand fully – cruny Oct 28 '16 at 18:30
  • curry, it just simply serializes some object and enqueues it for sending.... Not related with your original question about interfaces.... I think you should read some more docs because you don't know what is going on under the hood :( My thinking on problems like this is: *All you can send from client to server is just a sequence of bytes... So since there isn't a magic here, how can i recreate the original class at receiver end* .... – L.B Oct 28 '16 at 18:40
  • @L.B Yes sir thanks ! , M trying not to be so confused but , lets say we have some packets Server Packets (Msg, Order,Dosomework) those are classes that inherite from the interface Packet , i want to send for example Message Packet , i seralize it and send it in the Client side i have to check what the received packet type is – cruny Oct 28 '16 at 18:49
  • 2
    Please create a [*create a Minimal, Complete, and Verifiable example*](http://stackoverflow.com/help/mcve) otherwise it is too broad to answer here. See also http://sscce.org/ – L.B Oct 28 '16 at 18:55

0 Answers0