For a mmorpg server, which design should I use?
Assume 300 players spawning in same place, they seeing each other. For this we have to send 300 * ( 6(header) + 70(spawn packetpayload) ) bytes of packet (22,2 KB) each user. In total 6,5 MB.
1.
struct sendbuffer
{
char buffer[4096]
int offset
int size
};
(Logic is not written completely or maybe erroneous but you will understand)
void writepacket(void *buf, int size)
{
//check against overflow
memcpy(sendbuffer.buffer + offset, buf, size);
sendbuffer.size += size;
}
When sending,
call writepacket(&packet1, 10)
call writepacket(&packet2, 55)
call WSASend(sendbuffer.buffer, size)
on wsasend io operation completed,
offset += transferredBytes (parameter that GetQueuedCompletionStatus gave us)
call WSASend(sendbuffer.buffer + offset, size - offset)
2.
picking up a fixed-size pool allocator
when building packets, just allocate from pool and write in it. There is no copy but it will cost a lot of memory.
And queue these allocated packets for user, then send it one by one when WSASend calls completed send next one.
3.
Having multiple outstanding calls of WSASend for each packet?
or any better implementation?