PhysicsServerSharedMemory.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "PhysicsServerSharedMemory.h"
  2. #include "PosixSharedMemory.h"
  3. #include "Win32SharedMemory.h"
  4. #include "../CommonInterfaces/CommonRenderInterface.h"
  5. #include "btBulletDynamicsCommon.h"
  6. #include "LinearMath/btTransform.h"
  7. #include "Bullet3Common/b3Logging.h"
  8. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  9. #include "SharedMemoryBlock.h"
  10. #include "PhysicsServerCommandProcessor.h"
  11. struct PhysicsServerSharedMemoryInternalData
  12. {
  13. ///end handle management
  14. SharedMemoryInterface* m_sharedMemory;
  15. SharedMemoryBlock* m_testBlock1;
  16. int m_sharedMemoryKey;
  17. bool m_isConnected;
  18. bool m_verboseOutput;
  19. PhysicsServerCommandProcessor* m_commandProcessor;
  20. PhysicsServerSharedMemoryInternalData()
  21. :m_sharedMemory(0),
  22. m_testBlock1(0),
  23. m_sharedMemoryKey(SHARED_MEMORY_KEY),
  24. m_isConnected(false),
  25. m_verboseOutput(false),
  26. m_commandProcessor(0)
  27. {
  28. }
  29. SharedMemoryStatus& createServerStatus(int statusType, int sequenceNumber, int timeStamp)
  30. {
  31. SharedMemoryStatus& serverCmd =m_testBlock1->m_serverCommands[0];
  32. serverCmd .m_type = statusType;
  33. serverCmd.m_sequenceNumber = sequenceNumber;
  34. serverCmd.m_timeStamp = timeStamp;
  35. return serverCmd;
  36. }
  37. void submitServerStatus(SharedMemoryStatus& status)
  38. {
  39. m_testBlock1->m_numServerCommands++;
  40. }
  41. };
  42. PhysicsServerSharedMemory::PhysicsServerSharedMemory()
  43. {
  44. m_data = new PhysicsServerSharedMemoryInternalData();
  45. #ifdef _WIN32
  46. m_data->m_sharedMemory = new Win32SharedMemoryServer();
  47. #else
  48. m_data->m_sharedMemory = new PosixSharedMemory();
  49. #endif
  50. m_data->m_commandProcessor = new PhysicsServerCommandProcessor;
  51. m_data->m_commandProcessor ->createEmptyDynamicsWorld();
  52. }
  53. PhysicsServerSharedMemory::~PhysicsServerSharedMemory()
  54. {
  55. m_data->m_commandProcessor->deleteDynamicsWorld();
  56. delete m_data->m_commandProcessor;
  57. delete m_data;
  58. }
  59. void PhysicsServerSharedMemory::setSharedMemoryKey(int key)
  60. {
  61. m_data->m_sharedMemoryKey = key;
  62. }
  63. bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* guiHelper)
  64. {
  65. m_data->m_commandProcessor->setGuiHelper(guiHelper);
  66. bool allowCreation = true;
  67. if (m_data->m_isConnected)
  68. {
  69. b3Warning("connectSharedMemory, while already connected");
  70. return m_data->m_isConnected;
  71. }
  72. int counter = 0;
  73. do
  74. {
  75. m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE,allowCreation);
  76. if (m_data->m_testBlock1)
  77. {
  78. int magicId =m_data->m_testBlock1->m_magicId;
  79. if (m_data->m_verboseOutput)
  80. {
  81. b3Printf("magicId = %d\n", magicId);
  82. }
  83. if (m_data->m_testBlock1->m_magicId !=SHARED_MEMORY_MAGIC_NUMBER)
  84. {
  85. InitSharedMemoryBlock(m_data->m_testBlock1);
  86. if (m_data->m_verboseOutput)
  87. {
  88. b3Printf("Created and initialized shared memory block\n");
  89. }
  90. m_data->m_isConnected = true;
  91. } else
  92. {
  93. m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE);
  94. m_data->m_testBlock1 = 0;
  95. m_data->m_isConnected = false;
  96. }
  97. } else
  98. {
  99. b3Error("Cannot connect to shared memory");
  100. m_data->m_isConnected = false;
  101. }
  102. } while (counter++ < 10 && !m_data->m_isConnected);
  103. if (!m_data->m_isConnected)
  104. {
  105. b3Error("Server cannot connect to shared memory.\n");
  106. }
  107. return m_data->m_isConnected;
  108. }
  109. void PhysicsServerSharedMemory::disconnectSharedMemory(bool deInitializeSharedMemory)
  110. {
  111. m_data->m_commandProcessor->setGuiHelper(0);
  112. if (m_data->m_verboseOutput)
  113. {
  114. b3Printf("releaseSharedMemory1\n");
  115. }
  116. if (m_data->m_testBlock1)
  117. {
  118. if (m_data->m_verboseOutput)
  119. {
  120. b3Printf("m_testBlock1\n");
  121. }
  122. if (deInitializeSharedMemory)
  123. {
  124. m_data->m_testBlock1->m_magicId = 0;
  125. if (m_data->m_verboseOutput)
  126. {
  127. b3Printf("De-initialized shared memory, magic id = %d\n",m_data->m_testBlock1->m_magicId);
  128. }
  129. }
  130. btAssert(m_data->m_sharedMemory);
  131. m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE);
  132. }
  133. if (m_data->m_sharedMemory)
  134. {
  135. if (m_data->m_verboseOutput)
  136. {
  137. b3Printf("m_sharedMemory\n");
  138. }
  139. delete m_data->m_sharedMemory;
  140. m_data->m_sharedMemory = 0;
  141. m_data->m_testBlock1 = 0;
  142. }
  143. }
  144. void PhysicsServerSharedMemory::releaseSharedMemory()
  145. {
  146. if (m_data->m_verboseOutput)
  147. {
  148. b3Printf("releaseSharedMemory1\n");
  149. }
  150. if (m_data->m_testBlock1)
  151. {
  152. if (m_data->m_verboseOutput)
  153. {
  154. b3Printf("m_testBlock1\n");
  155. }
  156. m_data->m_testBlock1->m_magicId = 0;
  157. if (m_data->m_verboseOutput)
  158. {
  159. b3Printf("magic id = %d\n",m_data->m_testBlock1->m_magicId);
  160. }
  161. btAssert(m_data->m_sharedMemory);
  162. m_data->m_sharedMemory->releaseSharedMemory( m_data->m_sharedMemoryKey
  163. , SHARED_MEMORY_SIZE);
  164. }
  165. if (m_data->m_sharedMemory)
  166. {
  167. if (m_data->m_verboseOutput)
  168. {
  169. b3Printf("m_sharedMemory\n");
  170. }
  171. delete m_data->m_sharedMemory;
  172. m_data->m_sharedMemory = 0;
  173. m_data->m_testBlock1 = 0;
  174. }
  175. }
  176. void PhysicsServerSharedMemory::processClientCommands()
  177. {
  178. if (m_data->m_isConnected && m_data->m_testBlock1)
  179. {
  180. #if 0
  181. m_data->m_commandProcessor->processLogCommand();
  182. if (m_data->m_logPlayback)
  183. {
  184. if (m_data->m_testBlock1->m_numServerCommands>m_data->m_testBlock1->m_numProcessedServerCommands)
  185. {
  186. m_data->m_testBlock1->m_numProcessedServerCommands++;
  187. }
  188. //push a command from log file
  189. bool hasCommand = m_data->m_logPlayback->processNextCommand(&m_data->m_testBlock1->m_clientCommands[0]);
  190. if (hasCommand)
  191. {
  192. m_data->m_testBlock1->m_numClientCommands++;
  193. }
  194. }
  195. #endif
  196. ///we ignore overflow of integer for now
  197. if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands)
  198. {
  199. //until we implement a proper ring buffer, we assume always maximum of 1 outstanding commands
  200. btAssert(m_data->m_testBlock1->m_numClientCommands==m_data->m_testBlock1->m_numProcessedClientCommands+1);
  201. const SharedMemoryCommand& clientCmd =m_data->m_testBlock1->m_clientCommands[0];
  202. m_data->m_testBlock1->m_numProcessedClientCommands++;
  203. //todo, timeStamp
  204. int timeStamp = 0;
  205. SharedMemoryStatus& serverStatusOut = m_data->createServerStatus(CMD_BULLET_DATA_STREAM_RECEIVED_COMPLETED,clientCmd.m_sequenceNumber,timeStamp);
  206. bool hasStatus = m_data->m_commandProcessor->processCommand(clientCmd, serverStatusOut,&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
  207. if (hasStatus)
  208. {
  209. m_data->submitServerStatus(serverStatusOut);
  210. }
  211. }
  212. }
  213. }
  214. void PhysicsServerSharedMemory::renderScene()
  215. {
  216. m_data->m_commandProcessor->renderScene();
  217. }
  218. void PhysicsServerSharedMemory::physicsDebugDraw(int debugDrawFlags)
  219. {
  220. m_data->m_commandProcessor->physicsDebugDraw(debugDrawFlags);
  221. }
  222. bool PhysicsServerSharedMemory::pickBody(const btVector3& rayFromWorld, const btVector3& rayToWorld)
  223. {
  224. return m_data->m_commandProcessor->pickBody(rayFromWorld,rayToWorld);
  225. }
  226. bool PhysicsServerSharedMemory::movePickedBody(const btVector3& rayFromWorld, const btVector3& rayToWorld)
  227. {
  228. return m_data->m_commandProcessor->movePickedBody(rayFromWorld,rayToWorld);
  229. }
  230. void PhysicsServerSharedMemory::removePickingConstraint()
  231. {
  232. m_data->m_commandProcessor->removePickingConstraint();
  233. }
  234. void PhysicsServerSharedMemory::enableCommandLogging(bool enable, const char* fileName)
  235. {
  236. m_data->m_commandProcessor->enableCommandLogging(enable,fileName);
  237. }
  238. void PhysicsServerSharedMemory::replayFromLogFile(const char* fileName)
  239. {
  240. m_data->m_commandProcessor->replayFromLogFile(fileName);
  241. }