mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 17:24:24 +02:00
add methods for packing and unpacking of Assignments
This commit is contained in:
parent
9260bee653
commit
ebc7de3a90
5 changed files with 57 additions and 10 deletions
|
@ -38,8 +38,8 @@ int main(int argc, char* const argv[]) {
|
||||||
if (parameter == POOL_PARAMETER_CHAR) {
|
if (parameter == POOL_PARAMETER_CHAR) {
|
||||||
// copy the passed assignment pool
|
// copy the passed assignment pool
|
||||||
int poolLength = strlen(optarg);
|
int poolLength = strlen(optarg);
|
||||||
assignmentPool = new char[poolLength];
|
assignmentPool = new char[poolLength + sizeof(char)];
|
||||||
memcpy(assignmentPool, optarg, poolLength);
|
memcpy(assignmentPool, optarg, poolLength + sizeof(char));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,12 +49,16 @@ int main(int argc, const char* argv[]) {
|
||||||
serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, numSendHeaderBytes + sizeof(unsigned char));
|
serverSocket.send((sockaddr*) &senderSocket, assignmentPacket, numSendHeaderBytes + sizeof(unsigned char));
|
||||||
}
|
}
|
||||||
} else if (senderData[0] == PACKET_TYPE_CREATE_ASSIGNMENT && packetVersionMatch(senderData)) {
|
} 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
|
// assignment server is on a public server
|
||||||
// assume that the address we now have for the sender is the public address/port
|
// 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
|
// 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
|
// add this assignment to the queue
|
||||||
// assignmentQueue.push(newAssignment);
|
// assignmentQueue.push(newAssignment);
|
||||||
|
|
|
@ -96,8 +96,8 @@ int main(int argc, char* const argv[])
|
||||||
if (parameter == POOL_PARAMETER_CHAR) {
|
if (parameter == POOL_PARAMETER_CHAR) {
|
||||||
// copy the passed assignment pool
|
// copy the passed assignment pool
|
||||||
int poolLength = strlen(optarg);
|
int poolLength = strlen(optarg);
|
||||||
assignmentPool = new char[poolLength];
|
assignmentPool = new char[poolLength + sizeof(char)];
|
||||||
memcpy(assignmentPool, optarg, poolLength);
|
memcpy(assignmentPool, optarg, poolLength + sizeof(char));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,48 @@
|
||||||
|
|
||||||
Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool) :
|
Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool) :
|
||||||
_direction(direction),
|
_direction(direction),
|
||||||
_type(type),
|
_type(type)
|
||||||
_pool(pool)
|
|
||||||
{
|
{
|
||||||
|
// 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();
|
||||||
}
|
}
|
|
@ -25,17 +25,20 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool = NULL);
|
Assignment(Assignment::Direction direction, Assignment::Type type, const char* pool = NULL);
|
||||||
|
Assignment(const unsigned char* dataBuffer);
|
||||||
|
|
||||||
Assignment::Direction getDirection() const { return _direction; }
|
Assignment::Direction getDirection() const { return _direction; }
|
||||||
Assignment::Type getType() const { return _type; }
|
Assignment::Type getType() const { return _type; }
|
||||||
const char* getPool() const { return _pool; }
|
const char* getPool() const { return _pool; }
|
||||||
|
|
||||||
|
int packAssignmentToBuffer(unsigned char* buffer);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Assignment::Direction _direction;
|
Assignment::Direction _direction;
|
||||||
Assignment::Type _type;
|
Assignment::Type _type;
|
||||||
const char* _pool;
|
char* _pool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QDebug operator<<(QDebug debug, const Assignment &assignment);
|
||||||
|
|
||||||
#endif /* defined(__hifi__Assignment__) */
|
#endif /* defined(__hifi__Assignment__) */
|
||||||
|
|
Loading…
Reference in a new issue