mirror of
https://github.com/bulletphysics/bullet3
synced 2025-01-06 07:40:06 +00:00
UDP networking improvements and experiments (send CMD_STEP_FORWARD_SIMULATION as 4 bytes command, status 8 bytes, instead of enormous command/status packages.
Change some timeouts to use actual RTC time instead of dummy loops 1024*1024*1024. Fix type conected -> connected
This commit is contained in:
parent
2ea7111933
commit
87bce5eacf
@ -184,7 +184,7 @@ struct UdpNetworkedInternalData
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf("%s disconected.\n", m_event.peer->data);
|
||||
printf("%s disconnected.\n", m_event.peer->data);
|
||||
|
||||
break;
|
||||
default:
|
||||
@ -239,13 +239,24 @@ struct UdpNetworkedInternalData
|
||||
{
|
||||
|
||||
SharedMemoryStatus* statPtr = (SharedMemoryStatus*)&m_event.packet->data[4];
|
||||
m_lastStatus = *statPtr;
|
||||
int streamOffsetInBytes = 4 + sizeof(SharedMemoryStatus);
|
||||
int numStreamBytes = packetSizeInBytes - streamOffsetInBytes;
|
||||
m_stream.resize(numStreamBytes);
|
||||
for (int i = 0; i < numStreamBytes; i++)
|
||||
if (statPtr->m_type == CMD_STEP_FORWARD_SIMULATION_COMPLETED)
|
||||
{
|
||||
m_stream[i] = m_event.packet->data[i + streamOffsetInBytes];
|
||||
SharedMemoryStatus dummy;
|
||||
dummy.m_type = CMD_STEP_FORWARD_SIMULATION_COMPLETED;
|
||||
m_lastStatus = dummy;
|
||||
m_stream.resize(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
m_lastStatus = *statPtr;
|
||||
int streamOffsetInBytes = 4 + sizeof(SharedMemoryStatus);
|
||||
int numStreamBytes = packetSizeInBytes - streamOffsetInBytes;
|
||||
m_stream.resize(numStreamBytes);
|
||||
for (int i = 0; i < numStreamBytes; i++)
|
||||
{
|
||||
m_stream[i] = m_event.packet->data[i + streamOffsetInBytes];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -258,7 +269,7 @@ struct UdpNetworkedInternalData
|
||||
}
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
{
|
||||
printf("%s disconected.\n", m_event.peer->data);
|
||||
printf("%s disconnected.\n", m_event.peer->data);
|
||||
|
||||
break;
|
||||
}
|
||||
@ -360,9 +371,19 @@ void UDPThreadFunc(void* userPtr, void* lsMemory)
|
||||
|
||||
if (hasCommand)
|
||||
{
|
||||
int sz = 0;
|
||||
ENetPacket *packet = 0;
|
||||
|
||||
int sz = sizeof(SharedMemoryCommand);
|
||||
ENetPacket *packet = enet_packet_create(&args->m_clientCmd, sz, ENET_PACKET_FLAG_RELIABLE);
|
||||
if (args->m_clientCmd.m_type == CMD_STEP_FORWARD_SIMULATION)
|
||||
{
|
||||
sz = sizeof(int);
|
||||
packet = enet_packet_create(&args->m_clientCmd.m_type, sz, ENET_PACKET_FLAG_RELIABLE);
|
||||
}
|
||||
else
|
||||
{
|
||||
sz = sizeof(SharedMemoryCommand);
|
||||
packet = enet_packet_create(&args->m_clientCmd, sz, ENET_PACKET_FLAG_RELIABLE);
|
||||
}
|
||||
int res;
|
||||
res = enet_peer_send(args->m_peer, 0, packet);
|
||||
args->m_cs->lock();
|
||||
@ -476,13 +497,14 @@ bool UdpNetworkedPhysicsProcessor::processCommand(const struct SharedMemoryComma
|
||||
|
||||
bool UdpNetworkedPhysicsProcessor::receiveStatus(struct SharedMemoryStatus& serverStatusOut, char* bufferServerToClient, int bufferSizeInBytes)
|
||||
{
|
||||
if (gVerboseNetworkMessagesClient)
|
||||
{
|
||||
printf("UdpNetworkedPhysicsProcessor::receiveStatus\n");
|
||||
}
|
||||
bool hasStatus = false;
|
||||
if (m_data->m_hasStatus)
|
||||
{
|
||||
if (gVerboseNetworkMessagesClient)
|
||||
{
|
||||
printf("UdpNetworkedPhysicsProcessor::receiveStatus\n");
|
||||
}
|
||||
|
||||
hasStatus = true;
|
||||
serverStatusOut = m_data->m_lastStatus;
|
||||
int numStreamBytes = m_data->m_stream.size();
|
||||
@ -553,9 +575,9 @@ bool UdpNetworkedPhysicsProcessor::connect()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
unsigned int sharedParam = m_data->m_cs->getSharedParam(1);
|
||||
bool isConnected = (sharedParam == eUDP_Connected);
|
||||
return isConnected;
|
||||
}
|
||||
|
||||
void UdpNetworkedPhysicsProcessor::disconnect()
|
||||
|
@ -20,7 +20,7 @@ b3PhysicsClientHandle b3ConnectPhysicsUDP(const char* hostName, int port)
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("b3ConnectPhysicsUDP NOT connected.\n");
|
||||
printf("b3ConnectPhysicsUDP connection failed.\n");
|
||||
|
||||
}
|
||||
return (b3PhysicsClientHandle)direct;
|
||||
|
@ -125,6 +125,7 @@ bool PhysicsDirect::connect()
|
||||
m_data->m_commandProcessor->setGuiHelper(&m_data->m_noGfx);
|
||||
|
||||
|
||||
if (connected)
|
||||
//also request serialization data
|
||||
{
|
||||
SharedMemoryCommand command;
|
||||
@ -211,7 +212,7 @@ SharedMemoryCommand* PhysicsDirect::getAvailableSharedMemoryCommand()
|
||||
|
||||
bool PhysicsDirect::canSubmitCommand() const
|
||||
{
|
||||
return true;
|
||||
return m_data->m_commandProcessor->isConnected();
|
||||
}
|
||||
|
||||
bool PhysicsDirect::processDebugLines(const struct SharedMemoryCommand& orgCommand)
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "SharedMemoryCommands.h"
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
#include "PhysicsServerCommandProcessor.h"
|
||||
|
||||
#include "../Utils/b3Clock.h"
|
||||
|
||||
bool gVerboseNetworkMessagesServer = false;
|
||||
|
||||
@ -36,6 +36,8 @@ void MySerializeInt(unsigned int sz, unsigned char* output)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
b3CommandLineArgs parseArgs(argc,argv);
|
||||
b3Clock clock;
|
||||
double timeOutInSeconds = 10;
|
||||
|
||||
DummyGUIHelper guiHelper;
|
||||
MyCommandProcessor* sm = new MyCommandProcessor;
|
||||
@ -102,6 +104,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
while (true)
|
||||
{
|
||||
b3Clock::usleep(0);
|
||||
|
||||
serviceResult = 1;
|
||||
|
||||
/* Keep doing host_service until no events are left */
|
||||
@ -136,20 +140,36 @@ int main(int argc, char *argv[])
|
||||
event.peer->data,
|
||||
event.channelID);
|
||||
}
|
||||
SharedMemoryCommand cmd;
|
||||
|
||||
SharedMemoryCommand* cmdPtr = 0;
|
||||
|
||||
//performance test
|
||||
if (event.packet->dataLength == sizeof(int))
|
||||
{
|
||||
cmdPtr = &cmd;
|
||||
cmd.m_type = *(int*)event.packet->data;
|
||||
}
|
||||
|
||||
if (event.packet->dataLength == sizeof(SharedMemoryCommand))
|
||||
{
|
||||
SharedMemoryCommand* cmdPtr = (SharedMemoryCommand*)event.packet->data;
|
||||
cmdPtr = (SharedMemoryCommand*)event.packet->data;
|
||||
}
|
||||
if (cmdPtr)
|
||||
{
|
||||
SharedMemoryStatus serverStatus;
|
||||
b3AlignedObjectArray<char> buffer;
|
||||
buffer.resize(SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
|
||||
|
||||
bool hasStatus = sm->processCommand(*cmdPtr,serverStatus, &buffer[0], buffer.size());
|
||||
|
||||
int timeout = 1024 * 1024 * 1024;
|
||||
while ((!hasStatus) && (timeout-- > 0))
|
||||
double startTimeSeconds = clock.getTimeInSeconds();
|
||||
double curTimeSeconds = clock.getTimeInSeconds();
|
||||
|
||||
while ((!hasStatus) && ((curTimeSeconds - startTimeSeconds) <timeOutInSeconds))
|
||||
{
|
||||
hasStatus = sm->receiveStatus(serverStatus, &buffer[0], buffer.size());
|
||||
|
||||
curTimeSeconds = clock.getTimeInSeconds();
|
||||
}
|
||||
if (gVerboseNetworkMessagesServer)
|
||||
{
|
||||
@ -158,29 +178,49 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
if (hasStatus)
|
||||
{
|
||||
//create packetData with [int packetSizeInBytes, status, streamBytes)
|
||||
unsigned char* statBytes = (unsigned char*)&serverStatus;
|
||||
b3AlignedObjectArray<unsigned char> packetData;
|
||||
packetData.resize(4 + sizeof(SharedMemoryStatus) + serverStatus.m_numDataStreamBytes);
|
||||
int sz = packetData.size();
|
||||
int curPos = 0;
|
||||
unsigned char* statBytes = (unsigned char*)&serverStatus;
|
||||
|
||||
MySerializeInt(sz, &packetData[curPos]);
|
||||
curPos += 4;
|
||||
for (int i = 0; i < sizeof(SharedMemoryStatus); i++)
|
||||
if (cmdPtr->m_type == CMD_STEP_FORWARD_SIMULATION)
|
||||
{
|
||||
packetData[i + curPos] = statBytes[i];
|
||||
}
|
||||
curPos += sizeof(SharedMemoryStatus);
|
||||
packetData.resize(4 + sizeof(int));
|
||||
int sz = packetData.size();
|
||||
int curPos = 0;
|
||||
|
||||
for (int i = 0; i < serverStatus.m_numDataStreamBytes; i++)
|
||||
MySerializeInt(sz, &packetData[curPos]);
|
||||
curPos += 4;
|
||||
for (int i = 0; i < sizeof(int); i++)
|
||||
{
|
||||
packetData[i + curPos] = statBytes[i];
|
||||
}
|
||||
curPos += sizeof(int);
|
||||
ENetPacket *packet = enet_packet_create(&packetData[0], packetData.size(), ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_peer_send(event.peer, 0, packet);
|
||||
}
|
||||
else
|
||||
{
|
||||
packetData[i + curPos] = buffer[i];
|
||||
}
|
||||
//create packetData with [int packetSizeInBytes, status, streamBytes)
|
||||
packetData.resize(4 + sizeof(SharedMemoryStatus) + serverStatus.m_numDataStreamBytes);
|
||||
int sz = packetData.size();
|
||||
int curPos = 0;
|
||||
|
||||
ENetPacket *packet = enet_packet_create(&packetData[0], packetData.size() , ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_peer_send(event.peer, 0, packet);
|
||||
//enet_host_broadcast(server, 0, packet);
|
||||
MySerializeInt(sz, &packetData[curPos]);
|
||||
curPos += 4;
|
||||
for (int i = 0; i < sizeof(SharedMemoryStatus); i++)
|
||||
{
|
||||
packetData[i + curPos] = statBytes[i];
|
||||
}
|
||||
curPos += sizeof(SharedMemoryStatus);
|
||||
|
||||
for (int i = 0; i < serverStatus.m_numDataStreamBytes; i++)
|
||||
{
|
||||
packetData[i + curPos] = buffer[i];
|
||||
}
|
||||
|
||||
ENetPacket *packet = enet_packet_create(&packetData[0], packetData.size(), ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_peer_send(event.peer, 0, packet);
|
||||
//enet_host_broadcast(server, 0, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -43,6 +43,8 @@ project ("App_PhysicsServerSharedMemoryBridgeUDP")
|
||||
"../PosixSharedMemory.h",
|
||||
"../../Utils/b3ResourcePath.cpp",
|
||||
"../../Utils/b3ResourcePath.h",
|
||||
"../../Utils/b3Clock.cpp",
|
||||
"../../Utils/b3Clock.h",
|
||||
}
|
||||
|
||||
|
||||
|
@ -104,7 +104,7 @@ int main(int argc, char* argv[]) {
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf("%s disconected.\n", event.peer->data);
|
||||
printf("%s disconnected.\n", event.peer->data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf("%s disconected.\n", event.peer->data);
|
||||
printf("%s disconnected.\n", event.peer->data);
|
||||
|
||||
/* Reset the peer's client information. */
|
||||
|
||||
|
@ -139,7 +139,7 @@ int main(int argc, char* argv[])
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf ("%s disconected.\n", event.peer -> data);
|
||||
printf ("%s disconnected.\n", event.peer -> data);
|
||||
/* Reset the peer's client information. */
|
||||
event.peer -> data = NULL;
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ int main(int argc, char* argv[])
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf ("%s disconected.\n", event.peer -> data);
|
||||
printf ("%s disconnected.\n", event.peer -> data);
|
||||
/* Reset the peer's client information. */
|
||||
event.peer -> data = NULL;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user