The server waits now for all client to start (which means it will be

behind the clients by the maximum latency, which in turn means that
at server time T all client events at time T have arrived, so less
rollback necessary).
This commit is contained in:
hiker
2016-11-16 08:48:04 +11:00
parent 2f2940115e
commit ba600f40f2
5 changed files with 106 additions and 14 deletions

View File

@@ -39,6 +39,7 @@ GameEventsProtocol::~GameEventsProtocol()
*/
void GameEventsProtocol::setup()
{
m_count_ready_clients = 0;
World::getWorld()->setReadyToRace();
} // setup
@@ -63,16 +64,18 @@ bool GameEventsProtocol::notifyEvent(Event* event)
int8_t type = data.getUInt8();
switch (type)
{
case GE_ITEM_COLLECTED:
collectedItem(data); break;
case GE_KART_FINISHED_RACE:
kartFinishedRace(data); break;
case GE_START_READY_SET_GO:
receivedReadySetGo(); break;
case GE_START_READY_SET_GO:
receivedReadySetGo(); break;
case GE_CLIENT_STARTED_RSG:
receivedClientHasStarted(event); break;
case GE_ITEM_COLLECTED:
collectedItem(data); break;
case GE_KART_FINISHED_RACE:
kartFinishedRace(data); break;
default:
Log::warn("GameEventsProtocol", "Unkown message type.");
break;
default:
Log::warn("GameEventsProtocol", "Unkown message type.");
break;
}
return true;
} // notifyEvent
@@ -187,3 +190,39 @@ void GameEventsProtocol::receivedReadySetGo()
assert(NetworkConfig::get()->isClient());
World::getWorld()->startReadySetGo();
} // receivedReadySetGo
// ----------------------------------------------------------------------------
/** Called on a client when it has started its ready-set-go. The client will
* inform the server anout this. The server will wait for all clients to
* have started before it will start its own countdown.
*/
void GameEventsProtocol::clientHasStarted()
{
assert(NetworkConfig::get()->isClient());
NetworkString *ns = getNetworkString(1);
ns->setSynchronous(true);
ns->addUInt8(GE_CLIENT_STARTED_RSG);
sendToServer(ns, /*reliable*/true);
delete ns;
} // clientHasStarted
// ----------------------------------------------------------------------------
/** Called on the server when a client has signaled that it has started ready
* set go. The server waits for the last client before it starts its own
* ready set go. */
void GameEventsProtocol::receivedClientHasStarted(Event *event)
{
assert(NetworkConfig::get()->isServer());
m_count_ready_clients++;
Log::verbose("GameEvent",
"Host %d has started ready-set-go: %d out of %d done",
event->getPeer()->getHostId(), m_count_ready_clients,
STKHost::get()->getGameSetup()->getPlayerCount() );
if (m_count_ready_clients==STKHost::get()->getGameSetup()->getPlayerCount())
{
Log::verbose("GameEvent", "All %d clients have started.",
STKHost::get()->getGameSetup()->getPlayerCount());
// SIgnal the server to start now - since it is now behind the client
// times by the latency of the 'slowest' client.
World::getWorld()->startReadySetGo();
}
} // receivedClientHasStarted