From ebc7de3a90defe83392be5ab95eb57335281445a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 4 Sep 2013 11:37:11 -0700 Subject: [PATCH] add methods for packing and unpacking of Assignments --- assignment-client/src/main.cpp | 4 +-- assignment-server/src/main.cpp | 8 ++++-- domain-server/src/main.cpp | 4 +-- libraries/shared/src/Assignment.cpp | 44 +++++++++++++++++++++++++++-- libraries/shared/src/Assignment.h | 7 +++-- 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/assignment-client/src/main.cpp b/assignment-client/src/main.cpp index 6143f531ca..b1b6303525 100644 --- a/assignment-client/src/main.cpp +++ b/assignment-client/src/main.cpp @@ -38,8 +38,8 @@ int main(int argc, char* const argv[]) { if (parameter == POOL_PARAMETER_CHAR) { // copy the passed assignment pool int poolLength = strlen(optarg); - assignmentPool = new char[poolLength]; - memcpy(assignmentPool, optarg, poolLength); + assignmentPool = new char[poolLength + sizeof(char)]; + memcpy(assignmentPool, optarg, poolLength + sizeof(char)); } } diff --git a/assignment-server/src/main.cpp b/assignment-server/src/main.cpp index 44b29e10b9..5330b175d2 100644 --- a/assignment-server/src/main.cpp +++ b/assignment-server/src/main.cpp @@ -49,12 +49,16 @@ int main(int argc, const char* argv[]) { serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, numSendHeaderBytes + sizeof(unsigned char)); } } else if (senderData[0] == PACKET_TYPE_CREATE_ASSIGNMENT && packetVersionMatch(senderData)) { + // memcpy the sent assignment + Assignment createdAssignment(senderData + numBytesForPacketHeader(PACKET_TYPE_CREATE_ASSIGNMENT)); + + qDebug() << "Received an assignment:" << createdAssignment; + // assignment server is on a public server // assume that the address we now have for the sender is the public address/port // and store that with the assignment so it can be given to the requestor later -// Assignment newAssignment((Assignment::Type) *(senderData + numBytesForPacketHeader(senderData)), senderSocket); -// qDebug() << "Received assignment of type " << newAssignment.getType() << "\n"; + // add this assignment to the queue // assignmentQueue.push(newAssignment); diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp index 0ea1e8e4e3..43e13636ed 100644 --- a/domain-server/src/main.cpp +++ b/domain-server/src/main.cpp @@ -96,8 +96,8 @@ int main(int argc, char* const argv[]) if (parameter == POOL_PARAMETER_CHAR) { // copy the passed assignment pool int poolLength = strlen(optarg); - assignmentPool = new char[poolLength]; - memcpy(assignmentPool, optarg, poolLength); + assignmentPool = new char[poolLength + sizeof(char)]; + memcpy(assignmentPool, optarg, poolLength + sizeof(char)); } } diff --git a/libraries/shared/src/Assignment.cpp b/libraries/shared/src/Assignment.cpp index 56c53d378d..00ab2878a2 100644 --- a/libraries/shared/src/Assignment.cpp +++ b/libraries/shared/src/Assignment.cpp @@ -10,8 +10,48 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool) : _direction(direction), - _type(type), - _pool(pool) + _type(type) { + // copy the pool, if we got one + if (pool) { + int poolLength = strlen(pool); + + // create the char array and make it large enough for string and null termination + _pool = new char[poolLength + sizeof(char)]; + memcpy(_pool, pool, poolLength + 1); + } +} + +Assignment::Assignment(const unsigned char* dataBuffer) { + int numBytesRead = 0; + memcpy(&_direction, dataBuffer, sizeof(Assignment::Direction)); + numBytesRead += sizeof(Assignment::Direction); + + memcpy(&_type, dataBuffer + numBytesRead, sizeof(Assignment::Type)); + numBytesRead += sizeof(Assignment::Type); + + int poolLength = strlen((const char*) dataBuffer + numBytesRead); + _pool = new char[poolLength + sizeof(char)]; + memcpy(_pool, dataBuffer + numBytesRead, poolLength + sizeof(char)); +} + +int Assignment::packAssignmentToBuffer(unsigned char* buffer) { + int numPackedBytes = 0; + + memcpy(buffer, &_direction, sizeof(_direction)); + numPackedBytes += sizeof(_direction); + + memcpy(buffer, &_type, sizeof(_type)); + numPackedBytes += sizeof(_type); + + strcpy((char *)buffer, _pool); + numPackedBytes += strlen(_pool) + sizeof(char); + + return numPackedBytes; +} + +QDebug operator<<(QDebug debug, const Assignment &assignment) { + debug << "T:" << assignment.getType() << "P" << assignment.getPool(); + return debug.nospace(); } \ No newline at end of file diff --git a/libraries/shared/src/Assignment.h b/libraries/shared/src/Assignment.h index 5e7b3cd5dd..c85d235547 100644 --- a/libraries/shared/src/Assignment.h +++ b/libraries/shared/src/Assignment.h @@ -25,17 +25,20 @@ public: }; Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool = NULL); + Assignment(const unsigned char* dataBuffer); Assignment::Direction getDirection() const { return _direction; } Assignment::Type getType() const { return _type; } const char* getPool() const { return _pool; } + int packAssignmentToBuffer(unsigned char* buffer); + private: Assignment::Direction _direction; Assignment::Type _type; - const char* _pool; + char* _pool; }; - +QDebug operator<<(QDebug debug, const Assignment &assignment); #endif /* defined(__hifi__Assignment__) */