diff --git a/libraries/shared/src/Assignment.cpp b/libraries/shared/src/Assignment.cpp index d794495c29..55837e4107 100644 --- a/libraries/shared/src/Assignment.cpp +++ b/libraries/shared/src/Assignment.cpp @@ -22,7 +22,9 @@ Assignment::Assignment(Assignment::Command command, Assignment::Type type, Assig _location(location), _attachedPublicSocket(NULL), _attachedLocalSocket(NULL), - _numberOfInstances(1) + _numberOfInstances(1), + _payload(NULL), + _numPayloadBytes(0) { // set the create time on this assignment gettimeofday(&_time, NULL); @@ -37,7 +39,9 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) : _location(GlobalLocation), _attachedPublicSocket(NULL), _attachedLocalSocket(NULL), - _numberOfInstances(1) + _numberOfInstances(1), + _payload(NULL), + _numPayloadBytes(0) { // set the create time on this assignment gettimeofday(&_time, NULL); @@ -65,23 +69,31 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) : if (numBytes > numBytesRead) { - sockaddr* newSocket = NULL; - - if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) { - // IPv4 address - newSocket = (sockaddr*) new sockaddr_in; - unpackSocket(dataBuffer + numBytesRead, newSocket); - } else { - // IPv6 address, or bad designator - qDebug("Received a socket that cannot be unpacked!\n"); + if (_command != Assignment::RequestCommand) { + sockaddr* newSocket = NULL; + + if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) { + // IPv4 address + newSocket = (sockaddr*) new sockaddr_in; + numBytesRead += unpackSocket(dataBuffer + numBytesRead, newSocket); + + if (_command == Assignment::CreateCommand) { + delete _attachedLocalSocket; + _attachedLocalSocket = newSocket; + } else { + delete _attachedPublicSocket; + _attachedPublicSocket = newSocket; + } + } else { + // IPv6 address, or bad designator + qDebug("Received a socket that cannot be unpacked!\n"); + } } - if (_command == Assignment::CreateCommand) { - delete _attachedLocalSocket; - _attachedLocalSocket = newSocket; - } else { - delete _attachedPublicSocket; - _attachedPublicSocket = newSocket; + _numPayloadBytes = numBytes - numBytesRead; + + if (_numPayloadBytes > 0) { + memcpy(_payload, dataBuffer + numBytesRead, numBytes - numBytesRead); } } } @@ -89,6 +101,25 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) : Assignment::~Assignment() { delete _attachedPublicSocket; delete _attachedLocalSocket; + delete _payload; + _numPayloadBytes = 0; +} + +const int MAX_PAYLOAD_BYTES = 1024; + +void Assignment::setPayload(uchar* payload, int numBytes) { + _payload = payload; + + if (numBytes > MAX_PAYLOAD_BYTES) { + qDebug("Set payload called with number of bytes greater than maximum (%d). Will only transfer %d bytes.\n", + MAX_PAYLOAD_BYTES, + MAX_PAYLOAD_BYTES); + + _numPayloadBytes = 1024; + } else { + _numPayloadBytes = numBytes; + } + } QString Assignment::getUUIDStringWithoutCurlyBraces() const { @@ -141,6 +172,11 @@ int Assignment::packToBuffer(unsigned char* buffer) { numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack); } + if (_numPayloadBytes) { + memcpy(buffer + numPackedBytes, _payload, _numPayloadBytes); + numPackedBytes += _numPayloadBytes; + } + return numPackedBytes; } diff --git a/libraries/shared/src/Assignment.h b/libraries/shared/src/Assignment.h index 59e2c45db7..4f46ea9e7e 100644 --- a/libraries/shared/src/Assignment.h +++ b/libraries/shared/src/Assignment.h @@ -56,6 +56,9 @@ public: Assignment::Location getLocation() const { return _location; } const timeval& getTime() const { return _time; } + uchar* getPayload() { return _payload; } + void setPayload(uchar *payload, int numBytes); + int getNumberOfInstances() const { return _numberOfInstances; } void setNumberOfInstances(int numberOfInstances) { _numberOfInstances = numberOfInstances; } void decrementNumberOfInstances() { --_numberOfInstances; } @@ -83,6 +86,8 @@ private: sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction timeval _time; /// time the assignment was created (set in constructor) int _numberOfInstances; /// the number of instances of this assignment + uchar *_payload; /// an optional payload attached to this assignment, a maximum for 1024 bytes will be packed + int _numPayloadBytes; /// number of bytes in the payload, up to a maximum of 1024 }; QDebug operator<<(QDebug debug, const Assignment &assignment);