Skip to content

Commit

Permalink
allow streaming of data
Browse files Browse the repository at this point in the history
  • Loading branch information
erwincoumans committed Oct 7, 2020
1 parent 9b81709 commit ad09940
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 15 deletions.
21 changes: 20 additions & 1 deletion examples/SharedMemory/PhysicsClientSharedMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus()

case CMD_CUSTOM_COMMAND_COMPLETED:
{

m_data->m_cachedReturnData.resize(serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes);
m_data->m_cachedReturnDataValue.m_length = serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes;

Expand All @@ -1365,7 +1366,7 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus()
m_data->m_cachedReturnDataValue.m_data1 = &m_data->m_cachedReturnData[0];
for (int i = 0; i < serverCmd.m_numDataStreamBytes; i++)
{
m_data->m_cachedReturnData[i] = m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[i];
m_data->m_cachedReturnData[i + serverCmd.m_customCommandResultArgs.m_returnDataStart] = m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[i];
}
}
break;
Expand Down Expand Up @@ -1680,6 +1681,7 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus()
m_data->m_lastServerStatus = m_data->m_tempBackupServerStatus;
}


if (serverCmd.m_type == CMD_REMOVE_USER_DATA_COMPLETED)
{
B3_PROFILE("CMD_REMOVE_USER_DATA_COMPLETED");
Expand Down Expand Up @@ -1867,6 +1869,23 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus()
return 0;
}

if (serverCmd.m_type == CMD_CUSTOM_COMMAND_COMPLETED)
{
int totalReceived = (serverCmd.m_numDataStreamBytes + serverCmd.m_customCommandResultArgs.m_returnDataStart);
int remaining = serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes - totalReceived;

if (remaining > 0)
{
// continue requesting return data
SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
command.m_type = CMD_CUSTOM_COMMAND;
command.m_customCommandArgs.m_startingReturnBytes =
totalReceived;
submitClientCommand(command);
return 0;
}
}

return &m_data->m_lastServerStatus;
}
else
Expand Down
88 changes: 76 additions & 12 deletions examples/SharedMemory/PhysicsDirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1312,18 +1312,6 @@ void PhysicsDirect::postProcessStatus(const struct SharedMemoryStatus& serverCmd
}
case CMD_CUSTOM_COMMAND_COMPLETED:
{
m_data->m_cachedReturnData.resize(serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes);
m_data->m_cachedReturnDataValue.m_length = serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes;

if (serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes)
{
m_data->m_cachedReturnDataValue.m_type = serverCmd.m_customCommandResultArgs.m_returnDataType;
m_data->m_cachedReturnDataValue.m_data1 = &m_data->m_cachedReturnData[0];
for (int i = 0; i < serverCmd.m_numDataStreamBytes; i++)
{
m_data->m_cachedReturnData[i] = m_data->m_bulletStreamDataServerToClient[i];
}
}
break;
}
default:
Expand All @@ -1333,8 +1321,84 @@ void PhysicsDirect::postProcessStatus(const struct SharedMemoryStatus& serverCmd

};
}


