diff --git a/domain-server/src/main.cpp b/domain-server/src/main.cpp index fa6018b6c6..2ffad2f720 100644 --- a/domain-server/src/main.cpp +++ b/domain-server/src/main.cpp @@ -167,7 +167,10 @@ int main(int argc, const char* argv[]) { if (voxelServerConfig) { qDebug("Reading Voxel Server Configuration.\n"); qDebug() << " config: " << voxelServerConfig << "\n"; - voxelServerAssignment.setPayload((uchar*)voxelServerConfig, strlen(voxelServerConfig) + 1); + int payloadLength = strlen(voxelServerConfig) + sizeof(char); + uchar* payload = new uchar[payloadLength]; + memcpy(payload, voxelServerConfig, payloadLength); + voxelServerAssignment.setPayload(payload, payloadLength); } // construct a local socket to send with our created assignments to the global AS diff --git a/libraries/shared/src/Assignment.cpp b/libraries/shared/src/Assignment.cpp index e2608b230a..2fd5758007 100644 --- a/libraries/shared/src/Assignment.cpp +++ b/libraries/shared/src/Assignment.cpp @@ -85,17 +85,18 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) : qDebug("Received a socket that cannot be unpacked!\n"); } } - + if (numBytes > numBytesRead) { _numPayloadBytes = numBytes - numBytesRead; - memcpy(_payload, dataBuffer + numBytesRead, numBytes - numBytesRead); + _payload = new uchar[_numPayloadBytes]; // <<< the code was not allocating _payload + memcpy(_payload, dataBuffer + numBytesRead, _numPayloadBytes); } } Assignment::~Assignment() { delete _attachedPublicSocket; delete _attachedLocalSocket; - delete _payload; + delete _payload; // <<< this probably needs to be delete[]??? _numPayloadBytes = 0; } diff --git a/libraries/voxel-server-library/src/VoxelServer.cpp b/libraries/voxel-server-library/src/VoxelServer.cpp index 257f9b2640..9dafeafeee 100644 --- a/libraries/voxel-server-library/src/VoxelServer.cpp +++ b/libraries/voxel-server-library/src/VoxelServer.cpp @@ -11,6 +11,9 @@ #include #include +#include +#include + #include #include #include @@ -64,6 +67,7 @@ VoxelServer::VoxelServer(Assignment::Command command, Assignment::Location locat _jurisdictionSender = NULL; _voxelServerPacketProcessor = NULL; _voxelPersistThread = NULL; + _parsedArgV = NULL; } VoxelServer::VoxelServer(const unsigned char* dataBuffer, int numBytes) : Assignment(dataBuffer, numBytes), @@ -86,6 +90,16 @@ VoxelServer::VoxelServer(const unsigned char* dataBuffer, int numBytes) : Assign _jurisdictionSender = NULL; _voxelServerPacketProcessor = NULL; _voxelPersistThread = NULL; + _parsedArgV = NULL; +} + +VoxelServer::~VoxelServer() { + if (_parsedArgV) { + for (int i = 0; i < _argc; i++) { + delete[] _parsedArgV[i]; + } + delete[] _parsedArgV; + } } void VoxelServer::setArguments(int argc, char** argv) { @@ -93,6 +107,44 @@ void VoxelServer::setArguments(int argc, char** argv) { _argv = const_cast(argv); } +void VoxelServer::parsePayload() { + + //uchar* getPayload() { return _payload; } + //int getNumPayloadBytes() const { return _numPayloadBytes; } + + if (getNumPayloadBytes() > 0) { + QString config((const char*)getPayload()); + + QString delimiterPattern(" "); + QStringList configList = config.split(delimiterPattern); + + int argCount = configList.size() + 2; + + printf("VoxelServer::parsePayload()... argCount=%d\n",argCount); + + _parsedArgV = new char*[argCount]; + const char* dummy = "dummy"; + _parsedArgV[0] = new char[strlen(dummy)+1]; + strcpy(_parsedArgV[0], dummy); + + for (int i = 1; i < argCount-1; i++) { + QString configItem = configList.at(i-1); + _parsedArgV[i] = new char[configItem.length() + 1]; + strcpy(_parsedArgV[i], configItem.toLocal8Bit().constData()); + printf("VoxelServer::parsePayload()... _parsedArgV[%d]=%s\n", i, _parsedArgV[i]); + } + _parsedArgV[argCount - 1] = new char[strlen(dummy)+1]; + strcpy(_parsedArgV[argCount - 1], dummy); + + setArguments(argCount, _parsedArgV); + + for (int i = 0; i < argCount; i++) { + printf("_argv[%d]=%s\n", i, _argv[i]); + } + + } +} + void VoxelServer::setupStandAlone(const char* domain, int port) { NodeList::createInstance(NODE_TYPE_VOXEL_SERVER, port); @@ -114,6 +166,12 @@ void VoxelServer::setupStandAlone(const char* domain, int port) { //int main(int argc, const char * argv[]) { void VoxelServer::run() { + + // Now would be a good time to parse our arguments, if we got them as assignment + if (getNumPayloadBytes() > 0) { + parsePayload(); + } + pthread_mutex_init(&_treeLock, NULL); qInstallMessageHandler(sharedMessageHandler); @@ -143,7 +201,7 @@ void VoxelServer::run() { _jurisdiction = new JurisdictionMap(jurisdictionRoot, jurisdictionEndNodes); } } - + // should we send environments? Default is yes, but this command line suppresses sending const char* DUMP_VOXELS_ON_MOVE = "--dumpVoxelsOnMove"; _dumpVoxelsOnMove = cmdOptionExists(_argc, _argv, DUMP_VOXELS_ON_MOVE); diff --git a/libraries/voxel-server-library/src/VoxelServer.h b/libraries/voxel-server-library/src/VoxelServer.h index 6eb412ea68..8ab18d8c46 100644 --- a/libraries/voxel-server-library/src/VoxelServer.h +++ b/libraries/voxel-server-library/src/VoxelServer.h @@ -27,6 +27,8 @@ public: VoxelServer(const unsigned char* dataBuffer, int numBytes); + ~VoxelServer(); + /// runs the voxel server assignment void run(); @@ -61,6 +63,7 @@ public: private: int _argc; const char** _argv; + char** _parsedArgV; bool _dontKillOnMissingDomain; char _voxelPersistFilename[MAX_FILENAME_LENGTH]; @@ -84,6 +87,7 @@ private: NodeWatcher _nodeWatcher; // used to cleanup AGENT data when agents are killed + void parsePayload(); }; #endif // __voxel_server__VoxelServer__