From 7964f360ab5ad1aa43b87ae5164433fec515dd7e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 7 Oct 2013 17:52:21 -0700 Subject: [PATCH] add method to DataServerClient to resend unmatched packets --- interface/src/DataServerClient.cpp | 33 ++++++++++++++++++++---------- interface/src/DataServerClient.h | 5 +++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/interface/src/DataServerClient.cpp b/interface/src/DataServerClient.cpp index ba748b401b..e7d5287d4c 100644 --- a/interface/src/DataServerClient.cpp +++ b/interface/src/DataServerClient.cpp @@ -17,7 +17,7 @@ #include "DataServerClient.h" QString DataServerClient::_clientUsername; -std::vector DataServerClient::_unmatchedPackets; +std::map DataServerClient::_unmatchedPackets; const char DATA_SERVER_HOSTNAME[] = "127.0.0.1"; const unsigned short DATA_SERVER_PORT = 3282; @@ -46,7 +46,7 @@ void DataServerClient::putValueForKey(const char* key, const char* value) { putPacket[numPacketBytes++] = '\0'; // add the putPacket to our vector of unconfirmed packets, will be deleted once put is confirmed - _unmatchedPackets.push_back(putPacket); + _unmatchedPackets.insert(std::pair(putPacket, numPacketBytes)); // send this put request to the data server NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, putPacket, numPacketBytes); @@ -82,20 +82,20 @@ void DataServerClient::getValueForKeyAndUserString(const char* key, QString& use void DataServerClient::processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes) { - for (std::vector::iterator unconfirmedPacket = _unmatchedPackets.begin(); - unconfirmedPacket != _unmatchedPackets.end(); - ++unconfirmedPacket) { - if (memcmp(*unconfirmedPacket + sizeof(PACKET_TYPE), + for (std::map::iterator mapIterator = _unmatchedPackets.begin(); + mapIterator != _unmatchedPackets.end(); + ++mapIterator) { + if (memcmp(mapIterator->first + sizeof(PACKET_TYPE), packetData + sizeof(PACKET_TYPE), numPacketBytes - sizeof(PACKET_TYPE)) == 0) { - // this is a match - remove the confirmed packet from the vector so it isn't sent back out - _unmatchedPackets.erase(unconfirmedPacket); + + // this is a match - remove the confirmed packet from the vector and delete associated member + // so it isn't sent back out + delete[] mapIterator->first; + _unmatchedPackets.erase(mapIterator); // we've matched the packet - bail out break; - } else { - // no match, just push the iterator - unconfirmedPacket++; } } } @@ -143,3 +143,14 @@ void DataServerClient::processMessageFromDataServer(unsigned char* packetData, i break; } } + +void DataServerClient::resendUnmatchedPackets() { + for (std::map::iterator mapIterator = _unmatchedPackets.begin(); + mapIterator != _unmatchedPackets.end(); + ++mapIterator) { + // send the unmatched packet to the data server + NodeList::getInstance()->getNodeSocket()->send((sockaddr*) &DATA_SERVER_SOCKET, + mapIterator->first, + mapIterator->second); + } +} diff --git a/interface/src/DataServerClient.h b/interface/src/DataServerClient.h index 594f4b5692..e7a7b26beb 100644 --- a/interface/src/DataServerClient.h +++ b/interface/src/DataServerClient.h @@ -9,7 +9,7 @@ #ifndef __hifi__DataServerClient__ #define __hifi__DataServerClient__ -#include +#include #include @@ -22,12 +22,13 @@ public: static void processConfirmFromDataServer(unsigned char* packetData, int numPacketBytes); static void processSendFromDataServer(unsigned char* packetData, int numPacketBytes); static void processMessageFromDataServer(unsigned char* packetData, int numPacketBytes); + static void resendUnmatchedPackets(); static void setClientUsername(const QString& clientUsername) { _clientUsername = clientUsername; } static QString& setClientUsername() { return _clientUsername; } private: static QString _clientUsername; - static std::vector _unmatchedPackets; + static std::map _unmatchedPackets; }; namespace DataServerKey {