use packet version helpers for PACKET_TYPE_SET_VOXEL commands

This commit is contained in:
Stephen Birarda 2013-07-08 14:40:15 -07:00
parent b816761a74
commit 374fffb151
2 changed files with 33 additions and 23 deletions

View file

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

View file

@ -11,17 +11,20 @@
#include <cstring>
#include <cctype>
#include <time.h>
#ifdef _WIN32
#include "Syssocket.h"
#endif
#include "Log.h"
#include "SharedUtil.h"
#include "OctalCode.h"
#ifdef __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#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<voxelCount && success;i++) {
for (int i = 0; i < voxelCount && success; i++) {
// get the coded voxel
unsigned char* voxelData = pointToVoxel(voxelDetails[i].x,voxelDetails[i].y,voxelDetails[i].z,
voxelDetails[i].s,voxelDetails[i].red,voxelDetails[i].green,voxelDetails[i].blue);
@ -224,12 +228,12 @@ bool createVoxelEditMessage(unsigned char command, short int sequence,
// make sure we have room to copy this voxel
if (actualMessageSize+lengthOfVoxelData > 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;
}