Original Post
I've done a bit of network programming with sockets in other projects, and created a networking class that allows me to easily perform server/client networking tasks, however the one part I was never satisfied with was the memory management of incoming and outgoing packets. I currently use two std::vector buffers per socket; one for incoming packets (or partial packets) that have not yet been processed, and one for outgoing packets that have not yet been sent. When a packet in the buffer is processed, the remaining buffer was moved to the beginning of the vector to process the next packet. I used a std::vector for these so that memory allocation was taken care of, and by moving all remaining buffer data to the beginning of the vector after each packet was processed, I loosely ensured that the buffer wouldn't keep growing and being reallocated. But it feels flawed. For one, all that copying of packets within the vector surely can't be efficient when you're dealing with game environments and lots of connected players. And by relying on std::vectors for my packet buffers, I never know when a larger-than-normal packet will arrive and cause my vectors to be reallocated, possibly several times. Given that there is a continual stream of packets incoming and outgoing, what are some ways to minimize reallocation and memory copying? Or is it just a fact of life with networking? I don't really have any game examples of how this could be done, my method just doesn't seem like it would be the best there is. Any tips would be awesome.