tcp fixes and allow to run graphics server on mainloop (for Mac)

This commit is contained in:
Erwin Coumans 2020-03-20 16:50:44 -07:00
parent 911355fe62
commit 99c7c32b10
8 changed files with 161 additions and 18 deletions

View File

@ -158,8 +158,8 @@ void TCPThreadFunc(void* userPtr, void* lsMemory)
socket.Listen("localhost", args->m_port);
//socket.SetReceiveTimeout(1, 0);
//socket.SetNonblocking();
socket.SetReceiveTimeout(100, 100);// (1, 0);
int curNumErr = 0;

View File

@ -67,7 +67,7 @@ struct RemoteGUIHelperTCPInternalData
{
m_isConnected = false;
connect();
m_tcpSocket.SetBlocking();
}
virtual ~RemoteGUIHelperTCPInternalData()

View File

@ -357,18 +357,20 @@ class InProcessGraphicsServerSharedMemory : public PhysicsClientSharedMemory
SharedMemoryInterface* m_sharedMemory;
public:
InProcessGraphicsServerSharedMemory(int argc, char* argv[], bool useInProcessMemory)
InProcessGraphicsServerSharedMemory(int port)
{
int newargc = argc + 2;
int newargc = 3;
m_newargv = (char**)malloc(sizeof(void*) * newargc);
char* t0 = (char*)"--unused";
m_newargv[0] = t0;
for (int i = 0; i < argc; i++)
m_newargv[i + 1] = argv[i];
char* t1 = (char*)"--start_demo_name=Graphics Server";
m_newargv[argc + 1] = t1;
char portArg[1024];
sprintf(portArg, "--port=%d", port);
m_newargv[1] = t1;
m_newargv[2] = portArg;
bool useInProcessMemory = false;
m_data2 = btCreateInProcessExampleBrowser(newargc, m_newargv, useInProcessMemory);
SharedMemoryInterface* shMem = btGetSharedMemoryInterface(m_data2);
@ -429,10 +431,139 @@ public:
};
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectSharedMemory(int argc, char* argv[])
class InProcessGraphicsServerSharedMemoryMainThread : public PhysicsClientSharedMemory
{
InProcessGraphicsServerSharedMemory* cl = new InProcessGraphicsServerSharedMemory(argc, argv, 0);
btInProcessExampleBrowserMainThreadInternalData* m_data2;
char** m_newargv;
SharedMemoryCommand m_command;
GraphicsSharedMemoryBlock* m_testBlock1;
SharedMemoryInterface* m_sharedMemory;
b3Clock m_clock;
public:
InProcessGraphicsServerSharedMemoryMainThread(int port)
{
int newargc = 3;
m_newargv = (char**)malloc(sizeof(void*) * newargc);
char* t0 = (char*)"--unused";
m_newargv[0] = t0;
char* t1 = (char*)"--start_demo_name=Graphics Server";
m_newargv[1] = t1;
char portArg[1024];
sprintf(portArg, "--port=%d", port);
m_newargv[2] = portArg;
bool useInProcessMemory = false;
m_data2 = btCreateInProcessExampleBrowserMainThread(newargc, m_newargv, useInProcessMemory);
SharedMemoryInterface* shMem = btGetSharedMemoryInterfaceMainThread(m_data2);
setSharedMemoryInterface(shMem);
///////////////////
#ifdef _WIN32
m_sharedMemory = new Win32SharedMemoryServer();
#else
m_sharedMemory = new PosixSharedMemory();
#endif
/// server always has to create and initialize shared memory
bool allowCreation = false;
m_testBlock1 = (GraphicsSharedMemoryBlock*)m_sharedMemory->allocateSharedMemory(
GRAPHICS_SHARED_MEMORY_KEY, GRAPHICS_SHARED_MEMORY_SIZE, allowCreation);
m_clock.reset();
}
virtual ~InProcessGraphicsServerSharedMemoryMainThread()
{
m_sharedMemory->releaseSharedMemory(GRAPHICS_SHARED_MEMORY_KEY, GRAPHICS_SHARED_MEMORY_SIZE);
delete m_sharedMemory;
setSharedMemoryInterface(0);
btShutDownExampleBrowserMainThread(m_data2);
free(m_newargv);
}
virtual bool canSubmitCommand() const
{
btUpdateInProcessExampleBrowserMainThread(m_data2);
if (m_testBlock1)
{
if (m_testBlock1->m_magicId != GRAPHICS_SHARED_MEMORY_MAGIC_NUMBER)
{
return false;
}
}
return true;
}
virtual struct SharedMemoryCommand* getAvailableSharedMemoryCommand()
{
return &m_command;
}
virtual bool submitClientCommand(const struct SharedMemoryCommand& command)
{
switch (command.m_type)
{
default:
{
}
}
return true;
}
// return non-null if there is a status, nullptr otherwise
virtual const struct SharedMemoryStatus* processServerStatus()
{
{
if (btIsExampleBrowserMainThreadTerminated(m_data2))
{
PhysicsClientSharedMemory::disconnectSharedMemory();
}
}
{
unsigned long int ms = m_clock.getTimeMilliseconds();
if (ms > 2)
{
B3_PROFILE("m_clock.reset()");
btUpdateInProcessExampleBrowserMainThread(m_data2);
m_clock.reset();
}
}
{
b3Clock::usleep(0);
}
const SharedMemoryStatus* stat = 0;
{
stat = PhysicsClientSharedMemory::processServerStatus();
}
return stat;
}
};
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectSharedMemory(int port)
{
InProcessGraphicsServerSharedMemory* cl = new InProcessGraphicsServerSharedMemory(port);
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
cl->connect();
return (b3PhysicsClientHandle)cl;
}
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(int port)
{
InProcessGraphicsServerSharedMemoryMainThread* cl = new InProcessGraphicsServerSharedMemoryMainThread(port);
cl->setSharedMemoryKey(SHARED_MEMORY_KEY + 1);
cl->connect();
return (b3PhysicsClientHandle)cl;
}

