In C I was used to work with char* to which I mapped some structure representing a network packet. Then I would just assign the packet parameters and send via network:
UPDATED - I forget to add that I have some payload
struct packet {
uint64_t id __attribute__((packed));
uint64_t something __attribute__((packed));
short flags __attribute__((packed));
// here follows payload with variable length
};
...
char *payload = get_payload();
size_t payload_size = get_payload_size();//doesnt matter how
char *data = malloc(sizeof(struct packet)+payload_size);
struct packet *pkt = (struct packet *)data;
pkt->id = 123;
pkt->something = get_something();
pkt->flags = FLAG | FLAG2;
memcpy(data+sizeof(struct packet), payload, payload_size);
send(data, sizeof(struct packet)+payload_size);
Now I would like to implement this with some nice C++ features (if there are any which could do this in better way) Best change I can do in C++ is to use new instead of malloc.
char *data = new char[sizeof(struct packet)];//rest is same
Ok this is called object serialization.. I ve found couple of examples always using boost which I cannot use.. isnt there a native way in C++ to acomplish this?