23#ifndef PROTOCOL_MANAGER_HPP
24#define PROTOCOL_MANAGER_HPP
28#include "utils/no_copy.hpp"
30#include "utils/stk_process.hpp"
31#include "utils/synchronised.hpp"
36#include <condition_variable>
107 std::vector<std::shared_ptr<Protocol> > m_protocols;
111 void update(
int ticks,
bool async);
123 return !m_protocols.empty() &&
124 m_protocols[0]->handleConnects();
132 return !m_protocols.empty() &&
133 m_protocols[0]->handleDisconnects();
136 void addProtocol(std::shared_ptr<Protocol> p);
139 bool isEmpty()
const {
return m_protocols.empty(); }
171 std::condition_variable m_game_protocol_cv;
173 std::mutex m_game_protocol_mutex, m_protocols_mutex;
181 std::array<OneProtocolType, PROTOCOL_MAX>& protocols);
198 bool isExiting()
const {
return m_exit.load(); }
200 const std::thread& getThread()
const
205 static std::shared_ptr<ProtocolManager> createInstance();
207 static bool emptyInstance()
212 static std::shared_ptr<ProtocolManager> lock()
Class representing an event that need to pass trough the system. This is used to remove ENet dependen...
Definition: event.hpp:73
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
A simple class that stores all protocols of a certain type.
Definition: protocol_manager.hpp:105
bool handleDisconnects() const
Returns if this protocol class handles disconnect events.
Definition: protocol_manager.hpp:130
bool handleConnects() const
Returns if this protocol class handles connect events.
Definition: protocol_manager.hpp:121
void removeProtocol(std::shared_ptr< Protocol > p)
Removes a protocol from the list of protocols of a certain type.
Definition: protocol_manager.cpp:259
bool notifyEvent(Event *event)
Calls either notifyEvent(event) or notifyEventAsynchronous(evet) on all protocols.
Definition: protocol_manager.cpp:295
bool isEmpty() const
Returns if there are no protocols of this type registered.
Definition: protocol_manager.hpp:139
std::shared_ptr< Protocol > getFirstProtocol()
Returns the first protocol of a given type.
Definition: protocol_manager.hpp:116
void update(int ticks, bool async)
Calls either the synchronous update or asynchronous update function in all protocols of this type.
Definition: protocol_manager.cpp:349
Manages the protocols at runtime.
Definition: protocol_manager.hpp:97
void abort()
Stops the protocol manager.
Definition: protocol_manager.cpp:160
void findAndTerminate(ProtocolType type)
Finds a protocol with the given type and requests it to be terminated.
Definition: protocol_manager.cpp:282
static std::weak_ptr< ProtocolManager > m_protocol_manager[PT_COUNT]
Definition: protocol_manager.hpp:178
std::atomic_bool m_exit
When set to true, the main thread will exit.
Definition: protocol_manager.hpp:162
std::list< Event * > EventList
A list of network events - messages, disconnect and disconnects.
Definition: protocol_manager.hpp:151
std::shared_ptr< Protocol > getProtocol(ProtocolType type)
Get a protocol using its type.
Definition: protocol_manager.cpp:495
void asynchronousUpdate()
Updates the manager.
Definition: protocol_manager.cpp:430
bool sendEvent(Event *event, std::array< OneProtocolType, PROTOCOL_MAX > &protocols)
Sends the event to the corresponding protocol.
Definition: protocol_manager.cpp:321
void requestStart(std::shared_ptr< Protocol > protocol)
Asks the manager to start a protocol.
Definition: protocol_manager.cpp:211
void requestTerminate(std::shared_ptr< Protocol > protocol)
Notifies the manager that a protocol is terminated.
Definition: protocol_manager.cpp:225
Synchronised< EventList > m_async_events_to_process
Contains the network events to pass asynchronously to protocols (i.e.
Definition: protocol_manager.hpp:159
std::array< OneProtocolType, PROTOCOL_MAX > m_all_protocols
The list of all protocol types, each one containing a (potentially empty) list of protocols.
Definition: protocol_manager.hpp:148
void update(int ticks)
Updates the manager.
Definition: protocol_manager.cpp:370
Synchronised< EventList > m_sync_events_to_process
Contains the network events to pass synchronously to protocols (i.e.
Definition: protocol_manager.hpp:155
std::thread m_game_protocol_thread
Asynchronous game protocol thread to handle controller action as fast as possible.
Definition: protocol_manager.hpp:169
void propagateEvent(Event *event)
Function that processes incoming events.
Definition: protocol_manager.cpp:180
std::thread m_asynchronous_update_thread
Definition: protocol_manager.hpp:165
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
Defines functions to easily manipulate 8-bit network destinated strings.
Generic protocols declarations.
ProtocolType
The types that protocols can have.
Definition: protocol.hpp:43
Declares the general types that are used by the network.