add methods for packing and unpacking of Assignments

This commit is contained in:
Stephen Birarda 2013-09-04 11:37:11 -07:00
parent 9260bee653
commit ebc7de3a90
5 changed files with 57 additions and 10 deletions

View file

@ -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));
}
}

View file

@ -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);

View file

@ -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));
}
}

View file

@ -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();
}

View file

@ -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__) */