I've used UDP before and it was convenient as it receives packets. However I've experienced some issues (mostly out of order packets), so I thought TCP might be better solution than creating own system to fix UDP disadvantages. Sadly TCP came with own disadvantage: it receives bytes, not packets, so I'm looking into a way to put bytes back into packets, otherwise data cannot be processed. I have one idea in my mind, but not sure if this is correct way, so I thought could seek some help here.
Here's my idea:
// general packet structure
class Packet {
union {
short size;
char data[1024]; // first two bytes contain whole packet's size
};
void send() {
send(socket, data, size);
}
};
// sender
Packet p;
p << 0x0057;
p << player.name;
p.send();
// receiver
char data[1024];
short totalSize = 0;
while(true) {
totalSize += recv(data + totalSize);
if(totalSize >= 2)
if(totalSize >= (short)data) { // comparing with first 2 bytes (pseudo-cast)
// if we received more data than packet's size we can process it
threadPool.queueForProcessing(data, (short)data);
// copy remaining bytes to the beginning of data
}
}
Thank you in advance.
Edited by Ripiz, 29 May 2012 - 09:19 AM.






