From e344a9494600fd0a201e7fcbbbe273b602cf0b1d Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 18 Jun 2019 00:42:44 +0800 Subject: [PATCH] Allow game protocols self-terminated if world is gone --- src/network/protocol.hpp | 4 ++-- src/network/protocols/game_events_protocol.cpp | 8 ++++++++ src/network/protocols/game_events_protocol.hpp | 12 +++++++++++- src/network/protocols/game_protocol.cpp | 9 ++++++++- src/network/protocols/game_protocol.hpp | 12 ++++++++++-- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/network/protocol.hpp b/src/network/protocol.hpp index d72f490aa..bb4950bea 100644 --- a/src/network/protocol.hpp +++ b/src/network/protocol.hpp @@ -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. diff --git a/src/network/protocols/game_events_protocol.cpp b/src/network/protocols/game_events_protocol.cpp index ddb103acf..9db260447 100644 --- a/src/network/protocols/game_events_protocol.cpp +++ b/src/network/protocols/game_events_protocol.cpp @@ -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) { diff --git a/src/network/protocols/game_events_protocol.hpp b/src/network/protocols/game_events_protocol.hpp index 830221b01..819663caa 100644 --- a/src/network/protocols/game_events_protocol.hpp +++ b/src/network/protocols/game_events_protocol.hpp @@ -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 diff --git a/src/network/protocols/game_protocol.cpp b/src/network/protocols/game_protocol.cpp index 5d88191af..57e0fcd84 100644 --- a/src/network/protocols/game_protocol.cpp +++ b/src/network/protocols/game_protocol.cpp @@ -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 diff --git a/src/network/protocols/game_protocol.hpp b/src/network/protocols/game_protocol.hpp index e0961340f..ff0582b17 100644 --- a/src/network/protocols/game_protocol.hpp +++ b/src/network/protocols/game_protocol.hpp @@ -59,6 +59,7 @@ private: * to reduce number of rollbacks. */ std::vector 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 acquireWorldDeletingMutex() const { return std::unique_lock(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