mal-packet-weaver
C++20 packet serialization/deserialization library.
|
The PacketDispatcher class is responsible for managing packet dispatching and handling. More...
#include "packet-dispatcher.hpp"
Classes | |
class | SynchronizationWrapper |
A wrapper class for synchronizing and processing data. More... | |
Public Types | |
using | BasePacketPtr = std::unique_ptr<Packet> |
Alias for a unique pointer to a base packet type. | |
template<typename T > | |
using | shared_promise = std::shared_ptr<std::promise<T>> |
Alias for a shared promise of type T. | |
using | shared_packet_promise = shared_promise<BasePacketPtr> |
Alias for a shared promise of a base packet pointer. | |
using | promise_filter = std::pair<std::function<bool(BasePacketPtr const &)>, shared_packet_promise> |
Alias for a filter function paired with a shared packet promise. | |
using | handler_tuple = std::tuple<float, PacketFilterFunc<Packet>, PacketHandlerFunc<Packet>> |
Alias for a tuple containing information for packet handling. | |
Public Member Functions | |
PacketDispatcher (boost::asio::io_context &io_context) | |
Constructs a PacketDispatcher instance associated with the given io_context. | |
void | enqueue_packet (BasePacketPtr &&packet) |
Enqueues a packet for processing. | |
template<IsPacket DerivedPacket> | |
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > | await_packet (float timeout=-1.0f) |
Wait until the packet is registered in the dispatch system and return as soon as possible. | |
template<IsPacket DerivedPacket> | |
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > | await_packet (PacketFilterFunc< DerivedPacket > filter, float timeout=-1.0f) |
Wait until a packet satisfying the filter condition is registered in the dispatch system and return as soon as possible. | |
template<IsPacket DerivedPacket> | |
void | register_default_handler (PacketHandlerFunc< DerivedPacket > handler, PacketFilterFunc< DerivedPacket > filter={}, float delay=0.0f) |
Registers a default handler for the provided packet type. | |
void | register_subsystem_handler (PacketSubsystemID subsystem_id, PacketHandlerFunc< Packet > handler, PacketFilterFunc< Packet > filter={}, float delay=0.0f) |
Registers a subsystem handler for the provided packet type. | |
void | enqueue_promise (UniquePacketID packet_id, shared_packet_promise promise) |
Enqueues a promise associated with a packet. | |
void | enqueue_filter_promise (UniquePacketID packet_id, promise_filter filtered_promise) |
Enqueues a promise with a filter associated with a packet. | |
void | Destroy () |
Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is false. This way coroutines can end and unlock the remaining instances of shared_ptr. | |
Private Member Functions | |
boost::asio::awaitable< std::shared_ptr< PacketDispatcher > > | get_shared_ptr () |
Retrieves a shared pointer to the current dispatcher. | |
boost::asio::awaitable< void > | Run () |
This function represents the main loop for running a task with exponential backoff and asynchronous I/O. It processes input packets and handles them while managing delays and timers. | |
bool | fulfill_promises (UniquePacketID packet_id, BasePacketPtr &packet) |
Fulfills promises associated with a packet ID. | |
bool | fulfill_handlers (UniquePacketID packet_id, BasePacketPtr &packet, float &min_handler_timestamp, SteadyTimer &timer) |
Fulfills handlers associated with a packet ID and packet data. | |
void | push_packet (BasePacketPtr &&packet) |
Pushes an input packet to the unprocessed_packets_input_ queue. | |
boost::asio::awaitable< bool > | pop_inputs () |
Pops input packets from input queues to local maps for processing. | |
The PacketDispatcher class is responsible for managing packet dispatching and handling.
This class associates with a specific io_context and provides functionality for enqueuing packets and managing packet handlers and filters.
To correctly destroy this object, you need to call Destroy function, because coroutines share the object from this.
Definition at line 44 of file packet-dispatcher.hpp.
using mal_packet_weaver::PacketDispatcher::BasePacketPtr = std::unique_ptr<Packet> |
Alias for a unique pointer to a base packet type.
Definition at line 148 of file packet-dispatcher.hpp.
using mal_packet_weaver::PacketDispatcher::handler_tuple = std::tuple<float, PacketFilterFunc<Packet>, PacketHandlerFunc<Packet>> |
Alias for a tuple containing information for packet handling.
This type alias defines a tuple that holds information related to packet handling. The first element is a float representing a priority, the second element is a packet filter function, and the third element is a packet handler function.
Definition at line 182 of file packet-dispatcher.hpp.
using mal_packet_weaver::PacketDispatcher::promise_filter = std::pair<std::function<bool(BasePacketPtr const &)>, shared_packet_promise> |
Alias for a filter function paired with a shared packet promise.
This type alias defines a pair where the first element is a filter function that accepts a const reference to a base packet pointer and returns a boolean. The second element is a shared promise that holds a base packet pointer.
Definition at line 172 of file packet-dispatcher.hpp.
Alias for a shared promise of a base packet pointer.
Definition at line 163 of file packet-dispatcher.hpp.
using mal_packet_weaver::PacketDispatcher::shared_promise = std::shared_ptr<std::promise<T>> |
Alias for a shared promise of type T.
This type alias defines a shared pointer to a promise that holds a value of type T.
T | The type of the value held by the promise. |
Definition at line 158 of file packet-dispatcher.hpp.
mal_packet_weaver::PacketDispatcher::PacketDispatcher | ( | boost::asio::io_context & | io_context | ) |
Constructs a PacketDispatcher instance associated with the given io_context.
io_context | The io_context to associate with the dispatcher. |
Definition at line 9 of file packet-dispatcher.cpp.
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::PacketDispatcher::await_packet | ( | float | timeout = -1.0f | ) |
Wait until the packet is registered in the dispatch system and return as soon as possible.
This function template waits for a specific type of packet to be registered in the dispatch system. It can optionally wait for a specified timeout duration.
DerivedPacket | The type of packet you want to wait for. |
timeout | If less than or equal to zero, the function will not return until the promise is fulfilled. Otherwise, it will wait for the given timeout (in seconds) before returning. |
Definition at line 11 of file packet-dispatcher.inl.
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::PacketDispatcher::await_packet | ( | PacketFilterFunc< DerivedPacket > | filter, |
float | timeout = -1.0f ) |
Wait until a packet satisfying the filter condition is registered in the dispatch system and return as soon as possible.
This function template waits for a packet of a specific type, satisfying a provided filter condition, to be registered in the dispatch system. It can optionally wait for a specified timeout duration.
DerivedPacket | The type of packet you want to wait for. |
filter | A function to filter the packet. If the functor returns true, the packet will fulfill the promise. |
timeout | If less than or equal to zero, the function will not return until the promise is fulfilled. Otherwise, it will wait for the given timeout (in seconds) before returning. |
Definition at line 51 of file packet-dispatcher.inl.
|
inline |
Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is false. This way coroutines can end and unlock the remaining instances of shared_ptr.
Definition at line 306 of file packet-dispatcher.hpp.
|
inline |
Enqueues a promise with a filter associated with a packet.
This function enqueues a promise (associated with a specific packet ID) that includes a filter function. The promise will be fulfilled based on the provided filter's outcome. The enqueued promises with filters will be processed later.
packet_id | The unique packet identifier for which the filtered promise is being enqueued. |
filtered_promise | The promise filter to be enqueued. |
Definition at line 123 of file packet-dispatcher.inl.
|
inline |
Enqueues a packet for processing.
This function enqueues a unique pointer to a packet for processing by pushing it onto the internal queue.
packet | The unique pointer to the packet to be enqueued. |
Definition at line 5 of file packet-dispatcher.inl.
|
inline |
Enqueues a promise associated with a packet.
This function enqueues a promise (associated with a specific packet ID) for future fulfillment. The promise is associated with a unique packet identifier. The enqueued promises will be processed later.
packet_id | The unique packet identifier for which the promise is being enqueued. |
promise | The shared packet promise to be enqueued. |
Definition at line 116 of file packet-dispatcher.inl.
|
inlineprivate |
Fulfills handlers associated with a packet ID and packet data.
This function fulfills handlers for a given packet ID by searching for associated handlers in the default_handlers_ map. For each handler, it checks if the specified delay is greater than the packet's time alive. If so, it updates the minimum handler timestamp. Then, it checks if the associated filter condition (if any) is satisfied before executing the handler.
packet_id | The unique ID of the packet. |
packet | A reference to the packet for which handlers should be fulfilled. |
min_handler_timestamp | The minimum handler timestamp to update. |
timer | The timer used for timestamp calculations. |
true
if at least one handler was fulfilled, otherwise false
. Definition at line 165 of file packet-dispatcher.inl.
|
inlineprivate |
Fulfills promises associated with a packet ID.
This function fulfills promises from two different maps: promise_filter_map_ and promise_map_. It searches for promises in promise_filter_map_ first, and if a matching promise is found, it checks if the associated filter condition (if any) is satisfied before fulfilling the promise. If no matching promise is found in promise_filter_map_, it then searches for a promise in promise_map_ and fulfills the first one if available.
packet_id | The unique ID of the packet. |
packet | A reference to the packet to be fulfilled. |
true
if at least one promise was fulfilled, otherwise false
. Definition at line 130 of file packet-dispatcher.inl.
|
private |
Retrieves a shared pointer to the current dispatcher.
io | The boost::asio::io_context used for asynchronous operations. |
Definition at line 109 of file packet-dispatcher.cpp.
|
private |
Pops input packets from input queues to local maps for processing.
Definition at line 215 of file packet-dispatcher.cpp.
|
inlineprivate |
Pushes an input packet to the unprocessed_packets_input_ queue.
This function posts a task to the unprocessed_packets_input_strand_ to push an input packet into the unprocessed_packets_input_ queue. The packet is moved into a unique pointer, and the unprocessed_packets_input_updated_ atomic flag is set to indicate that the queue has been updated.
packet | The packet to push (as an rvalue reference). |
Definition at line 229 of file packet-dispatcher.inl.
void mal_packet_weaver::PacketDispatcher::register_default_handler | ( | PacketHandlerFunc< DerivedPacket > | handler, |
PacketFilterFunc< DerivedPacket > | filter = {}, | ||
float | delay = 0.0f ) |
Registers a default handler for the provided packet type.
This function registers a default packet handler for a specific packet type. The filter function can be provided, and if it returns false, the packet is passed to the next handler. An optional filter function can also be provided to determine whether the handler should be applied based on the packet's properties. A delay parameter can be used to postpone the handler's execution for a certain amount of time.
DerivedPacket | The type of packet for which the handler should be registered. |
handler | The packet handler function. |
filter | The packet filter function to determine whether the handler should be applied. (Optional) |
delay | The delay in seconds before the handler is executed. (Default is 0.0) |
Definition at line 94 of file packet-dispatcher.inl.
void mal_packet_weaver::PacketDispatcher::register_subsystem_handler | ( | PacketSubsystemID | subsystem_id, |
PacketHandlerFunc< Packet > | handler, | ||
PacketFilterFunc< Packet > | filter = {}, | ||
float | delay = 0.0f ) |
Registers a subsystem handler for the provided packet type.
This function registers a subsystem packet handler for a section of packets. The filter function can be provided, and if it returns false, the packet is passed to the next handler. An optional filter function can also be provided to determine whether the handler should be applied based on the packet's properties. A delay parameter can be used to postpone the handler's execution for a certain amount of time.
handler | The packet handler function. |
filter | The packet filter function to determine whether the handler should be applied. (Optional) |
delay | The delay in seconds before the handler is executed. (Default is 0.0) |
Definition at line 100 of file packet-dispatcher.cpp.
|
private |
This function represents the main loop for running a task with exponential backoff and asynchronous I/O. It processes input packets and handles them while managing delays and timers.
Definition at line 135 of file packet-dispatcher.cpp.
|
private |
Definition at line 405 of file packet-dispatcher.hpp.
|
private |
Map storing default packet handlers for each packet ID.
Definition at line 400 of file packet-dispatcher.hpp.
|
private |
Queue for storing default handlers inputs.
Definition at line 389 of file packet-dispatcher.hpp.
|
private |
Reference to the associated Boost.Asio io_context.
Definition at line 378 of file packet-dispatcher.hpp.
|
private |
Map storing promise filters for each packet ID.
Definition at line 398 of file packet-dispatcher.hpp.
|
private |
Queue for storing promise filter map inputs.
Definition at line 387 of file packet-dispatcher.hpp.
|
private |
Map storing promises for each packet ID.
Definition at line 396 of file packet-dispatcher.hpp.
|
private |
Queue for storing promise map inputs.
Definition at line 385 of file packet-dispatcher.hpp.
|
private |
Signal handler for the dispatcher. Allows to wait until the actual data has been passed.
Definition at line 380 of file packet-dispatcher.hpp.
|
private |
Map storing default packet handlers for entire subsystems, if no default_handler_ was declared
Definition at line 402 of file packet-dispatcher.hpp.
|
private |
Queue for storing subsystem handlers inputs.
Definition at line 391 of file packet-dispatcher.hpp.
|
private |
Map storing unprocessed packets for each packet ID.
Definition at line 394 of file packet-dispatcher.hpp.
|
private |
Queue for storing unprocessed input packets.
Definition at line 383 of file packet-dispatcher.hpp.