make temp copy of NetworkPacket to protect against vector resizing

This commit is contained in:
ZappoMan 2013-11-05 14:50:49 -08:00
parent 406d42fe87
commit 318b9b1671
2 changed files with 6 additions and 10 deletions

View file

@ -20,7 +20,6 @@ NetworkPacket::NetworkPacket() {
NetworkPacket::~NetworkPacket() {
// nothing to do
printf("NetworkPacket::~NetworkPacket() this=%p this.getData()=%p\n", this, getData());
}
void NetworkPacket::copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength) {

View file

@ -31,16 +31,13 @@ bool ReceivedPacketProcessor::process() {
usleep(RECEIVED_THREAD_SLEEP_INTERVAL);
}
while (_packets.size() > 0) {
NetworkPacket& packet = _packets.front();
printf("ReceivedPacketProcessor::process() calling processPacket() NetworkPacket=%p packet.getData()=%p packet.getLength()=%ld\n",
&packet, packet.getData(), packet.getLength() );
processPacket(packet.getAddress(), packet.getData(), packet.getLength());
lock();
_packets.erase(_packets.begin());
unlock();
lock(); // lock to make sure nothing changes on us
NetworkPacket& packet = _packets.front(); // get the oldest packet
NetworkPacket temporary = packet; // make a copy of the packet in case the vector is resized on us
_packets.erase(_packets.begin()); // remove the oldest packet
unlock(); // let others add to the packets
processPacket(temporary.getAddress(), temporary.getData(), temporary.getLength()); // process our temporary copy
}
return isStillRunning(); // keep running till they terminate us
}