SuperTuxKart
|
Manages the protocols at runtime. More...
#include <protocol_manager.hpp>
Classes | |
class | OneProtocolType |
A simple class that stores all protocols of a certain type. More... | |
Public Member Functions | |
void | abort () |
Stops the protocol manager. | |
void | propagateEvent (Event *event) |
Function that processes incoming events. | |
std::shared_ptr< Protocol > | getProtocol (ProtocolType type) |
Get a protocol using its type. | |
void | requestStart (std::shared_ptr< Protocol > protocol) |
Asks the manager to start a protocol. | |
void | requestTerminate (std::shared_ptr< Protocol > protocol) |
Notifies the manager that a protocol is terminated. | |
void | findAndTerminate (ProtocolType type) |
Finds a protocol with the given type and requests it to be terminated. | |
void | update (int ticks) |
Updates the manager. | |
bool | isExiting () const |
const std::thread & | getThread () const |
Static Public Member Functions | |
static std::shared_ptr< ProtocolManager > | createInstance () |
static bool | emptyInstance () |
static std::shared_ptr< ProtocolManager > | lock () |
Private Types | |
typedef std::list< Event * > | EventList |
A list of network events - messages, disconnect and disconnects. | |
Private Member Functions | |
bool | sendEvent (Event *event, std::array< OneProtocolType, PROTOCOL_MAX > &protocols) |
Sends the event to the corresponding protocol. | |
void | asynchronousUpdate () |
Updates the manager. | |
Private Attributes | |
std::array< OneProtocolType, PROTOCOL_MAX > | m_all_protocols |
The list of all protocol types, each one containing a (potentially empty) list of protocols. | |
Synchronised< EventList > | m_sync_events_to_process |
Contains the network events to pass synchronously to protocols (i.e. | |
Synchronised< EventList > | m_async_events_to_process |
Contains the network events to pass asynchronously to protocols (i.e. | |
std::atomic_bool | m_exit |
When set to true, the main thread will exit. | |
std::thread | m_asynchronous_update_thread |
std::thread | m_game_protocol_thread |
Asynchronous game protocol thread to handle controller action as fast as possible. | |
std::condition_variable | m_game_protocol_cv |
std::mutex | m_game_protocol_mutex |
std::mutex | m_protocols_mutex |
EventList | m_controller_events_list |
Static Private Attributes | |
static std::weak_ptr< ProtocolManager > | m_protocol_manager [PT_COUNT] |
Manages the protocols at runtime.
This class is in charge of storing and managing protocols. It is a singleton as there can be only one protocol manager per game instance. Any game object that wants to start a protocol must create a protocol and give it to this singleton. The protocols are updated in two different ways: 1) Asynchronous updates: A separate threads runs that delivers asynchronous events (i.e. messages), updates each protocol, and handles new requests (start/stop protocol etc). Protocols are updated using the Protocol::asynchronousUpdate() function.
2) Synchronous updates: This is called from the main game thread, and will deliver synchronous events (i.e. messages), and updates each protocol using Protocol::update().
Since the STK main loop is not thread safe, any game changing events must (e.g. events that push a new screen, ...) be processed synchronoysly. On the other hand, asynchronous updates will be handled much more frequently, so synchronous updates should be avoided as much as possible. The sender selects if a message is synchronous or asynchronous. The network layer (separate thread) calls propagateEvent in the ProtocolManager, which will add the event to the synchronous or asynchornous queue. Protocol start/pause/... requests are also stored in a separate queue, which is thread-safe, and requests will be handled by the ProtocolManager thread, to ensure that they are processed independently from the frames per second.
Events received by ENET are queried and then handled by STKHost::mainLoop. Besides messages these events also include connection and disconnection notifications. Protocols can decide to receives those notifications or not. The Enet events are converted into STK events, which store e.g. the sender as STKPeer info, and the message data is converted into a NetworkString. This STK event is then forwarded to the corresponding protocols.
There are some protocols that can have more than one instance running at a time (e.g. on the server a connect to peer protocol). The Protocol Manager stores each protocol with the same protocol id in a OneProtocol structure (so in most cases this is just one protocol instance in one OneProtocol structure, but e.g. several connect_to_peer instances would be stored in one OneProtocoll instance. The OneProtocol instance is responsible to forward events to all protocols with the same id.
|
private |
Updates the manager.
This function processes the events queue, notifies the concerned protocols that they have events to process. Then ask all protocols to update themselves. Finally processes stored requests about starting, stopping, pausing etc... protocols. This function is called in a separate thread running in this instance. This function IS NOT FPS-dependant.
void ProtocolManager::findAndTerminate | ( | ProtocolType | type | ) |
Finds a protocol with the given type and requests it to be terminated.
If no such protocol exist it will do nothing
type | The protocol type to delete. |
std::shared_ptr< Protocol > ProtocolManager::getProtocol | ( | ProtocolType | type | ) |
Get a protocol using its type.
type | : The type of the protocol. |
void ProtocolManager::propagateEvent | ( | Event * | event | ) |
Function that processes incoming events.
This function is called by the network manager each time there is an incoming packet.
void ProtocolManager::requestStart | ( | std::shared_ptr< Protocol > | protocol | ) |
Asks the manager to start a protocol.
Add the protocol to the protocols vector.
protocol | : Protocol concerned. |
void ProtocolManager::requestTerminate | ( | std::shared_ptr< Protocol > | protocol | ) |
Notifies the manager that a protocol is terminated.
Remove a protocol from the protocols vector.
protocol | : Protocol concerned. |
|
private |
Sends the event to the corresponding protocol.
Returns true if the event can be ignored, or false otherwise.
void ProtocolManager::update | ( | int | ticks | ) |
Updates the manager.
This function processes the events queue, notifies the concerned protocols that they have events to process. Then asks all protocols to update themselves. Finally it processes stored requests about starting, stopping, pausing etc... protocols. This function is called by the main thread (i.e. from main_loop). This function IS FPS-dependant.
|
private |
Contains the network events to pass asynchronously to protocols (i.e.
from the separate ProtocolManager thread).
|
private |
Asynchronous update thread.
|
staticprivate |
Single instance of protocol manager.
|
private |
Contains the network events to pass synchronously to protocols (i.e.
from the main thread).