From 374fffb1512484702ce7d97144797f18dd54fcba Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 8 Jul 2013 14:40:15 -0700 Subject: [PATCH] use packet version helpers for PACKET_TYPE_SET_VOXEL commands --- interface/src/Application.cpp | 23 ++++++++++++-------- libraries/shared/src/SharedUtil.cpp | 33 +++++++++++++++++------------ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ad309a9000..42a58abab7 100755 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1312,7 +1312,8 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { // if we have room don't have room in the buffer, then send the previously generated message first if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) { controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - args->bufferInUse = sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + sizeof(unsigned short int); // reset + args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + + sizeof(unsigned short int); // reset } // copy this node's code color details into our buffer. @@ -1375,10 +1376,12 @@ void Application::importVoxels() { // the server as an set voxel message, this will also rebase the voxels to the new location unsigned char* calculatedOctCode = NULL; SendVoxelsOperationArgs args; - args.messageBuffer[0] = PACKET_TYPE_SET_VOXEL_DESTRUCTIVE; - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE)]; + + int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); + + unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; *sequenceAt = 0; - args.bufferInUse = sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + sizeof(unsigned short int); // set to command + sequence + args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. @@ -1391,7 +1394,7 @@ void Application::importVoxels() { importVoxels.recurseTreeWithOperation(sendVoxelsOperation, &args); // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + sizeof(unsigned short int))) { + if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); } @@ -1426,10 +1429,12 @@ void Application::pasteVoxels() { // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to // the server as an set voxel message, this will also rebase the voxels to the new location SendVoxelsOperationArgs args; - args.messageBuffer[0] = PACKET_TYPE_SET_VOXEL_DESTRUCTIVE; - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE)]; + + int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); + + unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; *sequenceAt = 0; - args.bufferInUse = sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + sizeof(unsigned short int); // set to command + sequence + args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. If we don't have an actual selectedNode then use the mouseVoxel to create a @@ -1443,7 +1448,7 @@ void Application::pasteVoxels() { _clipboardTree.recurseTreeWithOperation(sendVoxelsOperation, &args); // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (sizeof(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) + sizeof(unsigned short int))) { + if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); } diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index bdc35ff3f5..4a5a78b4e3 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -11,17 +11,20 @@ #include #include #include + #ifdef _WIN32 #include "Syssocket.h" #endif -#include "Log.h" -#include "SharedUtil.h" -#include "OctalCode.h" #ifdef __APPLE__ #include #endif +#include "Log.h" +#include "OctalCode.h" +#include "PacketHeaders.h" +#include "SharedUtil.h" + long long usecTimestamp(timeval *time) { return (time->tv_sec * 1000000 + time->tv_usec); } @@ -209,13 +212,14 @@ bool createVoxelEditMessage(unsigned char command, short int sequence, int messageSize = MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE; // just a guess for now int actualMessageSize = 3; unsigned char* messageBuffer = new unsigned char[messageSize]; - unsigned short int* sequenceAt = (unsigned short int*)&messageBuffer[1]; - messageBuffer[0]=command; - *sequenceAt=sequence; - unsigned char* copyAt = &messageBuffer[3]; + int numBytesPacketHeader = populateTypeAndVersion(messageBuffer, command); + unsigned short int* sequenceAt = (unsigned short int*) &messageBuffer[numBytesPacketHeader]; + + *sequenceAt = sequence; + unsigned char* copyAt = &messageBuffer[numBytesPacketHeader + sizeof(sequence)]; - for (int i=0;i MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) { - success=false; + success = false; } else { // add it to our message - memcpy(copyAt,voxelData,lengthOfVoxelData); - copyAt+=lengthOfVoxelData; - actualMessageSize+=lengthOfVoxelData; + memcpy(copyAt, voxelData, lengthOfVoxelData); + copyAt += lengthOfVoxelData; + actualMessageSize += lengthOfVoxelData; } // cleanup delete[] voxelData; @@ -238,9 +242,10 @@ bool createVoxelEditMessage(unsigned char command, short int sequence, if (success) { // finally, copy the result to the output bufferOut = new unsigned char[actualMessageSize]; - sizeOut=actualMessageSize; - memcpy(bufferOut,messageBuffer,actualMessageSize); + sizeOut = actualMessageSize; + memcpy(bufferOut, messageBuffer, actualMessageSize); } + delete[] messageBuffer; // clean up our temporary buffer return success; }