From 0cf869e56a8c3421f0710acf090158add82adf70 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Thu, 5 Jan 2017 18:30:01 -0800 Subject: [PATCH] add multiple shared memory blocks, to allow multiple simultaneous connections. At the moment, MAX_SHARED_MEMORY_BLOCKS = 2 Note that the amount of shared memory is limited on many systems, 32MB, while one block is already over 1MB at the moment... You can connect to SHARED_MEMORY using the current key, and key+1. There are a few commands that cannot currently be executed in parallel among multiple connections, since the implementation of the server caches some data: rendering image (getCameraData), getting contact point data and getting debug lines. --- .../SharedMemory/PhysicsServerExample.cpp | 18 +- .../PhysicsServerSharedMemory.cpp | 258 ++++++++++-------- 2 files changed, 138 insertions(+), 138 deletions(-) diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index ca4d9e204..889c3a5b8 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -1439,23 +1439,7 @@ void PhysicsServerExample::stepSimulation(float deltaTime) - #if 0 - if (m_options == PHYSICS_SERVER_USE_RTC_CLOCK) - { - btClock rtc; - btScalar endTime = rtc.getTimeMilliseconds() + deltaTime*btScalar(800); - - while (rtc.getTimeMilliseconds()m_childGuiHelper->getRenderInterface()) { diff --git a/examples/SharedMemory/PhysicsServerSharedMemory.cpp b/examples/SharedMemory/PhysicsServerSharedMemory.cpp index 5c967c9c4..004bc048c 100644 --- a/examples/SharedMemory/PhysicsServerSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsServerSharedMemory.cpp @@ -14,7 +14,8 @@ #include "PhysicsServerCommandProcessor.h" - +//number of shared memory blocks == number of simultaneous connections +#define MAX_SHARED_MEMORY_BLOCKS 2 struct PhysicsServerSharedMemoryInternalData { @@ -25,7 +26,7 @@ struct PhysicsServerSharedMemoryInternalData SharedMemoryInterface* m_sharedMemory; bool m_ownsSharedMemory; - SharedMemoryBlock* m_testBlock1; + SharedMemoryBlock* m_testBlocks[MAX_SHARED_MEMORY_BLOCKS]; int m_sharedMemoryKey; bool m_isConnected; bool m_verboseOutput; @@ -34,27 +35,31 @@ struct PhysicsServerSharedMemoryInternalData PhysicsServerSharedMemoryInternalData() :m_sharedMemory(0), m_ownsSharedMemory(false), - m_testBlock1(0), - m_sharedMemoryKey(SHARED_MEMORY_KEY), + m_sharedMemoryKey(SHARED_MEMORY_KEY), m_isConnected(false), m_verboseOutput(false), m_commandProcessor(0) { - + + for (int i=0;im_serverCommands[0]; + SharedMemoryStatus& serverCmd =m_testBlocks[blockIndex]->m_serverCommands[0]; serverCmd .m_type = statusType; serverCmd.m_sequenceNumber = sequenceNumber; serverCmd.m_timeStamp = timeStamp; return serverCmd; } - void submitServerStatus(SharedMemoryStatus& status) + void submitServerStatus(SharedMemoryStatus& status,int blockIndex) { - m_testBlock1->m_numServerCommands++; + m_testBlocks[blockIndex]->m_numServerCommands++; } }; @@ -117,39 +122,41 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* int counter = 0; - do - { - - m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation); - if (m_data->m_testBlock1) - { - int magicId =m_data->m_testBlock1->m_magicId; - if (m_data->m_verboseOutput) - { - b3Printf("magicId = %d\n", magicId); - } - - if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) - { - InitSharedMemoryBlock(m_data->m_testBlock1); - if (m_data->m_verboseOutput) - { - b3Printf("Created and initialized shared memory block\n"); - } - m_data->m_isConnected = true; - } else - { - m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); - m_data->m_testBlock1 = 0; - m_data->m_isConnected = false; - } - } else - { - b3Error("Cannot connect to shared memory"); - m_data->m_isConnected = false; - } - } while (counter++ < 10 && !m_data->m_isConnected); + for (int block=0;blockm_testBlocks[block] = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey+block, SHARED_MEMORY_SIZE,allowCreation); + if (m_data->m_testBlocks[block]) + { + int magicId =m_data->m_testBlocks[block]->m_magicId; + if (m_data->m_verboseOutput) + { + b3Printf("magicId = %d\n", magicId); + } + + if (m_data->m_testBlocks[block]->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER) + { + InitSharedMemoryBlock(m_data->m_testBlocks[block]); + if (m_data->m_verboseOutput) + { + b3Printf("Created and initialized shared memory block\n"); + } + m_data->m_isConnected = true; + } else + { + m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey+block, SHARED_MEMORY_SIZE); + m_data->m_testBlocks[block] = 0; + m_data->m_isConnected = false; + } + } else + { + b3Error("Cannot connect to shared memory"); + m_data->m_isConnected = false; + } + } while (counter++ < 10 && !m_data->m_isConnected); + } if (!m_data->m_isConnected) { b3Error("Server cannot connect to shared memory.\n"); @@ -167,71 +174,77 @@ void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMe { b3Printf("releaseSharedMemory1\n"); } - if (m_data->m_testBlock1) - { - if (m_data->m_verboseOutput) - { - b3Printf("m_testBlock1\n"); - } - if (deInitializeSharedMemory) - { - m_data->m_testBlock1->m_magicId = 0; - if (m_data->m_verboseOutput) - { - b3Printf("De-initialized shared memory, magic id = %d\n",m_data->m_testBlock1->m_magicId); - } - } - btAssert(m_data->m_sharedMemory); - m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE); - } - if (m_data->m_sharedMemory) - { - if (m_data->m_verboseOutput) - { - b3Printf("m_sharedMemory\n"); - } - if (m_data->m_ownsSharedMemory) - { - delete m_data->m_sharedMemory; - } - m_data->m_sharedMemory = 0; - m_data->m_testBlock1 = 0; - } + for (int block = 0;blockm_testBlocks[block]) + { + if (m_data->m_verboseOutput) + { + b3Printf("m_testBlock1\n"); + } + if (deInitializeSharedMemory) + { + m_data->m_testBlocks[block]->m_magicId = 0; + if (m_data->m_verboseOutput) + { + b3Printf("De-initialized shared memory, magic id = %d\n",m_data->m_testBlocks[block]->m_magicId); + } + } + btAssert(m_data->m_sharedMemory); + m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey+block, SHARED_MEMORY_SIZE); + } + if (m_data->m_sharedMemory) + { + if (m_data->m_verboseOutput) + { + b3Printf("m_sharedMemory\n"); + } + if (m_data->m_ownsSharedMemory) + { + delete m_data->m_sharedMemory; + } + m_data->m_sharedMemory = 0; + m_data->m_testBlocks[block] = 0; + } + } } void PhysicsServerSharedMemory::releaseSharedMemory() { - if (m_data->m_verboseOutput) - { - b3Printf("releaseSharedMemory1\n"); - } - if (m_data->m_testBlock1) + for (int block = 0;blockm_verboseOutput) - { - b3Printf("m_testBlock1\n"); - } - m_data->m_testBlock1->m_magicId = 0; - if (m_data->m_verboseOutput) - { - b3Printf("magic id = %d\n",m_data->m_testBlock1->m_magicId); - } - btAssert(m_data->m_sharedMemory); - m_data->m_sharedMemory->releaseSharedMemory( m_data->m_sharedMemoryKey -, SHARED_MEMORY_SIZE); - } - if (m_data->m_sharedMemory) - { - if (m_data->m_verboseOutput) - { - b3Printf("m_sharedMemory\n"); - } - if (m_data->m_ownsSharedMemory) - { - delete m_data->m_sharedMemory; - } - m_data->m_sharedMemory = 0; - m_data->m_testBlock1 = 0; + if (m_data->m_verboseOutput) + { + b3Printf("releaseSharedMemory1\n"); + } + if (m_data->m_testBlocks[block]) + { + if (m_data->m_verboseOutput) + { + b3Printf("m_testBlock1\n"); + } + m_data->m_testBlocks[block]->m_magicId = 0; + if (m_data->m_verboseOutput) + { + b3Printf("magic id = %d\n",m_data->m_testBlocks[block]->m_magicId); + } + btAssert(m_data->m_sharedMemory); + m_data->m_sharedMemory->releaseSharedMemory( m_data->m_sharedMemoryKey+block + , SHARED_MEMORY_SIZE); + } + if (m_data->m_sharedMemory) + { + if (m_data->m_verboseOutput) + { + b3Printf("m_sharedMemory\n"); + } + if (m_data->m_ownsSharedMemory) + { + delete m_data->m_sharedMemory; + } + m_data->m_sharedMemory = 0; + m_data->m_testBlocks[block] = 0; + } } } @@ -250,30 +263,33 @@ void PhysicsServerSharedMemory::enableRealTimeSimulation(bool enableRealTimeSim) void PhysicsServerSharedMemory::processClientCommands() { - if (m_data->m_isConnected && m_data->m_testBlock1) + for (int block = 0;blockm_commandProcessor->replayLogCommand(&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); - - ///we ignore overflow of integer for now - if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands) + if (m_data->m_isConnected && m_data->m_testBlocks[block]) { - - - //until we implement a proper ring buffer, we assume always maximum of 1 outstanding commands - btAssert(m_data->m_testBlock1->m_numClientCommands==m_data->m_testBlock1->m_numProcessedClientCommands+1); + m_data->m_commandProcessor->replayLogCommand(&m_data->m_testBlocks[block]->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); - const SharedMemoryCommand& clientCmd =m_data->m_testBlock1->m_clientCommands[0]; + ///we ignore overflow of integer for now + if (m_data->m_testBlocks[block]->m_numClientCommands> m_data->m_testBlocks[block]->m_numProcessedClientCommands) + { + - m_data->m_testBlock1->m_numProcessedClientCommands++; - //todo, timeStamp - int timeStamp = 0; - SharedMemoryStatus& serverStatusOut = m_data->createServerStatus(CMD_BULLET_DATA_STREAM_RECEIVED_COMPLETED,clientCmd.m_sequenceNumber,timeStamp); - bool hasStatus = m_data->m_commandProcessor->processCommand(clientCmd, serverStatusOut,&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); - if (hasStatus) - { - m_data->submitServerStatus(serverStatusOut); - } - + //until we implement a proper ring buffer, we assume always maximum of 1 outstanding commands + btAssert(m_data->m_testBlocks[block]->m_numClientCommands==m_data->m_testBlocks[block]->m_numProcessedClientCommands+1); + + const SharedMemoryCommand& clientCmd =m_data->m_testBlocks[block]->m_clientCommands[0]; + + m_data->m_testBlocks[block]->m_numProcessedClientCommands++; + //todo, timeStamp + int timeStamp = 0; + SharedMemoryStatus& serverStatusOut = m_data->createServerStatus(CMD_BULLET_DATA_STREAM_RECEIVED_COMPLETED,clientCmd.m_sequenceNumber,timeStamp,block); + bool hasStatus = m_data->m_commandProcessor->processCommand(clientCmd, serverStatusOut,&m_data->m_testBlocks[block]->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE); + if (hasStatus) + { + m_data->submitServerStatus(serverStatusOut,block); + } + + } } } }