From 87bce5eacff4e8f22e05b2e0c617b117a79169bf Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Sat, 28 Jan 2017 22:14:45 -0800 Subject: [PATCH] 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 --- examples/SharedMemory/PhysicsClientUDP.cpp | 56 +++++++++---- .../SharedMemory/PhysicsClientUDP_C_API.cpp | 2 +- examples/SharedMemory/PhysicsDirect.cpp | 3 +- examples/SharedMemory/udp/main.cpp | 84 ++++++++++++++----- examples/SharedMemory/udp/premake4.lua | 2 + test/enet/chat/client/main.cpp | 2 +- test/enet/chat/server/main.cpp | 2 +- test/enet/nat_punchthrough/client/main.cpp | 2 +- test/enet/nat_punchthrough/server/main.cpp | 2 +- 9 files changed, 110 insertions(+), 45 deletions(-) diff --git a/examples/SharedMemory/PhysicsClientUDP.cpp b/examples/SharedMemory/PhysicsClientUDP.cpp index 66c29fbe2..cd4cef440 100644 --- a/examples/SharedMemory/PhysicsClientUDP.cpp +++ b/examples/SharedMemory/PhysicsClientUDP.cpp @@ -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() diff --git a/examples/SharedMemory/PhysicsClientUDP_C_API.cpp b/examples/SharedMemory/PhysicsClientUDP_C_API.cpp index c0a1adbfc..c3b024c43 100644 --- a/examples/SharedMemory/PhysicsClientUDP_C_API.cpp +++ b/examples/SharedMemory/PhysicsClientUDP_C_API.cpp @@ -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; diff --git a/examples/SharedMemory/PhysicsDirect.cpp b/examples/SharedMemory/PhysicsDirect.cpp index 414fb349a..bd54f02e6 100644 --- a/examples/SharedMemory/PhysicsDirect.cpp +++ b/examples/SharedMemory/PhysicsDirect.cpp @@ -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) diff --git a/examples/SharedMemory/udp/main.cpp b/examples/SharedMemory/udp/main.cpp index af0a76e95..3a3f07ac7 100644 --- a/examples/SharedMemory/udp/main.cpp +++ b/examples/SharedMemory/udp/main.cpp @@ -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 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) 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 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 diff --git a/examples/SharedMemory/udp/premake4.lua b/examples/SharedMemory/udp/premake4.lua index 8517cf51c..7f3ef58a8 100644 --- a/examples/SharedMemory/udp/premake4.lua +++ b/examples/SharedMemory/udp/premake4.lua @@ -43,6 +43,8 @@ project ("App_PhysicsServerSharedMemoryBridgeUDP") "../PosixSharedMemory.h", "../../Utils/b3ResourcePath.cpp", "../../Utils/b3ResourcePath.h", + "../../Utils/b3Clock.cpp", + "../../Utils/b3Clock.h", } diff --git a/test/enet/chat/client/main.cpp b/test/enet/chat/client/main.cpp index b7387fac1..8f4a9ea99 100644 --- a/test/enet/chat/client/main.cpp +++ b/test/enet/chat/client/main.cpp @@ -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; } diff --git a/test/enet/chat/server/main.cpp b/test/enet/chat/server/main.cpp index cc15bd6a6..ed708131b 100644 --- a/test/enet/chat/server/main.cpp +++ b/test/enet/chat/server/main.cpp @@ -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. */ diff --git a/test/enet/nat_punchthrough/client/main.cpp b/test/enet/nat_punchthrough/client/main.cpp index 2c0696869..ceb6fe1ff 100644 --- a/test/enet/nat_punchthrough/client/main.cpp +++ b/test/enet/nat_punchthrough/client/main.cpp @@ -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; } diff --git a/test/enet/nat_punchthrough/server/main.cpp b/test/enet/nat_punchthrough/server/main.cpp index ded38f2a7..c06adeeaa 100644 --- a/test/enet/nat_punchthrough/server/main.cpp +++ b/test/enet/nat_punchthrough/server/main.cpp @@ -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; }