Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ design - Network packets and serialization
    primarykey
    data
    text
    <p>I have, for my game, a Packet class, which represents network packet and consists basically of an array of data, and some pure virtual functions</p> <p>I would then like to have classes deriving from Packet, for example: StatePacket, PauseRequestPacket, etc. Each one of these sub-classes would implement the virtual functions, Handle(), which would be called by the networking engine when one of these packets is received so that it can do it's job, several get/set functions which would read and set fields in the array of data.</p> <p>So I have two problems:</p> <ol> <li>The (abstract) Packet class would need to be copyable and assignable, but without slicing, keeping all the fields of the derived class. It may even be possible that the derived class will have no extra fields, only function, which would work with the array on the base class. How can I achieve that?</li> <li>When serializing, I would give each sub-class an unique numeric ID, and then write it to the stream before the sub-class' own serialization. But for unserialization, how would I map the read ID to the appropriate sub-class to instanciate it?</li> </ol> <p>If anyone want's any clarifications, just ask.</p> <p>-- Thank you</p> <hr> <p><strong>Edit:</strong> I'm not quite happy with it, but that's what I managed:</p> <p>Packet.h: <a href="http://pastebin.com/f512e52f1" rel="nofollow noreferrer">http://pastebin.com/f512e52f1</a><br> Packet.cpp: <a href="http://pastebin.com/f5d535d19" rel="nofollow noreferrer">http://pastebin.com/f5d535d19</a><br> PacketFactory.h: <a href="http://pastebin.com/f29b7d637" rel="nofollow noreferrer">http://pastebin.com/f29b7d637</a><br> PacketFactory.cpp: <a href="http://pastebin.com/f689edd9b" rel="nofollow noreferrer">http://pastebin.com/f689edd9b</a><br> PacketAcknowledge.h: <a href="http://pastebin.com/f50f13d6f" rel="nofollow noreferrer">http://pastebin.com/f50f13d6f</a><br> PacketAcknowledge.cpp: <a href="http://pastebin.com/f62d34eef" rel="nofollow noreferrer">http://pastebin.com/f62d34eef</a> </p> <p>If someone has the time to look at it and suggest any improvements, I'd be thankful.</p> <hr> <p>Yes, I'm aware of the factory pattern, but how would I code it to construct each class? A giant switch statement? That would also duplicade the ID for each class (once in the factory and one in the serializator), which I'd like to avoid.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload