Thread-safe event bus for decoupled component communication. More...
#include <EventBus.hpp>
Collaboration diagram for utl::EventBus:Classes | |
| struct | EventQueue |
| Internal event queue structure. More... | |
| struct | PriorityEventQueue |
| Priority queue for events. More... | |
Public Member Functions | |
| EventBus () | |
| Constructor. | |
| ~EventBus () | |
| Destructor. | |
| EventBus (const EventBus &)=delete | |
| EventBus & | operator= (const EventBus &)=delete |
| EventBus (EventBus &&)=delete | |
| EventBus & | operator= (EventBus &&)=delete |
| bool | publish (const Event &event) |
| Publish an event to the bus. | |
| template<typename T > | |
| bool | publish (EventType type, const T &data, std::uint32_t sourceId=0, std::uint32_t targetId=0, EventPriority priority=EventPriority::NORMAL) |
| Publish event with serialized data. | |
| std::vector< Event > | consume (std::uint32_t maxEvents=100) |
| Consume events from the bus (non-blocking) | |
| std::vector< Event > | waitForEvents (std::chrono::milliseconds timeout=std::chrono::milliseconds(100), std::uint32_t maxEvents=100) |
| Consume events with timeout (blocking) | |
| std::vector< Event > | consumeType (EventType type, std::uint32_t maxEvents=100) |
| Consume events of specific type. | |
| std::vector< Event > | consumeForTarget (std::uint32_t targetId, std::uint32_t maxEvents=100) |
| Consume events targeted to specific component. | |
| void | subscribe (std::uint32_t componentId, EventType type) |
| Subscribe component to specific event types. | |
| void | unsubscribe (std::uint32_t componentId, EventType type) |
| Unsubscribe component from specific event type. | |
| void | registerComponent (std::uint32_t componentId, const std::string &name) |
| Register component name for better debugging. | |
| void | unregisterComponent (std::uint32_t componentId) |
| Unregister component. | |
| void | filterEventType (EventType type) |
| Add event type to filter (filtered events will be dropped) | |
| void | unfilterEventType (EventType type) |
| Remove event type from filter. | |
| void | setMaxQueueSize (std::uint64_t maxSize) |
| Set maximum queue size. | |
| std::uint64_t | getQueueSize () const |
| Get current queue size. | |
| EventStats | getStats () const |
| Get event bus statistics. | |
| void | clearStats () |
| Clear all statistics. | |
| void | clear () |
| Clear all events from queue. | |
| void | stop () |
| Stop the event bus. | |
| bool | isRunning () const |
| Check if event bus is running. | |
Static Public Member Functions | |
| static EventBus & | getInstance () |
| Get singleton instance of EventBus. | |
Private Member Functions | |
| void | updateStats (const Event &event, bool isPublish, bool isExpired=false) |
| Update statistics for an event. | |
Private Attributes | |
| EventQueue | m_eventQueue |
| PriorityEventQueue | m_priorityQueue |
| std::unordered_map< EventType, std::unordered_set< std::uint32_t > > | m_subscribers |
| std::mutex | m_subscribersMutex |
| EventStats | m_stats |
| std::mutex | m_statsMutex |
| std::atomic< bool > | m_running {true} |
| std::atomic< std::uint64_t > | m_maxQueueSize {10000} |
| std::unordered_set< EventType > | m_filteredTypes |
| std::mutex | m_filterMutex |
| std::unordered_map< std::uint32_t, std::string > | m_componentNames |
| std::mutex | m_componentMutex |
Thread-safe event bus for decoupled component communication.
Definition at line 30 of file EventBus.hpp.
|
inline |
Constructor.
Definition at line 91 of file EventBus.hpp.
References utl::INFO, utl::Logger::log(), m_stats, and utl::EventStats::reset().
Here is the call graph for this function:
|
inline |
Destructor.
Definition at line 100 of file EventBus.hpp.
References utl::EventBus::EventQueue::cv, utl::INFO, utl::Logger::log(), m_eventQueue, and m_running.
Here is the call graph for this function:
|
delete |
|
delete |
|
inline |
Clear all events from queue.
Definition at line 526 of file EventBus.hpp.
References utl::EventBus::EventQueue::events, utl::INFO, utl::Logger::log(), m_eventQueue, and utl::EventBus::EventQueue::mutex.
Here is the call graph for this function:
|
inline |
Clear all statistics.
Definition at line 517 of file EventBus.hpp.
References m_stats, m_statsMutex, and utl::EventStats::reset().
Here is the call graph for this function:
|
inline |
Consume events from the bus (non-blocking)
| maxEvents | Maximum number of events to consume |
Definition at line 271 of file EventBus.hpp.
References utl::EventBus::EventQueue::events, m_eventQueue, utl::EventBus::EventQueue::mutex, and updateStats().
Referenced by consumeForTarget(), consumeType(), and waitForEvents().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Consume events targeted to specific component.
| targetId | Target component ID |
| maxEvents | Maximum number of events to consume |
Definition at line 357 of file EventBus.hpp.
References consume(), utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.
Referenced by eng::AsioClient::processBusEvent(), gme::WaitingRoomScene::processEventBus(), srv::AsioServer::processEventBusEvents(), and gme::RTypeServer::update().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Consume events of specific type.
| type | Event type to filter |
| maxEvents | Maximum number of events to consume |
Definition at line 329 of file EventBus.hpp.
References consume(), utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.
Here is the call graph for this function:
|
inline |
Add event type to filter (filtered events will be dropped)
| type | Event type to filter |
Definition at line 462 of file EventBus.hpp.
References utl::INFO, utl::Logger::log(), m_filteredTypes, and m_filterMutex.
Here is the call graph for this function:
|
inlinestatic |
Get singleton instance of EventBus.
Definition at line 82 of file EventBus.hpp.
Referenced by gme::GameMulti::handleWorldStateUpdate(), gme::GameMulti::processEventBus(), and gme::GameMulti::setupEventSubscriptions().
Here is the caller graph for this function:
|
inline |
Get current queue size.
Definition at line 496 of file EventBus.hpp.
References utl::EventBus::EventQueue::events, m_eventQueue, and utl::EventBus::EventQueue::mutex.
Referenced by getStats().
Here is the caller graph for this function:
|
inline |
Get event bus statistics.
Definition at line 506 of file EventBus.hpp.
References utl::EventStats::currentQueueSize, getQueueSize(), m_stats, and m_statsMutex.
Here is the call graph for this function:
|
inline |
Check if event bus is running.
Definition at line 550 of file EventBus.hpp.
References m_running.
|
inline |
Publish an event to the bus.
| event | Event to publish |
Definition at line 118 of file EventBus.hpp.
References utl::EventBus::EventQueue::cv, utl::EventBus::EventQueue::events, utl::Logger::log(), m_eventQueue, m_filteredTypes, m_filterMutex, m_maxQueueSize, m_running, utl::EventBus::EventQueue::mutex, utl::Event::sourceId, utl::EventBus::EventQueue::totalEvents, utl::Event::type, updateStats(), and utl::WARNING.
Referenced by srv::AsioServer::broadcastGameStart(), gme::RTypeServer::broadcastWorldState(), gme::RTypeServer::checkGameOver(), srv::AsioServer::handleConnect(), eng::AsioClient::handleConnectAccept(), srv::AsioServer::handleDisconnect(), eng::AsioClient::handleEntityEvent(), srv::AsioServer::handleEntityEvent(), eng::AsioClient::handleGameStart(), eng::AsioClient::handleLobbyCreateResponse(), eng::AsioClient::handleLobbyJoinResponse(), eng::AsioClient::handleLobbyListResponse(), eng::AsioClient::handleLobbyUpdate(), srv::AsioServer::handleStartGameRequest(), eng::AsioClient::handleWorldState(), gme::GameMulti::handleWorldStateUpdate(), gme::WaitingRoomScene::leaveLobby(), publish(), and gme::WaitingRoomScene::startGame().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Publish event with serialized data.
| T | Type of data to serialize |
| type | Event type |
| data | Data to serialize |
| sourceId | Source component ID |
| targetId | Target component ID (0 for broadcast) |
| priority | Event priority |
Definition at line 174 of file EventBus.hpp.
References rnp::Serializer::getData(), utl::Logger::log(), publish(), rnp::Serializer::serializeEntityEvents(), rnp::Serializer::serializeGameStart(), rnp::Serializer::serializeLobbyCreate(), rnp::Serializer::serializeLobbyCreateResponse(), rnp::Serializer::serializeLobbyJoin(), rnp::Serializer::serializeLobbyJoinResponse(), rnp::Serializer::serializeLobbyListResponse(), rnp::Serializer::serializeLobbyUpdate(), rnp::Serializer::serializeWorldState(), utl::WARNING, rnp::Serializer::writeByte(), rnp::Serializer::writeFloat(), rnp::Serializer::writeString(), rnp::Serializer::writeUInt16(), and rnp::Serializer::writeUInt32().
Here is the call graph for this function:
|
inline |
Register component name for better debugging.
| componentId | Component ID |
| name | Component name |
Definition at line 423 of file EventBus.hpp.
References utl::INFO, utl::Logger::log(), m_componentMutex, and m_componentNames.
Referenced by eng::AsioClient::AsioClient(), srv::AsioServer::AsioServer(), gme::CreateRoomScene::CreateRoomScene(), gme::JoinRoomScene::JoinRoomScene(), gme::PlayerControllerMulti::PlayerControllerMulti(), gme::RTypeServer::RTypeServer(), gme::ServerScene::ServerScene(), gme::GameMulti::setupEventSubscriptions(), and gme::WaitingRoomScene::WaitingRoomScene().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Set maximum queue size.
| maxSize | Maximum number of events in queue |
Definition at line 490 of file EventBus.hpp.
References m_maxQueueSize.
|
inline |
Stop the event bus.
Definition at line 539 of file EventBus.hpp.
References utl::EventBus::EventQueue::cv, utl::INFO, utl::Logger::log(), m_eventQueue, and m_running.
Here is the call graph for this function:
|
inline |
Subscribe component to specific event types.
| componentId | Component ID |
| type | Event type to subscribe to |
Definition at line 384 of file EventBus.hpp.
References utl::Logger::log(), m_subscribers, m_subscribersMutex, and utl::WARNING.
Referenced by eng::AsioClient::AsioClient(), srv::AsioServer::AsioServer(), gme::CreateRoomScene::CreateRoomScene(), gme::RTypeServer::RTypeServer(), gme::GameMulti::setupEventSubscriptions(), and gme::WaitingRoomScene::setupEventSubscriptions().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Remove event type from filter.
| type | Event type to unfilter |
Definition at line 476 of file EventBus.hpp.
References utl::INFO, utl::Logger::log(), m_filteredTypes, and m_filterMutex.
Here is the call graph for this function:
|
inline |
Unregister component.
| componentId | Component ID |
Definition at line 436 of file EventBus.hpp.
References utl::INFO, utl::Logger::log(), m_componentMutex, m_componentNames, m_subscribers, and m_subscribersMutex.
Here is the call graph for this function:
|
inline |
Unsubscribe component from specific event type.
| componentId | Component ID |
| type | Event type to unsubscribe from |
Definition at line 399 of file EventBus.hpp.
References utl::Logger::log(), m_subscribers, m_subscribersMutex, and utl::WARNING.
Here is the call graph for this function:
|
inlineprivate |
Update statistics for an event.
| event | Event to update stats for |
| isPublish | True if this is a publish operation |
| isExpired | True if event expired |
Definition at line 559 of file EventBus.hpp.
References utl::EventStats::eventTypeCount, m_stats, m_statsMutex, utl::EventStats::priorityCount, utl::EventStats::sourceCount, utl::EventStats::targetCount, utl::Event::targetId, utl::EventStats::totalEventsConsumed, utl::EventStats::totalEventsExpired, and utl::EventStats::totalEventsPublished.
Referenced by consume(), and publish().
Here is the caller graph for this function:
|
inline |
Consume events with timeout (blocking)
| timeout | Maximum time to wait for events |
| maxEvents | Maximum number of events to consume |
Definition at line 309 of file EventBus.hpp.
References consume(), utl::EventBus::EventQueue::cv, m_eventQueue, and utl::EventBus::EventQueue::mutex.
Here is the call graph for this function:
|
mutableprivate |
Definition at line 75 of file EventBus.hpp.
Referenced by registerComponent(), and unregisterComponent().
|
private |
Definition at line 74 of file EventBus.hpp.
Referenced by registerComponent(), and unregisterComponent().
|
private |
Definition at line 60 of file EventBus.hpp.
Referenced by clear(), consume(), consumeForTarget(), consumeType(), getQueueSize(), publish(), stop(), waitForEvents(), and ~EventBus().
|
private |
Definition at line 70 of file EventBus.hpp.
Referenced by filterEventType(), publish(), and unfilterEventType().
|
mutableprivate |
Definition at line 71 of file EventBus.hpp.
Referenced by filterEventType(), publish(), and unfilterEventType().
|
private |
Definition at line 67 of file EventBus.hpp.
Referenced by publish(), and setMaxQueueSize().
|
private |
Definition at line 61 of file EventBus.hpp.
|
private |
Definition at line 66 of file EventBus.hpp.
Referenced by isRunning(), publish(), stop(), and ~EventBus().
|
private |
Definition at line 64 of file EventBus.hpp.
Referenced by clearStats(), EventBus(), getStats(), and updateStats().
|
mutableprivate |
Definition at line 65 of file EventBus.hpp.
Referenced by clearStats(), getStats(), and updateStats().
|
private |
Definition at line 62 of file EventBus.hpp.
Referenced by subscribe(), unregisterComponent(), and unsubscribe().
|
mutableprivate |
Definition at line 63 of file EventBus.hpp.
Referenced by subscribe(), unregisterComponent(), and unsubscribe().