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:
@@ -184,7 +184,7 @@ struct UdpNetworkedInternalData
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
printf("%s disconected.\n", m_event.peer->data);
|
printf("%s disconnected.\n", m_event.peer->data);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -239,6 +239,16 @@ struct UdpNetworkedInternalData
|
|||||||
{
|
{
|
||||||
|
|
||||||
SharedMemoryStatus* statPtr = (SharedMemoryStatus*)&m_event.packet->data[4];
|
SharedMemoryStatus* statPtr = (SharedMemoryStatus*)&m_event.packet->data[4];
|
||||||
|
if (statPtr->m_type == CMD_STEP_FORWARD_SIMULATION_COMPLETED)
|
||||||
|
{
|
||||||
|
SharedMemoryStatus dummy;
|
||||||
|
dummy.m_type = CMD_STEP_FORWARD_SIMULATION_COMPLETED;
|
||||||
|
m_lastStatus = dummy;
|
||||||
|
m_stream.resize(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
m_lastStatus = *statPtr;
|
m_lastStatus = *statPtr;
|
||||||
int streamOffsetInBytes = 4 + sizeof(SharedMemoryStatus);
|
int streamOffsetInBytes = 4 + sizeof(SharedMemoryStatus);
|
||||||
int numStreamBytes = packetSizeInBytes - streamOffsetInBytes;
|
int numStreamBytes = packetSizeInBytes - streamOffsetInBytes;
|
||||||
@@ -248,6 +258,7 @@ struct UdpNetworkedInternalData
|
|||||||
m_stream[i] = m_event.packet->data[i + streamOffsetInBytes];
|
m_stream[i] = m_event.packet->data[i + streamOffsetInBytes];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("unknown status message received\n");
|
printf("unknown status message received\n");
|
||||||
@@ -258,7 +269,7 @@ struct UdpNetworkedInternalData
|
|||||||
}
|
}
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
{
|
{
|
||||||
printf("%s disconected.\n", m_event.peer->data);
|
printf("%s disconnected.\n", m_event.peer->data);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -360,9 +371,19 @@ void UDPThreadFunc(void* userPtr, void* lsMemory)
|
|||||||
|
|
||||||
if (hasCommand)
|
if (hasCommand)
|
||||||
{
|
{
|
||||||
|
int sz = 0;
|
||||||
|
ENetPacket *packet = 0;
|
||||||
|
|
||||||
int sz = sizeof(SharedMemoryCommand);
|
if (args->m_clientCmd.m_type == CMD_STEP_FORWARD_SIMULATION)
|
||||||
ENetPacket *packet = enet_packet_create(&args->m_clientCmd, sz, ENET_PACKET_FLAG_RELIABLE);
|
{
|
||||||
|
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;
|
int res;
|
||||||
res = enet_peer_send(args->m_peer, 0, packet);
|
res = enet_peer_send(args->m_peer, 0, packet);
|
||||||
args->m_cs->lock();
|
args->m_cs->lock();
|
||||||
@@ -475,14 +496,15 @@ bool UdpNetworkedPhysicsProcessor::processCommand(const struct SharedMemoryComma
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool UdpNetworkedPhysicsProcessor::receiveStatus(struct SharedMemoryStatus& serverStatusOut, char* bufferServerToClient, int bufferSizeInBytes)
|
bool UdpNetworkedPhysicsProcessor::receiveStatus(struct SharedMemoryStatus& serverStatusOut, char* bufferServerToClient, int bufferSizeInBytes)
|
||||||
|
{
|
||||||
|
bool hasStatus = false;
|
||||||
|
if (m_data->m_hasStatus)
|
||||||
{
|
{
|
||||||
if (gVerboseNetworkMessagesClient)
|
if (gVerboseNetworkMessagesClient)
|
||||||
{
|
{
|
||||||
printf("UdpNetworkedPhysicsProcessor::receiveStatus\n");
|
printf("UdpNetworkedPhysicsProcessor::receiveStatus\n");
|
||||||
}
|
}
|
||||||
bool hasStatus = false;
|
|
||||||
if (m_data->m_hasStatus)
|
|
||||||
{
|
|
||||||
hasStatus = true;
|
hasStatus = true;
|
||||||
serverStatusOut = m_data->m_lastStatus;
|
serverStatusOut = m_data->m_lastStatus;
|
||||||
int numStreamBytes = m_data->m_stream.size();
|
int numStreamBytes = m_data->m_stream.size();
|
||||||
@@ -553,9 +575,9 @@ bool UdpNetworkedPhysicsProcessor::connect()
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
unsigned int sharedParam = m_data->m_cs->getSharedParam(1);
|
||||||
|
bool isConnected = (sharedParam == eUDP_Connected);
|
||||||
return true;
|
return isConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UdpNetworkedPhysicsProcessor::disconnect()
|
void UdpNetworkedPhysicsProcessor::disconnect()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ b3PhysicsClientHandle b3ConnectPhysicsUDP(const char* hostName, int port)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("b3ConnectPhysicsUDP NOT connected.\n");
|
printf("b3ConnectPhysicsUDP connection failed.\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
return (b3PhysicsClientHandle)direct;
|
return (b3PhysicsClientHandle)direct;
|
||||||
|
|||||||
@@ -125,6 +125,7 @@ bool PhysicsDirect::connect()
|
|||||||
m_data->m_commandProcessor->setGuiHelper(&m_data->m_noGfx);
|
m_data->m_commandProcessor->setGuiHelper(&m_data->m_noGfx);
|
||||||
|
|
||||||
|
|
||||||
|
if (connected)
|
||||||
//also request serialization data
|
//also request serialization data
|
||||||
{
|
{
|
||||||
SharedMemoryCommand command;
|
SharedMemoryCommand command;
|
||||||
@@ -211,7 +212,7 @@ SharedMemoryCommand* PhysicsDirect::getAvailableSharedMemoryCommand()
|
|||||||
|
|
||||||
bool PhysicsDirect::canSubmitCommand() const
|
bool PhysicsDirect::canSubmitCommand() const
|
||||||
{
|
{
|
||||||
return true;
|
return m_data->m_commandProcessor->isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicsDirect::processDebugLines(const struct SharedMemoryCommand& orgCommand)
|
bool PhysicsDirect::processDebugLines(const struct SharedMemoryCommand& orgCommand)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#include "SharedMemoryCommands.h"
|
#include "SharedMemoryCommands.h"
|
||||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||||
#include "PhysicsServerCommandProcessor.h"
|
#include "PhysicsServerCommandProcessor.h"
|
||||||
|
#include "../Utils/b3Clock.h"
|
||||||
|
|
||||||
bool gVerboseNetworkMessagesServer = false;
|
bool gVerboseNetworkMessagesServer = false;
|
||||||
|
|
||||||
@@ -36,6 +36,8 @@ void MySerializeInt(unsigned int sz, unsigned char* output)
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
b3CommandLineArgs parseArgs(argc,argv);
|
b3CommandLineArgs parseArgs(argc,argv);
|
||||||
|
b3Clock clock;
|
||||||
|
double timeOutInSeconds = 10;
|
||||||
|
|
||||||
DummyGUIHelper guiHelper;
|
DummyGUIHelper guiHelper;
|
||||||
MyCommandProcessor* sm = new MyCommandProcessor;
|
MyCommandProcessor* sm = new MyCommandProcessor;
|
||||||
@@ -102,6 +104,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
b3Clock::usleep(0);
|
||||||
|
|
||||||
serviceResult = 1;
|
serviceResult = 1;
|
||||||
|
|
||||||
/* Keep doing host_service until no events are left */
|
/* Keep doing host_service until no events are left */
|
||||||
@@ -136,20 +140,36 @@ int main(int argc, char *argv[])
|
|||||||
event.peer->data,
|
event.peer->data,
|
||||||
event.channelID);
|
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))
|
if (event.packet->dataLength == sizeof(SharedMemoryCommand))
|
||||||
{
|
{
|
||||||
SharedMemoryCommand* cmdPtr = (SharedMemoryCommand*)event.packet->data;
|
cmdPtr = (SharedMemoryCommand*)event.packet->data;
|
||||||
|
}
|
||||||
|
if (cmdPtr)
|
||||||
|
{
|
||||||
SharedMemoryStatus serverStatus;
|
SharedMemoryStatus serverStatus;
|
||||||
b3AlignedObjectArray<char> buffer;
|
b3AlignedObjectArray<char> buffer;
|
||||||
buffer.resize(SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
|
buffer.resize(SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
|
||||||
|
|
||||||
bool hasStatus = sm->processCommand(*cmdPtr,serverStatus, &buffer[0], buffer.size());
|
bool hasStatus = sm->processCommand(*cmdPtr,serverStatus, &buffer[0], buffer.size());
|
||||||
|
|
||||||
int timeout = 1024 * 1024 * 1024;
|
double startTimeSeconds = clock.getTimeInSeconds();
|
||||||
while ((!hasStatus) && (timeout-- > 0))
|
double curTimeSeconds = clock.getTimeInSeconds();
|
||||||
|
|
||||||
|
while ((!hasStatus) && ((curTimeSeconds - startTimeSeconds) <timeOutInSeconds))
|
||||||
{
|
{
|
||||||
hasStatus = sm->receiveStatus(serverStatus, &buffer[0], buffer.size());
|
hasStatus = sm->receiveStatus(serverStatus, &buffer[0], buffer.size());
|
||||||
|
curTimeSeconds = clock.getTimeInSeconds();
|
||||||
}
|
}
|
||||||
if (gVerboseNetworkMessagesServer)
|
if (gVerboseNetworkMessagesServer)
|
||||||
{
|
{
|
||||||
@@ -158,9 +178,28 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
if (hasStatus)
|
if (hasStatus)
|
||||||
{
|
{
|
||||||
//create packetData with [int packetSizeInBytes, status, streamBytes)
|
|
||||||
unsigned char* statBytes = (unsigned char*)&serverStatus;
|
|
||||||
b3AlignedObjectArray<unsigned char> packetData;
|
b3AlignedObjectArray<unsigned char> packetData;
|
||||||
|
unsigned char* statBytes = (unsigned char*)&serverStatus;
|
||||||
|
|
||||||
|
if (cmdPtr->m_type == CMD_STEP_FORWARD_SIMULATION)
|
||||||
|
{
|
||||||
|
packetData.resize(4 + sizeof(int));
|
||||||
|
int sz = packetData.size();
|
||||||
|
int curPos = 0;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
//create packetData with [int packetSizeInBytes, status, streamBytes)
|
||||||
packetData.resize(4 + sizeof(SharedMemoryStatus) + serverStatus.m_numDataStreamBytes);
|
packetData.resize(4 + sizeof(SharedMemoryStatus) + serverStatus.m_numDataStreamBytes);
|
||||||
int sz = packetData.size();
|
int sz = packetData.size();
|
||||||
int curPos = 0;
|
int curPos = 0;
|
||||||
@@ -183,6 +222,7 @@ int main(int argc, char *argv[])
|
|||||||
//enet_host_broadcast(server, 0, packet);
|
//enet_host_broadcast(server, 0, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("received packet with unknown contents\n");
|
printf("received packet with unknown contents\n");
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ project ("App_PhysicsServerSharedMemoryBridgeUDP")
|
|||||||
"../PosixSharedMemory.h",
|
"../PosixSharedMemory.h",
|
||||||
"../../Utils/b3ResourcePath.cpp",
|
"../../Utils/b3ResourcePath.cpp",
|
||||||
"../../Utils/b3ResourcePath.h",
|
"../../Utils/b3ResourcePath.h",
|
||||||
|
"../../Utils/b3Clock.cpp",
|
||||||
|
"../../Utils/b3Clock.h",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ int main(int argc, char* argv[]) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
printf("%s disconected.\n", event.peer->data);
|
printf("%s disconnected.\n", event.peer->data);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ int main(int argc, char *argv[])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
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. */
|
/* Reset the peer's client information. */
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ int main(int argc, char* argv[])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
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. */
|
/* Reset the peer's client information. */
|
||||||
event.peer -> data = NULL;
|
event.peer -> data = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ int main(int argc, char* argv[])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
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. */
|
/* Reset the peer's client information. */
|
||||||
event.peer -> data = NULL;
|
event.peer -> data = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user