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.
This commit is contained in:
@@ -1439,22 +1439,6 @@ 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()<endTime)
|
||||
{
|
||||
m_physicsServer.processClientCommands();
|
||||
}
|
||||
} else
|
||||
{
|
||||
//for (int i=0;i<10;i++)
|
||||
m_physicsServer.processClientCommands();
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
if (m_multiThreadedHelper->m_childGuiHelper->getRenderInterface())
|
||||
|
||||
@@ -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,7 +35,6 @@ struct PhysicsServerSharedMemoryInternalData
|
||||
PhysicsServerSharedMemoryInternalData()
|
||||
:m_sharedMemory(0),
|
||||
m_ownsSharedMemory(false),
|
||||
m_testBlock1(0),
|
||||
m_sharedMemoryKey(SHARED_MEMORY_KEY),
|
||||
m_isConnected(false),
|
||||
m_verboseOutput(false),
|
||||
@@ -42,19 +42,24 @@ struct PhysicsServerSharedMemoryInternalData
|
||||
|
||||
{
|
||||
|
||||
for (int i=0;i<MAX_SHARED_MEMORY_BLOCKS;i++)
|
||||
{
|
||||
m_testBlocks[i]=0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SharedMemoryStatus& createServerStatus(int statusType, int sequenceNumber, int timeStamp)
|
||||
SharedMemoryStatus& createServerStatus(int statusType, int sequenceNumber, int timeStamp, int blockIndex)
|
||||
{
|
||||
SharedMemoryStatus& serverCmd =m_testBlock1->m_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,21 +122,23 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface*
|
||||
|
||||
|
||||
int counter = 0;
|
||||
for (int block=0;block<MAX_SHARED_MEMORY_BLOCKS;block++)
|
||||
{
|
||||
do
|
||||
{
|
||||
|
||||
m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation);
|
||||
if (m_data->m_testBlock1)
|
||||
m_data->m_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_testBlock1->m_magicId;
|
||||
int magicId =m_data->m_testBlocks[block]->m_magicId;
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
b3Printf("magicId = %d\n", magicId);
|
||||
}
|
||||
|
||||
if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER)
|
||||
if (m_data->m_testBlocks[block]->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER)
|
||||
{
|
||||
InitSharedMemoryBlock(m_data->m_testBlock1);
|
||||
InitSharedMemoryBlock(m_data->m_testBlocks[block]);
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
b3Printf("Created and initialized shared memory block\n");
|
||||
@@ -139,8 +146,8 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface*
|
||||
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_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey+block, SHARED_MEMORY_SIZE);
|
||||
m_data->m_testBlocks[block] = 0;
|
||||
m_data->m_isConnected = false;
|
||||
}
|
||||
} else
|
||||
@@ -149,7 +156,7 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface*
|
||||
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,7 +174,9 @@ void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMe
|
||||
{
|
||||
b3Printf("releaseSharedMemory1\n");
|
||||
}
|
||||
if (m_data->m_testBlock1)
|
||||
for (int block = 0;block<MAX_SHARED_MEMORY_BLOCKS;block++)
|
||||
{
|
||||
if (m_data->m_testBlocks[block])
|
||||
{
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
@@ -175,14 +184,14 @@ void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMe
|
||||
}
|
||||
if (deInitializeSharedMemory)
|
||||
{
|
||||
m_data->m_testBlock1->m_magicId = 0;
|
||||
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_testBlock1->m_magicId);
|
||||
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, SHARED_MEMORY_SIZE);
|
||||
m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey+block, SHARED_MEMORY_SIZE);
|
||||
}
|
||||
if (m_data->m_sharedMemory)
|
||||
{
|
||||
@@ -195,29 +204,32 @@ void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMe
|
||||
delete m_data->m_sharedMemory;
|
||||
}
|
||||
m_data->m_sharedMemory = 0;
|
||||
m_data->m_testBlock1 = 0;
|
||||
m_data->m_testBlocks[block] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsServerSharedMemory::releaseSharedMemory()
|
||||
{
|
||||
for (int block = 0;block<MAX_SHARED_MEMORY_BLOCKS;block++)
|
||||
{
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
b3Printf("releaseSharedMemory1\n");
|
||||
}
|
||||
if (m_data->m_testBlock1)
|
||||
if (m_data->m_testBlocks[block])
|
||||
{
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
b3Printf("m_testBlock1\n");
|
||||
}
|
||||
m_data->m_testBlock1->m_magicId = 0;
|
||||
m_data->m_testBlocks[block]->m_magicId = 0;
|
||||
if (m_data->m_verboseOutput)
|
||||
{
|
||||
b3Printf("magic id = %d\n",m_data->m_testBlock1->m_magicId);
|
||||
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
|
||||
m_data->m_sharedMemory->releaseSharedMemory( m_data->m_sharedMemoryKey+block
|
||||
, SHARED_MEMORY_SIZE);
|
||||
}
|
||||
if (m_data->m_sharedMemory)
|
||||
@@ -231,7 +243,8 @@ void PhysicsServerSharedMemory::releaseSharedMemory()
|
||||
delete m_data->m_sharedMemory;
|
||||
}
|
||||
m_data->m_sharedMemory = 0;
|
||||
m_data->m_testBlock1 = 0;
|
||||
m_data->m_testBlocks[block] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,33 +263,36 @@ void PhysicsServerSharedMemory::enableRealTimeSimulation(bool enableRealTimeSim)
|
||||
|
||||
void PhysicsServerSharedMemory::processClientCommands()
|
||||
{
|
||||
if (m_data->m_isConnected && m_data->m_testBlock1)
|
||||
for (int block = 0;block<MAX_SHARED_MEMORY_BLOCKS;block++)
|
||||
{
|
||||
m_data->m_commandProcessor->replayLogCommand(&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
|
||||
if (m_data->m_isConnected && m_data->m_testBlocks[block])
|
||||
{
|
||||
m_data->m_commandProcessor->replayLogCommand(&m_data->m_testBlocks[block]->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_testBlocks[block]->m_numClientCommands> m_data->m_testBlocks[block]->m_numProcessedClientCommands)
|
||||
{
|
||||
|
||||
|
||||
//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);
|
||||
btAssert(m_data->m_testBlocks[block]->m_numClientCommands==m_data->m_testBlocks[block]->m_numProcessedClientCommands+1);
|
||||
|
||||
const SharedMemoryCommand& clientCmd =m_data->m_testBlock1->m_clientCommands[0];
|
||||
const SharedMemoryCommand& clientCmd =m_data->m_testBlocks[block]->m_clientCommands[0];
|
||||
|
||||
m_data->m_testBlock1->m_numProcessedClientCommands++;
|
||||
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);
|
||||
bool hasStatus = m_data->m_commandProcessor->processCommand(clientCmd, serverStatusOut,&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
|
||||
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);
|
||||
m_data->submitServerStatus(serverStatusOut,block);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsServerSharedMemory::renderScene()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user