Allow game protocols self-terminated if world is gone

This commit is contained in:
Benau 2019-06-18 00:42:44 +08:00
parent 0ce185e42c
commit e344a94946
5 changed files with 39 additions and 6 deletions

View File

@ -123,8 +123,8 @@ public:
void sendMessageToPeersInServer(NetworkString *message,
bool reliable = true);
void sendToServer(NetworkString *message, bool reliable = true);
void requestStart();
void requestTerminate();
virtual void requestStart();
virtual void requestTerminate();
// ------------------------------------------------------------------------
/** \brief Notify a protocol matching the Event type of that event.
* \param event : Pointer to the event.

View File

@ -28,6 +28,7 @@
*/
GameEventsProtocol::GameEventsProtocol() : Protocol(PROTOCOL_GAME_EVENTS)
{
m_self_terminated = false;
m_last_finished_position = 1;
} // GameEventsProtocol
@ -36,6 +37,13 @@ GameEventsProtocol::~GameEventsProtocol()
{
} // ~GameEventsProtocol
// ----------------------------------------------------------------------------
void GameEventsProtocol::update(int ticks)
{
if (!World::getWorld())
requestTerminate();
} // update
// ----------------------------------------------------------------------------
bool GameEventsProtocol::notifyEvent(Event* event)
{

View File

@ -20,6 +20,8 @@ public:
GE_CHECK_LINE = 7
}; // GameEventType
private:
bool m_self_terminated;
int m_last_finished_position;
void eliminatePlayer(const NetworkString &ns);
@ -33,13 +35,21 @@ public:
void kartFinishedRace(const NetworkString &ns);
void sendStartupBoost(uint8_t kart_id);
virtual void setup() OVERRIDE {}
virtual void update(int ticks) OVERRIDE {}
virtual void update(int ticks) OVERRIDE;
virtual void asynchronousUpdate() OVERRIDE {}
// ------------------------------------------------------------------------
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE
{
return false;
} // notifyEventAsynchronous
// ------------------------------------------------------------------------
virtual void requestTerminate() OVERRIDE
{
if (m_self_terminated)
return;
m_self_terminated = true;
Protocol::requestTerminate();
}
}; // class GameEventsProtocol

View File

@ -57,7 +57,7 @@ GameProtocol::GameProtocol()
: Protocol( PROTOCOL_CONTROLLER_EVENTS)
{
m_data_to_send = getNetworkString();
m_self_terminated = false;
} // GameProtocol
//-----------------------------------------------------------------------------
@ -389,3 +389,10 @@ void GameProtocol::rewind(BareNetworkString *buffer)
std::get<3>(a));
}
} // rewind
// ----------------------------------------------------------------------------
void GameProtocol::update(int ticks)
{
if (!World::getWorld())
requestTerminate();
} // update

View File

@ -59,6 +59,7 @@ private:
* to reduce number of rollbacks. */
std::vector<int8_t> m_adjust_time;
bool m_self_terminated;
// Dummy data structure to save all kart actions.
struct Action
{
@ -105,7 +106,7 @@ public:
virtual ~GameProtocol();
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE;
virtual void update(int ticks) OVERRIDE {}
virtual void update(int ticks) OVERRIDE;
void sendActions();
void controllerAction(int kart_id, PlayerAction action,
int value, int val_l, int val_r);
@ -139,7 +140,14 @@ public:
// ------------------------------------------------------------------------
std::unique_lock<std::mutex> acquireWorldDeletingMutex() const
{ return std::unique_lock<std::mutex>(m_world_deleting_mutex); }
// ------------------------------------------------------------------------
virtual void requestTerminate() OVERRIDE
{
if (m_self_terminated)
return;
m_self_terminated = true;
Protocol::requestTerminate();
}
}; // class GameProtocol
#endif // GAME_PROTOCOL_HPP