View File

@ -16,7 +16,9 @@ extern "C"
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectSharedMemory(int argc, char* argv[]);
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThreadSharedMemory(int argc, char* argv[]);
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectSharedMemory(int argc, char* argv[]);
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectSharedMemory(int port);
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(int port);
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect(void* guiHelperPtr);
//create a shared memory physics server, with a DummyGUIHelper (no graphics)

View File

@ -904,6 +904,7 @@ enum eCONNECT_METHOD
eCONNECT_SHARED_MEMORY_GUI=14,
eCONNECT_GRAPHICS_SERVER = 15,
eCONNECT_GRAPHICS_SERVER_TCP = 16,
eCONNECT_GRAPHICS_SERVER_MAIN_THREAD=17
};
enum eURDF_Flags

View File

@ -12,7 +12,4 @@ gravId = p.addUserDebugParameter("gravity",0,10,0)
while p.isConnected():
p.stepSimulation()
grav = p.readUserDebugParameter(gravId)
#print("grav=",grav)
time.sleep(1./240.)

View File

@ -2,6 +2,7 @@ import pybullet as p
import time
p.connect(p.GRAPHICS_SERVER)
#p.connect(p.GRAPHICS_SERVER_MAIN_THREAD)
while p.isConnected():
#p.stepSimulation()
p.stepSimulation()
time.sleep(1./240.)

View File

@ -462,9 +462,18 @@ static PyObject* pybullet_connectPhysicsServer(PyObject* self, PyObject* args, P
#endif
break;
}
case eCONNECT_GRAPHICS_SERVER_MAIN_THREAD:
{
sm = b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(tcpPort);
break;
}
case eCONNECT_GRAPHICS_SERVER:
{
sm = b3CreateInProcessGraphicsServerAndConnectSharedMemory(argc, argv);
#ifdef __APPLE__
sm = b3CreateInProcessGraphicsServerAndConnectMainThreadSharedMemory(tcpPort);
#else
sm = b3CreateInProcessGraphicsServerAndConnectSharedMemory(tcpPort);
#endif
break;
}
case eCONNECT_SHARED_MEMORY_SERVER:
@ -593,7 +602,7 @@ static PyObject* pybullet_connectPhysicsServer(PyObject* self, PyObject* args, P
sPhysicsClientsGUI[freeIndex] = method;
sNumPhysicsClients++;
if (method != eCONNECT_GRAPHICS_SERVER)
if (method != eCONNECT_GRAPHICS_SERVER && method != eCONNECT_GRAPHICS_SERVER_MAIN_THREAD)
{
command = b3InitSyncBodyInfoCommand(sm);
statusHandle = b3SubmitClientCommandAndWaitStatus(sm, command);
@ -12591,6 +12600,8 @@ initpybullet(void)
PyModule_AddIntConstant(m, "GRAPHICS_CLIENT", eCONNECT_SHARED_MEMORY_GUI); // user read
PyModule_AddIntConstant(m, "GRAPHICS_SERVER", eCONNECT_GRAPHICS_SERVER); // user read
PyModule_AddIntConstant(m, "GRAPHICS_SERVER_TCP", eCONNECT_GRAPHICS_SERVER_TCP); // user read
PyModule_AddIntConstant(m, "GRAPHICS_SERVER_MAIN_THREAD", eCONNECT_GRAPHICS_SERVER_MAIN_THREAD); // user read