bool PhysicsDirect::processCustomCommand(const struct SharedMemoryCommand& orgCommand)
{
SharedMemoryCommand command = orgCommand;

const SharedMemoryStatus& serverCmd = m_data->m_serverStatus;

int remaining = 0;
do
{
bool hasStatus = m_data->m_commandProcessor->processCommand(command, m_data->m_serverStatus, &m_data->m_bulletStreamDataServerToClient[0], SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);

b3Clock clock;
double startTime = clock.getTimeInSeconds();
double timeOutInSeconds = m_data->m_timeOutInSeconds;

while ((!hasStatus) && (clock.getTimeInSeconds() - startTime < timeOutInSeconds))
{
const SharedMemoryStatus* stat = processServerStatus();
if (stat)
{
hasStatus = true;
}
}

m_data->m_hasStatus = hasStatus;

if (hasStatus)
{

if (m_data->m_verboseOutput)
{
b3Printf("Success receiving %d return data\n",
serverCmd.m_numDataStreamBytes);
}


btAssert(m_data->m_serverStatus.m_type == CMD_CUSTOM_COMMAND_COMPLETED);

if (m_data->m_serverStatus.m_type == CMD_CUSTOM_COMMAND_COMPLETED)
{
m_data->m_cachedReturnData.resize(serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes);
m_data->m_cachedReturnDataValue.m_length = serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes;

if (serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes)
{
m_data->m_cachedReturnDataValue.m_type = serverCmd.m_customCommandResultArgs.m_returnDataType;
m_data->m_cachedReturnDataValue.m_data1 = &m_data->m_cachedReturnData[0];
for (int i = 0; i < serverCmd.m_numDataStreamBytes; i++)
{
m_data->m_cachedReturnData[i+ serverCmd.m_customCommandResultArgs.m_returnDataStart] = m_data->m_bulletStreamDataServerToClient[i];
}
}
int totalReceived = serverCmd.m_numDataStreamBytes + serverCmd.m_customCommandResultArgs.m_returnDataStart;
remaining = serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes - totalReceived;

if (remaining > 0)
{
m_data->m_hasStatus = false;
command.m_type = CMD_CUSTOM_COMMAND;
command.m_customCommandArgs.m_startingReturnBytes =
totalReceived;
}
}
}

} while (remaining > 0);

return m_data->m_hasStatus;
}

bool PhysicsDirect::submitClientCommand(const struct SharedMemoryCommand& command)
{
if (command.m_type == CMD_CUSTOM_COMMAND)
{
return processCustomCommand(command);
}
if (command.m_type == CMD_REQUEST_DEBUG_LINES)
{
return processDebugLines(command);
Expand Down
4 changes: 3 additions & 1 deletion examples/SharedMemory/PhysicsDirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class PhysicsDirect : public PhysicsClient

bool processVisualShapeData(const struct SharedMemoryCommand& orgCommand);

bool processMeshData(const struct SharedMemoryCommand& orgCommand);
bool processMeshData(const struct SharedMemoryCommand& orgCommand);

void processBodyJointInfo(int bodyUniqueId, const struct SharedMemoryStatus& serverCmd);

void processAddUserData(const struct SharedMemoryStatus& serverCmd);

bool processCustomCommand(const struct SharedMemoryCommand& orgCommand);

void postProcessStatus(const struct SharedMemoryStatus& serverCmd);

void resetData();
Expand Down
2 changes: 1 addition & 1 deletion examples/SharedMemory/PhysicsServerCommandProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5802,7 +5802,7 @@ bool PhysicsServerCommandProcessor::processCustomCommand(const struct SharedMemo
serverStatusOut.m_numDataStreamBytes = numBytes;
for (int i = 0; i < numBytes; i++)
{
bufferServerToClient[i] = returnData->m_data1[i];
bufferServerToClient[i] = returnData->m_data1[i+ startBytes];
}
serverCmd.m_customCommandResultArgs.m_returnDataSizeInBytes = returnData->m_length;
serverCmd.m_customCommandResultArgs.m_returnDataType = returnData->m_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ while (1):
struct MyRendererPluginClass
{
TinyRendererVisualShapeConverter m_renderer;
b3UserDataValue* m_returnData;
MyRendererPluginClass()
:m_returnData(0)
{
}
virtual ~MyRendererPluginClass()
{
if (m_returnData)
{
delete[] m_returnData->m_data1;
}
delete m_returnData;
}
};

Expand All @@ -38,6 +45,20 @@ B3_SHARED_API int initPlugin_tinyRendererPlugin(struct b3PluginContext* context)

B3_SHARED_API int executePluginCommand_tinyRendererPlugin(struct b3PluginContext* context, const struct b3PluginArguments* arguments)
{
MyRendererPluginClass* obj = (MyRendererPluginClass*)context->m_userPointer;
if (obj->m_returnData==0)
{
obj->m_returnData = new b3UserDataValue();
obj->m_returnData->m_type = 1;
obj->m_returnData->m_length = 123;
char* data = new char[obj->m_returnData->m_length];
for (int i = 0; i < obj->m_returnData->m_length; i++)
{
data[i] = i;
}
obj->m_returnData->m_data1 = data;
}
context->m_returnData = obj->m_returnData;
return -1;
}

Expand Down

0 comments on commit ad09940

Please sign in to comment.